简体   繁体   中英

NEXT-AUTH callback redirect only in signin

I want to use redirect only when signIn(). I have tried several methods.

  1. Use callback redirect in[...nextauth].tsx

This method make my app will redirect on both sign in and sign out. Not for my situation.

 callbacks: {
    async redirect({ url, baseUrl }) {
      return "/account/loadingregister";
    },
  },
  1. Inline use redirect in the component. However, this only works for signOut callback, I need this work in sign in callback.
<button onClick={() => signIn({ callbackUrl: "http://localhost:3000/abc" })}>
     Sign In
</button>

<button onClick={() => signOut({ callbackUrl: "http://localhost:3000/foo" })}>
     Sign Out
</button>
  1. Both are used. The [...nextauth].tsx will be the first priority. And inline redirect will be ignored.

So, how can I redirect only in sign in function?

import { signOut } from "next-auth/react";

const logoutHandler = () => {
    signOut();
  };

<button onClick={logoutHandler}>
     Sign Out
</button>

Finally, I found the answer. Need to use async function to redirect to other page after signIn.

const handleSignIn = async () => {
    await signIn("google", {
      callbackUrl: "/",
    });
  };

//component:

<button onClick={() => handleSignIn()}>Sign In</button>


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM