If the user visits /protected-page without a valid session, it will redirect the user to the
login page.
Note: Server Components are not aware of the req or the url of the page. So if you want the user to return to the
page after login, you must specify the returnTo option.
You can specify a function to returnTo that accepts the params (An object containing the dynamic
route parameters) and searchParams (An object containing the search parameters of the current URL)
argument from the page, to preserve dynamic routes and search params.
Wrap your Server Component with this method to make sure the user is authenticated before visiting the page.
If the user visits
/protected-page
without a valid session, it will redirect the user to the login page.Note: Server Components are not aware of the req or the url of the page. So if you want the user to return to the page after login, you must specify the
returnTo
option.You can specify a function to
returnTo
that accepts theparams
(An object containing the dynamic route parameters) andsearchParams
(An object containing the search parameters of the current URL) argument from the page, to preserve dynamic routes and search params.