Authentication
Tutorials
How to Post Login Redirect

How to Post Login Redirect

The post-login redirect works by passing a URL as a query parameter to the signIn or signUp method. Once the user is logged in successfully, they will be redirected to that URL.

The use case for this is when we browse to a specific application page and are automatically logged out due to inactivity. When we log back in, the application page is not in its previous state.

postLoginRedirect

The postLoginRedirect is a state parameter option for the signIn and signUp methods in the @roq/nextjs package. With this option set, any login will redirect to the path URL.

For example, In the generated application, to redirect the URL to /discussion-boards after user login, you can pass the path URL into the signIn method:

import { signIn, signUp, requireNextAuth } from '@roq/nextjs';
 
//...
signIn(type, {
        postLoginRedirect: "/discussion-boards",
    },
})

The ROQ login component will include state parameters in its URL:

http://localhost:3000/api/auth/login?postLoginRedirect=%2Fdiscussion-boards&variantKey=store-owner

The URL above tell us that this login form, after successful authentication, will redirect the user to the discussion boards page.

Post login redirect