UNPKG

1.5 kBJavaScriptView Raw
1import authMiddleware from '~auth/core/middleware'
2import Middleware from './middleware'
3import Auth from '~auth/core/auth'
4import ExpiredAuthSessionError from '~auth/inc/expired-auth-session-error'
5
6// Active schemes
7<%= options.uniqueSchemes.map(path => `import ${'scheme_' + hash(path)} from '${(path || '404').replace(/\\/g,'/')}'`).join('\n') %>
8
9Middleware.auth = authMiddleware
10
11export default function (ctx, inject) {
12 // Options
13 const options = <%= JSON.stringify(options.options) %>
14
15 // Create a new Auth instance
16 const $auth = new Auth(ctx, options)
17
18 // Register strategies
19 <%=
20 options.strategies.map(strategy => {
21 const scheme = 'scheme_' + hash(options.strategyScheme.get(strategy))
22 const schemeOptions = JSON.stringify(strategy)
23 const name = strategy.name
24 return `// ${name}\n $auth.registerStrategy('${name}', new ${scheme}($auth, ${schemeOptions}))`
25 }).join('\n\n ')
26 %>
27
28 // Inject it to nuxt context as $auth
29 inject('auth', $auth)
30 ctx.$auth = $auth
31
32 // Initialize auth
33 return $auth.init().catch(error => {
34 if (process.client) {
35
36 // Don't console log expired auth session errors. This error is common, and expected to happen.
37 // The error happens whenever the user does an ssr request (reload/initial navigation) with an expired refresh
38 // token. We don't want to log this as an error.
39 if (error instanceof ExpiredAuthSessionError) {
40 return
41 }
42
43 console.error('[ERROR] [AUTH]', error)
44 }
45 })
46}
47
\No newline at end of file