wanna_coder101
wanna_coder101

Reputation: 206

How to force new token/session in next-auth server-side after overriding default signOut method?

I'm using the credentials provider of Next-Auth, where the sign in would set the user into the token, then into the session. The set cookies is also applied.

In order to signOut, I overrode the default signOut Method

[...nextauth].ts

events: {
    async signOut({ token, session }) {
        // Delete auth cookie on signout so it doesn't persist past log out
        res.setHeader("Set-Cookie", "");
    }
},

However, now my session doesn't refresh or destroy itself. So the cilentside still thinks I'm logged in even after calling the sign out method.

How can I force refresh the token/session in the serverside? Would it just be setting token/session = {} in signOut? Is there some default method that can do that?

Upvotes: 1

Views: 10639

Answers (2)

christo8989
christo8989

Reputation: 6826

Can you use the signIn callback instead?

export const authOptions: NextAuthOptions = {
  callbacks: {
    signIn({ user }) {
      return user?.email === '[email protected]';
    },
  },
};

Here's their documentation if you want to read further. next-auth

You can also return a string for the signIn callback, which will act as a redirect.

Upvotes: 0

wanna_coder101
wanna_coder101

Reputation: 206

It was like my initial thoughts, just set token/session = {}, then that would delete the session accordingly.

async signOut({ token, session }) {
    // Delete auth cookie on signout so it doesn't persist past log out
    res.setHeader("Set-Cookie", "");

    // Set token/session to {}, that would update the cilentside token/session as well
    token = {};
    session = {};
}

Upvotes: 1

Related Questions