-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathmodule.ts
153 lines (147 loc) · 4.6 KB
/
module.ts
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit'
import { sha256 } from 'ohash'
import { defu } from 'defu'
// Module options TypeScript interface definition
export interface ModuleOptions {}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'auth-utils',
configKey: 'auth'
},
// Default configuration options of the Nuxt module
defaults: {},
setup (options, nuxt) {
const resolver = createResolver(import.meta.url)
if (!process.env.NUXT_SESSION_PASSWORD && !nuxt.options._prepare) {
const randomPassword = sha256(`${Date.now()}${Math.random()}`).slice(0, 32)
process.env.NUXT_SESSION_PASSWORD = randomPassword
console.warn('No session password set, using a random password, please set NUXT_SESSION_PASSWORD in your .env file with at least 32 chars')
console.log(`NUXT_SESSION_PASSWORD=${randomPassword}`)
}
nuxt.options.alias['#auth-utils'] = resolver.resolve('./runtime/types/index')
// App
addImportsDir(resolver.resolve('./runtime/composables'))
addPlugin(resolver.resolve('./runtime/plugins/session.server'))
// Server
if (nuxt.options.nitro.imports !== false) {
// TODO: address https://github.com/Atinux/nuxt-auth-utils/issues/1 upstream in unimport
nuxt.options.nitro.imports = defu(nuxt.options.nitro.imports, {
presets: [
{
from: resolver.resolve('./runtime/server/utils/oauth'),
imports: ['oauth']
},
{
from: resolver.resolve('./runtime/server/utils/session'),
imports: [
'sessionHooks',
'getUserSession',
'setUserSession',
'clearUserSession',
'requireUserSession',
]
}
]
})
}
// Waiting for https://github.com/nuxt/nuxt/pull/24000/files
// addServerImportsDir(resolver.resolve('./runtime/server/utils'))
addServerHandler({
handler: resolver.resolve('./runtime/server/api/session.delete'),
route: '/api/_auth/session',
method: 'delete'
})
addServerHandler({
handler: resolver.resolve('./runtime/server/api/session.get'),
route: '/api/_auth/session',
method: 'get'
})
// Runtime Config
const runtimeConfig = nuxt.options.runtimeConfig
runtimeConfig.session = defu(runtimeConfig.session, {
name: 'nuxt-session',
password: '',
cookie: {
sameSite: 'lax'
}
})
// Security settings
runtimeConfig.nuxtAuthUtils = defu(runtimeConfig.nuxtAuthUtils, {})
runtimeConfig.nuxtAuthUtils.security = defu(runtimeConfig.nuxtAuthUtils.security, {
cookie: {
secure: true,
httpOnly: true,
sameSite: 'lax',
maxAge: 60 * 15
}
})
// OAuth settings
runtimeConfig.oauth = defu(runtimeConfig.oauth, {})
// GitHub OAuth
runtimeConfig.oauth.github = defu(runtimeConfig.oauth.github, {
clientId: '',
clientSecret: ''
})
// Spotify OAuth
runtimeConfig.oauth.spotify = defu(runtimeConfig.oauth.spotify, {
clientId: '',
clientSecret: ''
})
// Google OAuth
runtimeConfig.oauth.google = defu(runtimeConfig.oauth.google, {
clientId: '',
clientSecret: ''
})
// Twitch OAuth
runtimeConfig.oauth.twitch = defu(runtimeConfig.oauth.twitch, {
clientId: '',
clientSecret: ''
})
// Auth0 OAuth
runtimeConfig.oauth.auth0 = defu(runtimeConfig.oauth.auth0, {
clientId: '',
clientSecret: '',
domain: '',
audience: ''
})
// Microsoft OAuth
runtimeConfig.oauth.microsoft = defu(runtimeConfig.oauth.microsoft, {
clientId: '',
clientSecret: '',
tenant: '',
scope: [],
authorizationURL: '',
tokenURL: '',
userURL: ''
})
// Discord OAuth
runtimeConfig.oauth.discord = defu(runtimeConfig.oauth.discord, {
clientId: '',
clientSecret: ''
})
// Battle.net OAuth
runtimeConfig.oauth.battledotnet = defu(runtimeConfig.oauth.battledotnet, {
clientId: '',
clientSecret: ''
})
// Keycloak OAuth
runtimeConfig.oauth.keycloak = defu(runtimeConfig.oauth.keycloak, {
clientId: '',
clientSecret: '',
serverUrl: '',
realm: ''
})
// LinkedIn OAuth
runtimeConfig.oauth.linkedin = defu(runtimeConfig.oauth.linkedin, {
clientId: '',
clientSecret: ''
})
// Cognito OAuth
runtimeConfig.oauth.cognito = defu(runtimeConfig.oauth.cognito, {
clientId: '',
clientSecret: '',
region: '',
userPoolId: ''
})
}
})