Skip to content

How to auto redirect to the provider login screen #4078

Answered by meleksomai
meleksomai asked this question in Help
Discussion options

You must be logged in to vote

Thanks @balazsorban44 . The solution I came across.

  1. Add a custom signin page under pages, for instance /pages/auth/signin.tsx:
import { signIn, useSession } from "next-auth/react";
import { useEffect } from "react";
import { useRouter } from "next/router";

export default function Signin() {
  const router = useRouter();
  const { status } = useSession();

  useEffect(() => {
    if (status === "unauthenticated") {
      console.log("No JWT");
      console.log(status);
      void signIn("okta");
    } else if (status === "authenticated") {
      void router.push("/");
    }
  }, [status]);

  return <div></div>;
}
  1. Update the _middleware.tsx to redirect to the custom sign-in page.
import

Replies: 13 comments 57 replies

Comment options

You must be logged in to vote
3 replies
@meleksomai
Comment options

@balazsorban44
Comment options

@fredericoo
Comment options

Comment options

You must be logged in to vote
22 replies
@jmiller-rise8
Comment options

@CarlosMoralesR
Comment options

@cdeutsch
Comment options

@federicocappellotto97
Comment options

@Mario-Accr
Comment options

Answer selected by meleksomai
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@GautheyValentin
Comment options

Comment options

You must be logged in to vote
2 replies
@dylanbr0wn
Comment options

@justinlevi
Comment options

Comment options

You must be logged in to vote
1 reply
@jmiller-rise8
Comment options

Comment options

You must be logged in to vote
19 replies
@benjamin-musil
Comment options

@AliakseiPaseishviliSyntheticabio
@stern-shawn
Comment options

@stern-shawn
Comment options

Comment options

You must be logged in to vote
2 replies
@ys-sherzad
Comment options

@pablote
Comment options

Comment options

You must be logged in to vote
3 replies
@henricook
Comment options

@nikolaphilipp
Comment options

@hakan-akgul
Comment options

Comment options

You must be logged in to vote
1 reply
@minyiediazvelir
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@jmiller-rise8
Comment options

@Firgrep
Comment options

Comment options

You must be logged in to vote
1 reply
@liamlows
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet