OAUTH_CALLBACK_ERROR next js #12255
Unanswered
VruyrSaribekyan
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Working in localhost but not working in dev production url dev.domain.com
import NextAuth, { NextAuthOptions, Profile } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import GoogleProvider from 'next-auth/providers/google'
import AppleProvider from 'next-auth/providers/apple'
import VKProvider from 'next-auth/providers/vk'
import { decode } from 'jsonwebtoken'
import { authApi } from '@/api/auth'
import { ELoginTypes } from '@/models/auth'
interface TokenPayload {
_id: string;
email: string;
role: string;
}
const apiVersion = '5.131'
export const authOptions: NextAuthOptions = {
providers: [
CredentialsProvider({
name: 'credentials',
credentials: {
email: { label: 'Email', type: 'email', placeholder: '[email protected]' },
password: { label: 'Password', type: 'password' }
},
async authorize(credentials) {
try {
if (!credentials?.email || !credentials?.password) {
throw new Error('Email and password are required.')
}
}
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
authorization: {
params: {},
},
checks: ["none"],
}),
AppleProvider({
clientId: process.env.APPLE_CLIENT_ID!,
clientSecret: process.env.APPLE_CLIENT_SECRET!,
authorization: {
params: {
scope: 'name email',
response_mode: 'form_post'
}
}
}),
VKProvider({
clientId: process.env.VK_CLIENT_ID!,
clientSecret: process.env.VK_CLIENT_SECRET!,
authorization: {
params: {
scope: 'email',
response_type: 'code',
v: apiVersion
}
},
accessTokenUrl:
https://oauth.vk.com/access_token?v=${apiVersion}
,requestTokenUrl:
https://oauth.vk.com/access_token?v=${apiVersion}
,profile(profile) {
return {
id: profile.id.toString(),
email: profile.email,
firstName: profile.first_name,
lastName: profile.last_name,
role: ELoginTypes.USER
}
}
})
],
session: {
strategy: 'jwt',
maxAge: 7 * 24 * 60 * 60 // 7 дней
},
pages: {
signIn: '/'
},
callbacks: {
async redirect({ url, baseUrl }) {
const redirectUrl = url.startsWith('/') ? new URL(url, baseUrl).toString() : url;
console.log([next-auth] Redirecting to "${redirectUrl}" (resolved from url "${url}" and baseUrl "${baseUrl}"));
return redirectUrl;
},
async jwt({ token, user, account, profile }) {
let access_token = ''
if (account?.provider === 'google') {
const response = await authApi.googleSignUp({
access_token: account.access_token as string,
auth_type: ELoginTypes.USER
})
access_token = response.data.access_token
}
if (account?.provider === 'apple') {
const authorizationCode = account?.code as string
}
if (account?.provider === 'vk') {
const vkProfile = profile as Profile & { id: string }
}
if (user) {
const accessTokenExpires = Date.now() + 15 * 60 * 1000 // 15 минут
const accessToken = user.accessToken || access_token
return {
...token,
accessToken,
refreshToken: user.refreshToken,
accessTokenExpires,
user: {
id: user.id,
email: user.email,
role: user.role
}
}
}
if (typeof token.accessTokenExpires === 'number' && Date.now() < token.accessTokenExpires) {
return token
}
return {
...token,
error: 'TokenExpiredError'
}
},
async session({ session, token }) {
session.user = token.user as any
session.accessToken = token.accessToken
session.refreshToken = token.refreshToken
session.error = token.error
if (token.email) {
session.user.email = token.email
}
return session
}
},
secret: process.env.NEXTAUTH_SECRET!
}
export default NextAuth(authOptions)
на localhost:3000 работает но на дев окружении не работает
NEXTAUTH_URL=https://dev.mydomen.com/
и все настройки правильно сделаны
помогите пожалуйста
"next": "^14.1.4",
"next-auth": "^4.24.8",
Beta Was this translation helpful? Give feedback.
All reactions