Session token value is getting refreshed every time on viewing the application tab (i,e localhost:3000) even without any interaction with the application. #12040
Unanswered
Prajwal-Mogaveera
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
Uh oh!
There was an error while loading. Please reload this page.
-
I have implemented credential login using NextAuth.js. After login, whenever I switch the tab and comeback to the application tab, the session token is getting refreshed every time. Is this behavior expected?. I have pasted my implementation code below. Can anyone please help me.
/api/auth/[...nextauth] /options.ts:
import { NextAuthOptions } from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import connectToMongoose from "@/DatabaseFunctions/db"
import Customer from "@/Models/Customer"
const bcryptjs = require("bcryptjs")
export const authOptions: NextAuthOptions = {
providers: [
CredentialsProvider({
id: "credentials",
name: "Credentials",
credentials: {
emailOrPhone: { label: "emailOrPhone", type: "text " },
password: { label: "Password", type: "password" }
},
async authorize(credentials: any): Promise {
await connectToMongoose()
try {
const { emailOrPhone, password } = credentials
// Determine if it's an email or phone based on the input pattern
const isEmail = emailOrPhone.includes("@")
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token._id = user._id?.toString()
}
console.log("JWT callback - token:")
},
pages: { signIn: "/login" },
session: {
strategy: "jwt"
},
secret: process.env.SEC_KEY
}
/api/auth/[...nextauth] /route.ts:
import NextAuth from "next-auth"
import { authOptions } from "./options"
const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
middleware.ts:
export { default } from "next-auth/middleware"
import { NextRequest, NextResponse } from "next/server"
import { getToken } from "next-auth/jwt"
export async function middleware(request: NextRequest) {
const token = await getToken({ req: request, secret: process.env.SEC_KEY })
const url = request.nextUrl
if ((token && url.pathname.startsWith("/login")) || url.pathname.startsWith("/signup")) {
return NextResponse.redirect(new URL("/", request.url))
}
}
export const config = {
matcher: ["/login"]
}
Beta Was this translation helpful? Give feedback.
All reactions