i have some question about next-auth custom login #10871
Unanswered
maririn312
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
-
import NextAuth, { NextAuthOptions } from 'next-auth'
import { jwtDecode } from 'jwt-decode'
import CredentialsProvider from 'next-auth/providers/credentials'
import KeycloakProvider from 'next-auth/providers/keycloak'
import { getUserInfo } from '@/service/service_urls'
async function refreshAccessToken(token: any) {
const resp = await fetch(
${process.env.REFRESH_TOKEN_URL}
, {headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
client_id:
${process.env.KEYCLOAK_CLIENT_ID}
,client_secret:
${process.env.KEYCLOAK_CLIENT_SECRET}
,grant_type: 'refresh_token',
refresh_token: token.refresh_token,
}),
method: 'POST',
})
const refreshToken = await resp.json()
if (!resp.ok) throw refreshToken
return {
...token,
access_token: refreshToken.access_token,
id_token: refreshToken.id_token,
expires_at: Number(token.expires_at) + Number(refreshToken.expires_in),
refresh_token: refreshToken.refresh_token,
}
}
const authOptions: NextAuthOptions = {
providers: [
KeycloakProvider({
clientId:
${process.env.KEYCLOAK_CLIENT_ID}
,clientSecret:
${process.env.KEYCLOAK_CLIENT_SECRET}
,issuer:
${process.env.KEYCLOAK_AUTH_ISSUER}
,}),
CredentialsProvider({
name: 'Credentials',
credentials: {
username: { label: 'Username', type: 'text' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials: any) {
const resp = await fetch(
${process.env.NODE_ENV} ${getUserInfo}
, {headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
username: credentials.username,
password: credentials.password,
}),
method: 'POST',
})
const user = await resp.json()
if (!resp.ok) throw user
return user
},
}),
],
callbacks: {
async jwt({ token, account }: any) {
if (account) {
token.id_token = account.id_token
token.refresh_token = account.refresh_token
token.expires_at = account.expires_at
token.access_token = account.access_token
},
secret:
${process.env.NEXTAUTH_SECRET}
,pages: {
signIn: '/login',
},
}
const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
this is my next-auth main route and this is my middleware import { withAuth } from 'next-auth/middleware'
export default withAuth(function middleware(request) {
const roles = request.nextauth.token?.role
const currentPath = request.nextUrl.pathname
})
i got redirect error in this code some please help me
Beta Was this translation helpful? Give feedback.
All reactions