Auth redirection
#
Change redirection path post login- ReactJS
- Angular
- Vue
Important
supertokens-auth-react
SDK and will inject the React components to show the UI. Therefore, the code snippet below refers to the supertokens-auth-react
SDK.By default, the user is redirected the the /
route on your website post login. To change this, you can use the getRedirectionURL
function on the frontend as shown below:
import SuperTokens from "supertokens-auth-react";import EmailPassword from "supertokens-auth-react/recipe/emailpassword";
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "...", }, recipeList: [ EmailPassword.init({ getRedirectionURL: async (context) => { if (context.action === "SUCCESS") { if (context.redirectToPath !== undefined) { // we are navigating back to where the user was before they authenticated return context.redirectToPath; } return "/dashboard"; } return undefined; } }), ]});
The user will be redirected to the provided URL on:
- Successful sign up.
- Successful sign in.
- Successful email verification post sign up.
- If the user is already logged in.
If you want to redirect the user to a different domain, then you can do so by first redirecting them to a specific path using the function above, which further redirects them to the final domain.
info
Please refer to this page to learn more about the getRedirectionURL
hook.
Important
supertokens-auth-react
SDK and will inject the React components to show the UI. Therefore, the code snippet below refers to the supertokens-auth-react
SDK.By default, the user is redirected the the /
route on your website post login. To change this, you can use the getRedirectionURL
function on the frontend as shown below:
import SuperTokens from "supertokens-auth-react";import EmailPassword from "supertokens-auth-react/recipe/emailpassword";
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "...", }, recipeList: [ EmailPassword.init({ getRedirectionURL: async (context) => { if (context.action === "SUCCESS") { if (context.redirectToPath !== undefined) { // we are navigating back to where the user was before they authenticated return context.redirectToPath; } return "/dashboard"; } return undefined; } }), ]});
The user will be redirected to the provided URL on:
- Successful sign up.
- Successful sign in.
- Successful email verification post sign up.
- If the user is already logged in.
If you want to redirect the user to a different domain, then you can do so by first redirecting them to a specific path using the function above, which further redirects them to the final domain.
info
Please refer to this page to learn more about the getRedirectionURL
hook.
By default, the user is redirected the the /
route on your website post login. To change this, you can use the getRedirectionURL
function on the frontend as shown below:
import SuperTokens from "supertokens-auth-react";import EmailPassword from "supertokens-auth-react/recipe/emailpassword";
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "...", }, recipeList: [ EmailPassword.init({ getRedirectionURL: async (context) => { if (context.action === "SUCCESS") { if (context.redirectToPath !== undefined) { // we are navigating back to where the user was before they authenticated return context.redirectToPath; } return "/dashboard"; } return undefined; } }), ]});
The user will be redirected to the provided URL on:
- Successful sign up.
- Successful sign in.
- Successful email verification post sign up.
- If the user is already logged in.
If you want to redirect the user to a different domain, then you can do so by first redirecting them to a specific path using the function above, which further redirects them to the final domain.
info
Please refer to this page to learn more about the getRedirectionURL
hook.
#
Redirect user to the login page- ReactJS
- Angular
- Vue
To navigate the user to the login page, you can set window.location.href
to the value of websiteBasePath
config (/auth
by default).
To show the sign in page on redirect, you want to add the query param ?show=signin
to the URL. To show the sign up page on redirect, you want to add ?show=signup
instead.
To make the user navigate back to their current page, you should add the query param ?redirectToPath=/somepath
.
For example if the user is currently on the /dashboard
page and is not logged in, you can navigate them to the sign in page like this:
window.location.href = "/auth?show=signin&redirectToPath=/dashboard"
This will show the sign in page and post login, will redirect the user back to /dashboard
.
Use the redirectToAuth({show?: "signin" | "signup", redirectBack?: boolean}?)
function to redirect the user to the login screen. For example, you may want to call this function when the user clicks on the login button.
import React from "react";import { redirectToAuth } from "supertokens-auth-react";
function NavBar () { async function onLogin () { redirectToAuth(); } return ( <ul> <li>Home</li> <li onClick={onLogin}>Login</li>
</ul> )}
- Call
redirectToAuth({show: "signin"})
to take them to the sign in screen - Call
redirectToAuth({show: "signup"})
to take them to the sign up screen - If you do not want the user to be redirected to the current page post sign in, use
redirectToAuth({redirectBack: false})
To navigate the user to the login page, you can set window.location.href
to the value of websiteBasePath
config (/auth
by default).
To show the sign in page on redirect, you want to add the query param ?show=signin
to the URL. To show the sign up page on redirect, you want to add ?show=signup
instead.
To make the user navigate back to their current page, you should add the query param ?redirectToPath=/somepath
.
For example if the user is currently on the /dashboard
page and is not logged in, you can navigate them to the sign in page like this:
window.location.href = "/auth?show=signin&redirectToPath=/dashboard"
This will show the sign in page and post login, will redirect the user back to /dashboard
.
#
Showing sign up by defaultThe login screen shows the sign in UI by default, to change that, you can set the folowing config:
- ReactJS
- Angular
- Vue
Important
supertokens-auth-react
SDK and will inject the React components to show the UI. Therefore, the code snippet below refers to the supertokens-auth-react
SDK.import SuperTokens from "supertokens-auth-react";import EmailPassword from 'supertokens-auth-react/recipe/emailpassword';import Session from 'supertokens-auth-react/recipe/session';
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, recipeList: [ EmailPassword.init({ signInAndUpFeature: { defaultToSignUp: true } }), Session.init({ /* ... */ }) ]});
Important
supertokens-auth-react
SDK and will inject the React components to show the UI. Therefore, the code snippet below refers to the supertokens-auth-react
SDK.import SuperTokens from "supertokens-auth-react";import EmailPassword from 'supertokens-auth-react/recipe/emailpassword';import Session from 'supertokens-auth-react/recipe/session';
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, recipeList: [ EmailPassword.init({ signInAndUpFeature: { defaultToSignUp: true } }), Session.init({ /* ... */ }) ]});
import SuperTokens from "supertokens-auth-react";import EmailPassword from 'supertokens-auth-react/recipe/emailpassword';import Session from 'supertokens-auth-react/recipe/session';
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, recipeList: [ EmailPassword.init({ signInAndUpFeature: { defaultToSignUp: true } }), Session.init({ /* ... */ }) ]});