Authentication with Cookies
Unanswered
Asian black bear posted this in #help-forum
Asian black bearOP
Hello all,
I am new to NextJS. I tried to implement authentication using cookies and I am facing some issues with the implementation. I have set the authentication cookie to expire in 30 seconds for testing.
Issue:
1. Once I login, I set the authentication cookies via api route and redirect to a protected route ("/list-meeting") in client component using useRouter from "next/navigation"
2. I wait for 30 seconds and then click on a button with next/link to another protected route ("/create-meeting"). Middleware then redirects me to login page.
3. After I login, I am again redirected to "list-meeting", which is a client component
4. Now when I click on next/link before the token expiry, I am redirected to "login" page.
I can't access the "create-meeting" page until I reload the page.
Let me know what I need to add for better understanding.
I am new to NextJS. I tried to implement authentication using cookies and I am facing some issues with the implementation. I have set the authentication cookie to expire in 30 seconds for testing.
Issue:
1. Once I login, I set the authentication cookies via api route and redirect to a protected route ("/list-meeting") in client component using useRouter from "next/navigation"
2. I wait for 30 seconds and then click on a button with next/link to another protected route ("/create-meeting"). Middleware then redirects me to login page.
3. After I login, I am again redirected to "list-meeting", which is a client component
4. Now when I click on next/link before the token expiry, I am redirected to "login" page.
I can't access the "create-meeting" page until I reload the page.
Let me know what I need to add for better understanding.
4 Replies
@Asian black bear Hello all,
I am new to NextJS. I tried to implement authentication using cookies and I am facing some issues with the implementation. I have set the authentication cookie to expire in 30 seconds for testing.
Issue:
1. Once I login, I set the authentication cookies via api route and redirect to a protected route ("/list-meeting") in client component using useRouter from "next/navigation"
2. I wait for 30 seconds and then click on a button with next/link to another protected route ("/create-meeting"). Middleware then redirects me to login page.
3. After I login, I am again redirected to "list-meeting", which is a client component
4. Now when I click on next/link before the token expiry, I am redirected to "login" page.
I can't access the "create-meeting" page until I reload the page.
Let me know what I need to add for better understanding.
Just a test (this is not the way to fix this):
Try adding this to next.config.js, does it work now?
Try adding this to next.config.js, does it work now?
/** @type {import('next').NextConfig} */
const nextConfig = {
// your other configurations
experimental: {
staleTimes: {
dynamic: 0,
},
},
}
module.exports = nextConfigAsian black bearOP
It didn't solve the problem.
then i don't know, my theory on why it didn't work was wrong, sorry
Asian black bearOP
No worries, let me know if I can add more information.