Create nextauth api page
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import NextAuth from 'next-auth'
|
||||
import Providers from 'next-auth/providers'
|
||||
import axios from 'axios'
|
||||
|
||||
const options = {
|
||||
providers: [
|
||||
Providers.Credentials({
|
||||
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) {
|
||||
const errorMessage = error.response.data.message[0].messages[0].message
|
||||
throw new Error(errorMessage)
|
||||
}
|
||||
}
|
||||
})
|
||||
],
|
||||
database: process.env.NEXT_PUBLIC_DATABASE_URL,
|
||||
session: {
|
||||
jwt: true
|
||||
},
|
||||
callbacks: {
|
||||
jwt: async (token, user) => {
|
||||
if (user) {
|
||||
token.jwt = user.jwt
|
||||
token.user = user.user
|
||||
}
|
||||
|
||||
return Promise.resolve(token)
|
||||
},
|
||||
session: async (session, token) => {
|
||||
session.jwt = token.jwt
|
||||
session.user = token.user
|
||||
return Promise.resolve(session)
|
||||
}
|
||||
},
|
||||
pages: {
|
||||
signIn: '/kont',
|
||||
error: '/kont'
|
||||
}
|
||||
}
|
||||
const Auth = (request, response) =>
|
||||
NextAuth(request, response, options)
|
||||
export default Auth
|
||||
Reference in New Issue
Block a user