-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
49 lines (42 loc) · 1.54 KB
/
index.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
44
45
46
47
48
49
const { resolve } = require('path')
const base = require('./lib/server-middleware')
const defaultOptions = {
moduleName: 'oauth',
fetchUser: () => ({}),
onLogout: () => {},
scopes: [],
pageComponentPath: resolve(__dirname, './lib/route.js'),
sessionDuration: 24 * 60 * 60 * 1000 // 24 hours
}
module.exports = function NuxtOAuth (moduleOptions) {
const options = Object.assign(defaultOptions, moduleOptions, this.options.oauth)
if (typeof options.onLogout !== 'function') throw new Error('options.onLogout must be a function')
if (typeof options.fetchUser !== 'function') throw new Error('options.fetchUser must be a function')
if (options.scopes && !Array.isArray(options.scopes)) throw new Error('options.scopes must be an array')
// Setup middlewares
this.options.serverMiddleware.unshift(base(options))
this.addPlugin({
src: resolve(__dirname, 'lib/plugin.js'),
fileName: 'nuxt-oauth.plugin.js',
options: {
moduleName: options.moduleName
}
})
// Add router middleware to config
this.options.router = this.options.router || {}
this.options.router.middleware = this.options.router.middleware || []
this.options.router.middleware.push('auth')
// Setup te /auth/login route
this.extendRoutes((routes, res) => {
routes.push({
name: 'oauth-login',
path: '/auth/login',
component: res(options.pageComponentPath)
}, {
name: 'oauth-logout',
path: '/auth/logout',
component: res(options.pageComponentPath)
})
})
}
module.exports.meta = require('./package.json')