Adapt api route for auth js
This commit is contained in:
@@ -1,87 +1,81 @@
|
|||||||
import NextAuth from 'next-auth'
|
import NextAuth from "next-auth"
|
||||||
import CredentialsProvider from 'next-auth/providers/credentials'
|
import Credentials from "next-auth/providers/credentials"
|
||||||
import TwitterProvider from 'next-auth/providers/twitter'
|
import Twitter from "next-auth/providers/twitter"
|
||||||
import GoogleProvider from 'next-auth/providers/google'
|
import Google from "next-auth/providers/google"
|
||||||
import GitHubProvider from 'next-auth/providers/github'
|
import GitHub from "next-auth/providers/github"
|
||||||
import axios from 'axios'
|
import axios from "axios"
|
||||||
|
|
||||||
const options = {
|
export const { handlers, auth } = NextAuth({
|
||||||
providers: [
|
providers: [
|
||||||
CredentialsProvider({
|
Credentials({
|
||||||
name: 'Credentials',
|
|
||||||
credentials: {
|
credentials: {
|
||||||
username: {label: 'Email', type: 'email', placeholder: 'email@exemple.net'},
|
username: {},
|
||||||
password: {label: 'Password', type: 'password'}
|
password: {}
|
||||||
},
|
},
|
||||||
authorize: async credentials => {
|
async authorize(credentials) {
|
||||||
try {
|
try {
|
||||||
const user = await axios.post(`${process.env.NEXT_PUBLIC_API_URL}/auth/local`, {
|
const user = await axios.post(
|
||||||
|
`${process.env.NEXT_PUBLIC_API_URL}/auth/local`,
|
||||||
|
{
|
||||||
identifier: credentials.username,
|
identifier: credentials.username,
|
||||||
password: credentials.password
|
password: credentials.password
|
||||||
})
|
|
||||||
if (user.data) {
|
|
||||||
return user.data
|
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
return null
|
return user.data || null
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage = error.response.data.error.message
|
throw new Error(error.response?.data?.error?.message)
|
||||||
throw new Error(errorMessage)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
TwitterProvider({
|
Twitter({
|
||||||
clientId: process.env.NEXT_PUBLIC_TWITTER_API_KEY,
|
clientId: process.env.NEXT_PUBLIC_TWITTER_API_KEY,
|
||||||
clientSecret: process.env.NEXT_PUBLIC_TWITTER_API_KEY_SECRET
|
clientSecret: process.env.NEXT_PUBLIC_TWITTER_API_KEY_SECRET
|
||||||
}),
|
}),
|
||||||
GoogleProvider({
|
Google({
|
||||||
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
||||||
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET
|
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET
|
||||||
}),
|
}),
|
||||||
GitHubProvider({
|
GitHub({
|
||||||
clientId: process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID,
|
clientId: process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID,
|
||||||
clientSecret: process.env.NEXT_PUBLIC_GITHUB_CLIENT_SECRET
|
clientSecret: process.env.NEXT_PUBLIC_GITHUB_CLIENT_SECRET
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
secret: process.env.NEXT_PUBLIC_JWT_SECRET,
|
|
||||||
session: {
|
session: {
|
||||||
strategy: 'jwt'
|
strategy: "jwt"
|
||||||
},
|
},
|
||||||
|
secret: process.env.NEXT_PUBLIC_JWT_SECRET,
|
||||||
callbacks: {
|
callbacks: {
|
||||||
async jwt({token, user, account}) {
|
async jwt({ token, user, account }) {
|
||||||
if (user) {
|
if (user && account) {
|
||||||
let url = `${process.env.NEXT_PUBLIC_API_URL}/auth/${account.provider}/callback?access_token=${account?.accessToken}`
|
let url = `${process.env.NEXT_PUBLIC_API_URL}/auth/${account.provider}/callback`
|
||||||
|
|
||||||
if (account.provider === 'twitter') {
|
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}`
|
url += `?access_token=${account.oauth_token}&access_secret=${account.oauth_token_secret}`
|
||||||
}
|
} else {
|
||||||
|
url += `?access_token=${account.access_token}`
|
||||||
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 response = await fetch(url)
|
||||||
|
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
|
|
||||||
token.id = data.id || user.id
|
token.id = data.id || user.id
|
||||||
token.jwt = data.jwt || user.jwt
|
token.jwt = data.jwt || user.jwt
|
||||||
token.user = data.user || user.user
|
token.user = data.user || user.user
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve(token)
|
return token
|
||||||
},
|
},
|
||||||
async session({session, token}) {
|
async session({ session, token }) {
|
||||||
session.jwt = token.jwt
|
session.jwt = token.jwt
|
||||||
session.user = token.user
|
session.user = token.user
|
||||||
return Promise.resolve(session)
|
return session
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
pages: {
|
pages: {
|
||||||
signIn: '/pwopose',
|
signIn: "/pwopose",
|
||||||
error: '/pwopose'
|
error: "/pwopose"
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
const handler = (request, response) =>
|
|
||||||
NextAuth(request, response, options)
|
|
||||||
|
|
||||||
export {handler as GET, handler as POST}
|
export const { GET, POST } = handlers
|
||||||
Reference in New Issue
Block a user