Next.js Discord

Discord Forum

Bug, session not refreshing on log out

Unanswered
Russian Blue posted this in #help-forum
Open in Discord
Russian BlueOP
I have github provider configured, log in is smooth, but when I click log out, nothing is changed until I either reload or simply alt tab to another window or even if i go to another tab and back then the changes are reflected

3 Replies

Russian BlueOP
anyone please??
import { PrismaAdapter } from "@auth/prisma-adapter";
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import { getSession } from "next-auth/react";
import { prisma } from "./lib/prisma";
import { authConfig } from "./auth.config";

export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
GitHub({
authorization: {
params: {
scope: "read:user user:email repo read:org", // Request read access to user info and repositories
},
},
}),
],

callbacks: {
...authConfig.callbacks,
// Store the GitHub access token in the token object
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token; // Store access token
}
return token;
},
// Add access token to the session object
async session({ session, token }) {
session.accessToken = token.accessToken; // Add access token to session
if (session.accessToken) {
const res = await fetch("https://api.github.com/user", {
headers: {
Authorization: token ${session.accessToken},
Accept: "application/vnd.github.v3+json",
"Cache-Control": "no-cache",
},
});

if (res.ok) {
const userData = await res.json();
session.user.github = userData;
}
}

return session;
},
},
// adapter: PrismaAdapter(prisma),
});
import type { NextAuthConfig, Session } from "next-auth";

import {
DEFAULT_LOGIN_REDIRECT,
publicRoutes,
authRoutes,
apiAuthPrefix,
} from "./routes";

export const authConfig = {
providers: [],
callbacks: {
async authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;

const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);

if (isApiAuthRoute) {
return true; //automatic allowing for anyone trying to signin from any provider, we are explicitly stating it here to ensure we aren't rejecting this route somewhere below
}

// if (isPublicRoute && isLoggedIn) {
// // return false;
// return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl)); //automatically redirect to signinPage
// }

if (!isLoggedIn && !isPublicRoute) {
return Response.redirect(new URL("/", nextUrl));
}

return true;
},
},
} satisfies NextAuthConfig;