2021-05-22 23:36:39 +02:00
|
|
|
import NextAuth from 'next-auth'
|
2022-02-03 01:59:49 +04:00
|
|
|
import CredentialsProvider from 'next-auth/providers/credentials'
|
2021-05-22 23:36:39 +02:00
|
|
|
import axios from 'axios'
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
providers: [
|
2022-02-03 01:59:49 +04:00
|
|
|
CredentialsProvider({
|
2021-05-22 23:36:39 +02:00
|
|
|
name: 'Credentials',
|
|
|
|
|
credentials: {
|
|
|
|
|
username: {label: 'Email', type: 'email', placeholder: 'email@exemple.net'},
|
|
|
|
|
password: {label: 'Password', type: 'password'}
|
|
|
|
|
},
|
|
|
|
|
authorize: async credentials => {
|
|
|
|
|
try {
|
|
|
|
|
const user = await axios.post(`${process.env.NEXT_PUBLIC_API_URL}/auth/local`, {
|
|
|
|
|
identifier: credentials.username,
|
|
|
|
|
password: credentials.password
|
|
|
|
|
})
|
|
|
|
|
if (user.data) {
|
|
|
|
|
return user.data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
} catch (error) {
|
2022-05-20 00:10:26 +04:00
|
|
|
const errorMessage = error.response.data.error.message
|
2021-05-22 23:36:39 +02:00
|
|
|
throw new Error(errorMessage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
],
|
2022-02-03 01:59:49 +04:00
|
|
|
secret: process.env.NEXT_PUBLIC_JWT_SECRET,
|
2021-05-22 23:36:39 +02:00
|
|
|
session: {
|
2022-02-03 01:59:49 +04:00
|
|
|
strategy: 'jwt'
|
2021-05-22 23:36:39 +02:00
|
|
|
},
|
2022-03-18 08:11:04 +04:00
|
|
|
callbacks: {
|
|
|
|
|
async jwt({token, user, account}) {
|
|
|
|
|
if (user) {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${process.env.NEXT_PUBLIC_API_URL}/auth/${account.provider}/callback?access_token=${account?.accessToken}`
|
|
|
|
|
)
|
|
|
|
|
const data = await response.json()
|
|
|
|
|
token.id = data.id || user.id
|
|
|
|
|
token.jwt = data.jwt || user.jwt
|
|
|
|
|
token.user = data.user || user.user
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.resolve(token)
|
|
|
|
|
},
|
|
|
|
|
async session({session, token}) {
|
|
|
|
|
session.jwt = token.jwt
|
|
|
|
|
session.user = token.user
|
|
|
|
|
return Promise.resolve(session)
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-05-22 23:36:39 +02:00
|
|
|
pages: {
|
2021-05-24 13:05:46 +02:00
|
|
|
signIn: '/soumet',
|
|
|
|
|
error: '/soumet'
|
2021-05-22 23:36:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const Auth = (request, response) =>
|
|
|
|
|
NextAuth(request, response, options)
|
|
|
|
|
export default Auth
|