Next.js Discord

Discord Forum

Check if user authenticated

Answered
Great Auk posted this in #help-forum
Open in Discord
Great AukOP
Since getServerSideProps is now deprecated, how do I check to see if a user is authenticated?
Answered by joulev
async function Page() {
  const session = await getSession();
  if (!session) redirect('/login');
  return <div>{session.data.user.name}</div>;
}

implement your own getSession based on your auth logic
View full answer

41 Replies

Great AukOP
Btw I'm using prisma, JWT & cookies. I can't seem to get middleware working
@Great Auk Since getServerSideProps is now deprecated, how do I check to see if a user is authenticated?
getServerSideProps is now deprecated
it isn't, it just requires using the pages router (which is not deprecated either)

how do I check to see if a user is authenticated?
in the app router, use server components
Great AukOP
What about middleware though?
async function Page() {
  const session = await getSession();
  if (!session) redirect('/login');
  return <div>{session.data.user.name}</div>;
}

implement your own getSession based on your auth logic
Answer
@Great Auk What about middleware though?
middleware remains the same
whatever middleware logic you wrote in the pages router continues to work with the app router
Great AukOP
am i right in saying this should print hello to the console on every page?
it is middleware.ts not app/middleware.ts
same level as app, not inside it
Great AukOP
ah okay
src/
  middleware.ts
  app/
    layout.tsx
    page.tsx
Great AukOP
yes that's working now, but why is it logging like 6 times when i refresh a page
Yacare Caiman
This my go-to middleware for authentication:

import { withAuth } from "next-auth/middleware"

export default withAuth({
  pages: {
    signIn: "/",
  },
})

export const config = { 
  matcher: [
    "/page/:path*",
  ]
}

This ensures that all pages specified in the array, will be protected, if the user isn't authenticated they'll be redirected to signIn
try it out
@Great Auk yes that's working now, but why is it logging like 6 times when i refresh a page
because it captures all requests, so not only the request to / but also to static files like /favicon.ico or /_next/*
you need to use a matcher to exclude unnecessary routes if needed
Yacare Caiman
oh you're not?
what do you use then=
?
Great AukOP
doing it by hand basically
storing JWT on client via cookie
@Great Auk sorry where does this go?
Yacare Caiman
it should be root folder (not app folder) -> middleware.ts
@Great Auk storing JWT on client via cookie
Yacare Caiman
oh, I suggest you use next-auth with credentials provider
lemme get a snipper rq
Great AukOP
maybe i should use next-auth... never integrated something like that with my apps
I'm using postgres & prisma for my db
Yacare Caiman
yeah you should definetly use next-auth, it just makes everything easier
Great AukOP
but say I want to use google auth, what do i store in my db
( i've used firebase before but that literally just does everything for you )
Yacare Caiman
here, this is my latest project's nextauth api, you'll see I have google and github providers, and credentials provider, so I can use my own logic https://github.com/freckledAsf/bictorchat/blob/main/app/api/auth/%5B...nextauth%5D/route.ts
@Great Auk but say I want to use google auth, what do i store in my db
Yacare Caiman
There's a model already constructed for the prisma scheme
here, this shows some basic config, including some models for your database
Great AukOP
Dope, nice and easy
Yacare Caiman
yessirr
Great AukOP
Will try it out
Yacare Caiman
oh btw, if you are planning on deploying your project to vercel, make sure to NOT include the NEXTAUTH_URL environment variable
Vercel manages that one for you