authts/react-oidc-context

Force sign out before sign in

ValGab opened this issue · 2 comments

Hello,

In a React application, I have a form that allows me to check if an email is already taken. When taken, I ask the user to log in with the email. I use this :

<button
        className="action-btn"
        onClick={() => {
          auth.signinRedirect({
            redirect_uri:
              window.location.origin + `/path`),
            login_hint: email,
          });
        }}
      >
        Me connecter
</button>

But sometimes, the user may already be logged in to the app with an account that does not match the email being checked. If the user is logged in, the login form is invisible and the redirection takes place directly. I would like to be able to disconnect it and send it directly back to the login with the pre-populated email, all invisibly.

Is it possible ?

const auth = useAuth(); provides all you need. You can check for auth.isAuthenticated and sign-out if required. You can check if the new email matches the existing auth.user.email == "..."

Yes, thank you @pamapa, I found this solution with useAuth()

onClick={() => {
          if (auth.isAuthenticated) {
            auth.removeUser();
            auth.signoutRedirect({
              id_token_hint: auth.user?.id_token,
              post_logout_redirect_uri:
                window.location.origin + "/path"),
            });
          } else {
            auth.signinRedirect({
              redirect_uri:
                window.location.origin + "/path"),
              login_hint: email,
            });
          }
        }}

"/path" is a Protected Route, so it demands to login directly after sign-out.