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