-
Notifications
You must be signed in to change notification settings - Fork 926
/
plugin.js
43 lines (34 loc) · 1.38 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Middleware from './middleware'
import { Auth, authMiddleware, ExpiredAuthSessionError } from '~auth/runtime'
// Active schemes
<%= options.schemeImports.map(i => `import { ${i.name}${i.name !== i.as ? ' as ' + i.as : '' } } from '${i.from}'`).join('\n') %>
Middleware.auth = authMiddleware
export default function (ctx, inject) {
// Options
const options = <%= JSON.stringify(options.options, null, 2) %>
// Create a new Auth instance
const $auth = new Auth(ctx, options)
// Register strategies
<%=
options.strategies.map(strategy => {
const scheme = options.strategyScheme[strategy.name]
const schemeOptions = JSON.stringify(strategy, null, 2)
return `// ${strategy.name}\n $auth.registerStrategy('${strategy.name}', new ${scheme.as}($auth, ${schemeOptions}))`
}).join('\n\n ')
%>
// Inject it to nuxt context as $auth
inject('auth', $auth)
ctx.$auth = $auth
// Initialize auth
return $auth.init().catch(error => {
if (process.client) {
// Don't console log expired auth session errors. This error is common, and expected to happen.
// The error happens whenever the user does an ssr request (reload/initial navigation) with an expired refresh
// token. We don't want to log this as an error.
if (error instanceof ExpiredAuthSessionError) {
return
}
console.error('[ERROR] [AUTH]', error)
}
})
}