88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
import NextAuth from 'next-auth'
|
|
import CredentialsProvider from 'next-auth/providers/credentials'
|
|
import TwitterProvider from 'next-auth/providers/twitter'
|
|
import GoogleProvider from 'next-auth/providers/google'
|
|
import GitHubProvider from 'next-auth/providers/github'
|
|
import axios from 'axios'
|
|
|
|
const options = {
|
|
providers: [
|
|
CredentialsProvider({
|
|
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.error.message
|
|
throw new Error(errorMessage)
|
|
}
|
|
}
|
|
}),
|
|
TwitterProvider({
|
|
clientId: process.env.NEXT_PUBLIC_TWITTER_API_KEY,
|
|
clientSecret: process.env.NEXT_PUBLIC_TWITTER_API_KEY_SECRET
|
|
}),
|
|
GoogleProvider({
|
|
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
|
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET
|
|
}),
|
|
GitHubProvider({
|
|
clientId: process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID,
|
|
clientSecret: process.env.NEXT_PUBLIC_GITHUB_CLIENT_SECRET
|
|
})
|
|
],
|
|
secret: process.env.NEXT_PUBLIC_JWT_SECRET,
|
|
session: {
|
|
strategy: 'jwt'
|
|
},
|
|
callbacks: {
|
|
async jwt({token, user, account}) {
|
|
if (user) {
|
|
let url = `${process.env.NEXT_PUBLIC_API_URL}/auth/${account.provider}/callback?access_token=${account?.accessToken}`
|
|
|
|
if (account.provider === 'twitter') {
|
|
url = `${process.env.NEXT_PUBLIC_API_URL}/auth/${account.provider}/callback?access_token=${account?.oauth_token}&access_secret=${account?.oauth_token_secret}`
|
|
}
|
|
|
|
if (account.provider === 'google' || account.provider === 'github') {
|
|
url = `${process.env.NEXT_PUBLIC_API_URL}/auth/${account.provider}/callback?access_token=${account?.access_token}`
|
|
}
|
|
|
|
const response = await fetch(url)
|
|
|
|
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)
|
|
}
|
|
},
|
|
pages: {
|
|
signIn: '/pwopose',
|
|
error: '/pwopose'
|
|
}
|
|
}
|
|
const handler = (request, response) =>
|
|
NextAuth(request, response, options)
|
|
|
|
export {handler as GET, handler as POST}
|