You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
currently,The implementation of next-auth Credentials authentication flow in Chapter 15 of the official tutorial works, but it has some flaws that cause several confusion for me and also some other learners. such as
Based on my observations, the following issues exist with the tutorial's implementation (which can be reproduced in the official live demo):
When navigating from the homepage to the login page and successfully logging in, the user is redirected to the /dashboard page, but the browser's URL does not change and remains as /#. Additionally, the Sign Out button does not work until the page is refreshed, after which the URL displays correctly as .../dashboard and the Sign Out button works as expected.
issues_point_1.mp4
Following the steps in point 1, when clicking the Sign Out button on any page (e.g., /dashboard/customers), the page content updates to the /# page, but the browser's URL does not change. After refreshing the page, the browser URL updates to .../#?callbackUrl=https%3A%2F%2Fnext-learn-dashboard.vercel.sh%2Fdashboard%2Fcustomers.
issues_point_2.mp4
Following the steps in point 2, if the browser URL contains a callbackUrl=... query parameter (e.g., when accessing https://next-learn-dashboard.vercel.sh/dashboard/customers while not logged in, which redirects to the /# page with the callbackUrl), the expected behavior is to be redirected to the callbackUrl page after a successful login. However, after logging in, the user is still redirected to the /dashboard page, and the same issue from point 1 occurs: the browser's URL does not update and remains as .../#?callbackUrl=https%3A%2F%2Fnext-learn-dashboard.vercel.sh%2Fdashboard%2Fcustomers, and the Sign Out button does not work. Refreshing the page resolves the issue.
To keep the same logic, the login form should include the redirectTo field, defaulting to /dashboard. If the URL contains a callbackUrl, the field should be set to that value.
modify app/#/page.tsx:
// ...exportdefaultfunctionLoginPage({
searchParams,}: {searchParams?: {callbackUrl?: string;};}){constredirectTo=searchParams?.callbackUrl||'/dashboard';// <============== get `redirectTo` from search param `callbackUrl`return(<mainclassName="flex items-center justify-center md:h-screen">{/* ... */}<LoginFormredirectTo={redirectTo}/>{/* <============== pass `redirectTo` to LoginForm */}</div></main>);}
In the sidenav.tsx, the redirectTo parameter should be specified. Typically, when signing out, the user should be redirected to the homepage or the login page.
currently,The implementation of
next-auth
Credentials authentication flow in Chapter 15 of the official tutorial works, but it has some flaws that cause several confusion for me and also some other learners. such asExisting Issues
Based on my observations, the following issues exist with the tutorial's implementation (which can be reproduced in the official live demo):
/dashboard
page, but the browser's URL does not change and remains as/#
. Additionally, theSign Out
button does not work until the page is refreshed, after which the URL displays correctly as.../dashboard
and theSign Out
button works as expected.issues_point_1.mp4
/dashboard/customers
), the page content updates to the/#
page, but the browser's URL does not change. After refreshing the page, the browser URL updates to.../#?callbackUrl=https%3A%2F%2Fnext-learn-dashboard.vercel.sh%2Fdashboard%2Fcustomers
.issues_point_2.mp4
callbackUrl=...
query parameter (e.g., when accessinghttps://next-learn-dashboard.vercel.sh/dashboard/customers
while not logged in, which redirects to the/#
page with thecallbackUrl
), the expected behavior is to be redirected to thecallbackUrl
page after a successful login. However, after logging in, the user is still redirected to the/dashboard
page, and the same issue from point 1 occurs: the browser's URL does not update and remains as.../#?callbackUrl=https%3A%2F%2Fnext-learn-dashboard.vercel.sh%2Fdashboard%2Fcustomers
, and theSign Out
button does not work. Refreshing the page resolves the issue.issues_point_3.mp4
Suggested Improvements
During SignIn
As mentioned in this comment Redirect URL not updating correctly in browser after successful redirect, pass the
redirectTo
parameter to thesignIn()
function, and ensure it aligns with the logic inauth.config.ts -> authorized
.The logic in
auth.config.ts -> authorized
:To keep the same logic, the login form should include the
redirectTo
field, defaulting to/dashboard
. If the URL contains acallbackUrl
, the field should be set to that value.modify
app/#/page.tsx
:modify
app/ui/#-form.tsx
:During SignOut
In the
sidenav.tsx
, theredirectTo
parameter should be specified. Typically, when signing out, the user should be redirected to the homepage or the login page.modify
app/ui/dashboard/sidenav.tsx
:Verification After the Fixes
minimal reproducible repo: https://github.com/ks4na/nextjs-dashboard/tree/chapter15-improvements
vercel.app preview link
Additional Information
next-auth documentation
For more details on
signIn()
,signOut()
, andredirectTo
, see the documentation: signIn(), signOut().The text was updated successfully, but these errors were encountered: