Issue with sign in using auth-next with next 14
Unanswered
Northeast Congo Lion posted this in #help-forum
Northeast Congo LionOP
Hello, everyone. Could you help. I have been learning next.js less then 1 month. lately next 14 have been released. I try recrate the dashboard application which described in 16th chapters. But in the and (at 15 chapter) i faced with issue. I enter correct user data but instead of redirecting on dashboard the app is redirecting me on login.
I think the issue in auth.config
import type { NextAuthConfig } from "next-auth";
export const authConfig = {
providers: [],
pages: {
signIn: "/login",
},
callbacks: {
authorized({ auth, request: { nextUrl } }) {
console.log("user", auth?.user);
const isLoggedIn = !!auth?.user;
console.log('is logged in:',isLoggedIn);
const isOnDashboard = nextUrl.pathname.startsWith("/dashboard");
console.log('is on dashboard:',isOnDashboard);
if (isOnDashboard) {
if (isLoggedIn) return true;
return false; // Redirect unauthenticated users to login page
} else if (isLoggedIn) {
return Response.redirect(new URL("/dashboard", nextUrl));
}
return true;
},
},
} satisfies NextAuthConfig;
But i'm not experienced in it. Please help me to finish this app
I think the issue in auth.config
import type { NextAuthConfig } from "next-auth";
export const authConfig = {
providers: [],
pages: {
signIn: "/login",
},
callbacks: {
authorized({ auth, request: { nextUrl } }) {
console.log("user", auth?.user);
const isLoggedIn = !!auth?.user;
console.log('is logged in:',isLoggedIn);
const isOnDashboard = nextUrl.pathname.startsWith("/dashboard");
console.log('is on dashboard:',isOnDashboard);
if (isOnDashboard) {
if (isLoggedIn) return true;
return false; // Redirect unauthenticated users to login page
} else if (isLoggedIn) {
return Response.redirect(new URL("/dashboard", nextUrl));
}
return true;
},
},
} satisfies NextAuthConfig;
But i'm not experienced in it. Please help me to finish this app
26 Replies
Blue horntail woodwasp
What version of Next-auth you install?
please check on package.json
Northeast Congo LionOP
"next-auth": "^5.0.0-beta.3",
i just followed steps in this tutorial https://nextjs.org/learn/dashboard-app
Blue horntail woodwasp
maybe you want to check my repo. I have completed the lesson. On every chapter I do commit so you can look at the commit log
I remember I also struggled on chapter 15. but I forget what's wrong 😄
Northeast Congo LionOP
thanks. i'll see it and compare with mine
Northeast Congo LionOP
I just cloned your repo locally and copied my database settings from env. I try to loggin and i faced with the same issue. After entering data i've been redirected to the login, not to dashboard.
here is env data
American Crocodile
Hey, I had the same problem recently and fixed it by deleting my localhost cookies. Maybe it works for you too
Northeast Congo LionOP
ufortunatelly, it doesn't work for me. I cleared them many times. But i can't pass to dashboard.
Blue horntail woodwasp
maybe try to console.log first after checking the data from database? is it successfully match?
Northeast Congo LionOP
Of course , before i asked to help i had done this. I had tried cosole.log and discovered that data wasn't gone
Blue horntail woodwasp
can I look at your
authenticate actions?maybe on
/lib/actions.tsplease try to
console.log formData. is there any data?Northeast Congo LionOP
Yes, of course.
@Northeast Congo Lion here is env data
Blue horntail woodwasp
have you tried to wrap your
AUTH_SECRET to string like this ?also try to specified
NEXTAUTH_URL with your local dev urlin my local
.env I did thisNortheast Congo LionOP
Thanks. It works.
But I don't understand why it needed. Where do this parameters use? Too many abstractions, I think😫
@Northeast Congo Lion But I don't understand why it needed. Where do this parameters use? Too many abstractions, I think😫
Blue horntail woodwasp
I agree, they try to simplified the flow and hide the complexity from us 😄
I also new on
Next-auth