UNPKG

5.32 kBtext/coffeescriptView Raw
1###
2Module dependencies
3###
4
5colors = require 'colors'
6
7class Logger
8
9 constructor: (@options) ->
10
11 @messages =
12 configuration:
13 "header": 'Connect VTEXID Options:'.bold.underline.red
14 "ttl": 'TTL: '.yellow + "#{options.ttl} seconds".green
15 "redirectUrl": 'Redirect Url: '.yellow + "#{options.redirectUrl}".green
16 "replaceWhiteList": 'Replace white-list: '.yellow + "#{options.replaceWhiteList}".green
17 "whiteListCustomRoutes": 'Routes to add in white-list: '.yellow + "#{options.addToWhiteList}".green
18 "withReturnUrl": 'Return url: '.yellow + "#{options.returnUrl}".green if options.returnUrl
19 "withoutReturnUrl": 'Return url: '.yellow + "none".green if not options.returnUrl
20 "logoutUrl": 'Logout url: '.yellow + "#{options.logoutUrl}".green
21 procedures:
22 "whiteList": '>> '.green + 'Setting white-list...'.yellow
23 "userLogout": '>> '.green + 'User logging out...'.yellow
24 "cache": ">> ".green + "Setting up cache...".yellow
25 "redirectUrl": '# '.cyan + "Resolving redirect url...".magenta
26 "cookiesParsing": '# '.cyan + "Parsing cookies...".magenta
27 "vtexIdCookieNotFound": '# '.cyan + "No vtex-id cookie found. Redirecting to login page...".magenta
28 "cacheCheck": '# '.cyan + "Checking cache for vtex-id cookie...".magenta
29 "apiRequest": '# '.cyan + "Cookie is not cached. Requesting vtex-id api for user information...".magenta
30 "loginPageRedirect": '# '.cyan + "API did not respond with a proper body. Redirecting to login page...".red
31 "unauthorized": '# '.cyan + "Could not authenticate the user. Returning 401...".red
32 withCustomParameter:
33 "whiteListRouteConfig": (route) -> " - #{route}".blue
34 "whiteListHit": (url) -> '# '.cyan + 'Route '.magenta + "#{url}".blue + " is in white-list. Bypassing...".magenta
35 "redirectUrl": (url) -> " - #{url}".blue
36 "cookie": (cookie, value) -> "#{cookie}: ".yellow + "#{value}".blue
37 "cachedUser": (user) -> '# '.cyan + "User ".magenta + "#{user}".blue + " is already in cache".magenta
38 "apiResponseCode": (code) -> ' >> '.blue + "Vtex-id api responded with code ".yellow + "#{code}".green
39 "cachingUser": (user) -> '# '.cyan + "Caching user ".magenta + "#{user}".blue
40
41 log = (message) -> console.log "[connect-vtexid] " + message
42
43 logConfigOptions: ->
44 if @options.verbosityLevel >= 1
45 console.log @messages.configuration["header"]
46 console.log @messages.configuration["ttl"]
47 console.log @messages.configuration["redirectUrl"]
48 console.log @messages.configuration["logoutUrl"]
49 console.log @messages.configuration["replaceWhiteList"] if @options.replaceWhiteList
50 console.log @messages.configuration["whiteListCustomRoutes"] if @options.addToWhiteList.length
51 console.log @messages.configuration["withReturnUrl"] if @options.returnUrl
52 console.log @messages.configuration["withoutReturnUrl"] if not @options.returnUrl
53
54 logWhiteListProcedure: ->
55 log @messages.procedures["whiteList"] if @options.verbose and @options.verbosityLevel >= 2
56
57 logWhiteListConfiguration: (whiteList) ->
58 if @options.verbose and @options.verbosityLevel >= 2
59 for route in whiteList.publicUris
60 console.log @messages.withCustomParameter["whiteListRouteConfig"](route)
61
62 logCacheSetupProcedure: ->
63 log @messages.procedures["cache"] if @options.verbose and @options.verbosityLevel >= 2
64
65 logWhiteListHit: (url) ->
66 log @messages.withCustomParameter["whiteListHit"](url) if @options.verbose and @options.verbosityLevel >= 2
67
68 logUserLogout: ->
69 log @messages.procedures["userLogout"] if @options.verbose
70
71 logRedirectUrlSetupProcedure: ->
72 log @messages.procedures["redirectUrl"] if @options.verbose and @options.verbosityLevel >= 2
73
74 logRedirectUrl: (url) ->
75 log @messages.withCustomParameter["redirectUrl"](url) if @options.verbose and @options.verbosityLevel >= 2
76
77 logCookieParsingProcedure: ->
78 log @messages.procedures["cookiesParsing"] if @options.verbose and @options.verbosityLevel >= 2
79
80 logCookies: (cookiesList) ->
81 if @options.verbose and @options.verbosityLevel >= 3
82 for cookie,value of cookiesList
83 console.log @messages.withCustomParameter["cookie"](cookie, value)
84
85 logCookieNotFound: ->
86 log @messages.procedures["vtexIdCookieNotFound"] if @options.verbose and @options.verbosityLevel >= 2
87
88 logCacheCheck: ->
89 log @messages.procedures["cacheCheck"] if @options.verbose and @options.verbosityLevel >= 2
90
91 logCachedUser: (user) ->
92 log @messages.withCustomParameter["cachedUser"](user) if @options.verbose
93
94 logApiRequest: ->
95 log @messages.procedures["apiRequest"] if @options.verbose and @options.verbosityLevel >= 2
96
97 logApiResponseCode: (code) ->
98 log @messages.withCustomParameter["apiResponseCode"](code) if @options.verbose and @options.verbosityLevel >= 2
99
100 logRedirectToLoginPage: ->
101 log @messages.procedures["loginPageRedirect"] if @options.verbose and @options.verbosityLevel >= 2
102
103 logUnauthorizedUser: ->
104 log @messages.procedures["unauthorized"] if @options.verbose and @options.verbosityLevel >= 2
105
106 logCachingUser: (user) ->
107 log @messages.withCustomParameter["cachedUser"](user) if @options.verbose
108
109module.exports = Logger