Files
pawol.nu/app/api/auth/[...nextauth]/route.js
T

82 lines
2.2 KiB
JavaScript
Raw Normal View History

2026-04-16 13:54:15 +04:00
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"
2021-05-22 23:36:39 +02:00
2026-04-16 13:54:15 +04:00
export const { handlers, auth } = NextAuth({
2021-05-22 23:36:39 +02:00
providers: [
2026-04-16 13:54:15 +04:00
Credentials({
2021-05-22 23:36:39 +02:00
credentials: {
2026-04-16 13:54:15 +04:00
username: {},
password: {}
2021-05-22 23:36:39 +02:00
},
2026-04-16 13:54:15 +04:00
async authorize(credentials) {
2021-05-22 23:36:39 +02:00
try {
2026-04-16 13:54:15 +04:00
const user = await axios.post(
`${process.env.NEXT_PUBLIC_API_URL}/auth/local`,
{
identifier: credentials.username,
password: credentials.password
}
)
2021-05-22 23:36:39 +02:00
2026-04-16 13:54:15 +04:00
return user.data || null
2021-05-22 23:36:39 +02:00
} catch (error) {
2026-04-16 13:54:15 +04:00
throw new Error(error.response?.data?.error?.message)
2021-05-22 23:36:39 +02:00
}
}
2022-10-23 23:24:43 +04:00
}),
2026-04-16 13:54:15 +04:00
Twitter({
2022-10-23 23:24:43 +04:00
clientId: process.env.NEXT_PUBLIC_TWITTER_API_KEY,
clientSecret: process.env.NEXT_PUBLIC_TWITTER_API_KEY_SECRET
2022-10-26 00:33:27 +04:00
}),
2026-04-16 13:54:15 +04:00
Google({
2022-10-26 00:33:27 +04:00
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET
2022-10-28 08:28:35 +04:00
}),
2026-04-16 13:54:15 +04:00
GitHub({
2022-10-28 08:28:35 +04:00
clientId: process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_GITHUB_CLIENT_SECRET
2021-05-22 23:36:39 +02:00
})
],
session: {
2026-04-21 19:32:21 +04:00
strategy: "jwt",
trustHost: true
2021-05-22 23:36:39 +02:00
},
2026-04-16 13:54:15 +04:00
secret: process.env.NEXT_PUBLIC_JWT_SECRET,
2022-03-18 08:11:04 +04:00
callbacks: {
2026-04-16 13:54:15 +04:00
async jwt({ token, user, account }) {
if (user && account) {
let url = `${process.env.NEXT_PUBLIC_API_URL}/auth/${account.provider}/callback`
2022-10-23 23:24:43 +04:00
2026-04-16 13:54:15 +04:00
if (account.provider === "twitter") {
url += `?access_token=${account.oauth_token}&access_secret=${account.oauth_token_secret}`
} else {
url += `?access_token=${account.access_token}`
2022-10-26 00:33:27 +04:00
}
2022-10-23 23:24:43 +04:00
const response = await fetch(url)
2022-03-18 08:11:04 +04:00
const data = await response.json()
2026-04-16 13:54:15 +04:00
2022-03-18 08:11:04 +04:00
token.id = data.id || user.id
token.jwt = data.jwt || user.jwt
token.user = data.user || user.user
}
2026-04-16 13:54:15 +04:00
return token
2022-03-18 08:11:04 +04:00
},
2026-04-16 13:54:15 +04:00
async session({ session, token }) {
2022-03-18 08:11:04 +04:00
session.jwt = token.jwt
session.user = token.user
2026-04-16 13:54:15 +04:00
return session
2022-03-18 08:11:04 +04:00
}
},
2021-05-22 23:36:39 +02:00
pages: {
2026-04-16 13:54:15 +04:00
signIn: "/pwopose",
error: "/pwopose"
2021-05-22 23:36:39 +02:00
}
2026-04-16 13:54:15 +04:00
})
2023-07-22 13:26:02 +04:00
2026-04-16 13:54:15 +04:00
export const { GET, POST } = handlers