[...].ts
2.14 KB
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
import CredentialsProvider from 'next-auth/providers/credentials'
import type { NuxtError } from 'nuxt/app'
import { NuxtAuthHandler } from '#auth'
// import GoogleProvider from 'next-auth/providers/google'
// const runtimeConfig = useRuntimeConfig()
export default NuxtAuthHandler({
secret: process.env.AUTH_SECRET,
providers: [
// GoogleProvider.default({
// clientId: runtimeConfig.public.AUTH_PROVIDER_GOOGLE_CLIENT_ID,
// clientSecret: runtimeConfig.AUTH_PROVIDER_GOOGLE_CLIENT_SECRET,
// }),
// @ts-expect-error You need to use .default here for it to work during SSR. May be fixed via Vite at some point
CredentialsProvider.default({
name: 'Credentials',
credentials: {}, // Object is required but can be left empty.
async authorize(credentials: any) {
const { user } = await $fetch<any>(`${process.env.NUXT_PUBLIC_API_BASE_URL}/login/`, {
method: 'POST',
body: JSON.stringify(credentials),
}).catch((err: NuxtError) => {
throw createError({
statusCode: err.statusCode || 403,
statusMessage: JSON.stringify(err.data),
})
})
return user || null
},
}),
],
pages: {
signIn: '/login',
},
callbacks: {
jwt: async ({ token, user }) => {
/*
* For adding custom parameters to user in session, we first need to add those parameters
* in token which then will be available in the `session()` callback
*/
if (user) {
token.username = user.username
token.fullName = user.fullName
token.avatar = user.avatar
token.abilityRules = user.abilityRules
token.role = user.role
}
return token
},
async session({ session, token }) {
// Add custom params to user in session which are added in `jwt()` callback via `token` parameter
if (session.user) {
session.user.username = token.username
session.user.fullName = token.fullName
session.user.avatar = token.avatar
session.user.abilityRules = token.abilityRules
session.user.role = token.role
}
return session
},
},
})