next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

How to access "session.email" on getServerSideProps

Open anshxlm opened this issue 3 years ago • 0 comments

Question 💬

export async function getServerSideProps(context) {
  //@ts-ignore
  const res = await fetch(host + `/api/auth/retrieve_profile?email=` + ****the currently signed in user email****)
  const data = await res.json()

  if (!data) {
    return {
      notFound: true
    }
  }

  return {
    props: { data }
  }
}

So my issue is I can't find a way to access the session email on server side. I understand that session.user?.email is only accessible on client side within elements, but how can I access it with getServerSideProps?

This is how I displayed the user email on the client side page:

import { unstable_getServerSession } from "next-auth/next";

export default async function AuthStatus() {
  const session = await unstable_getServerSession();
  return (
    <div className="absolute top-5 w-full flex justify-center items-center">
      {session && (
        <p className="text-stone-200 text-sm">
          Signed in as {session.user?.email}
        </p>
      )}
    </div>
  );
}

How to reproduce ☕️

export async function getServerSideProps(context) {
  //@ts-ignore
  const res = await fetch(host + `/api/auth/retrieve_profile?email=` + ****the currently signed in user email****)
  const data = await res.json()

  if (!data) {
    return {
      notFound: true
    }
  }

  return {
    props: { data }
  }
}

Contributing 🙌🏽

Yes, I am willing to help answer this question in a PR

anshxlm avatar Jan 05 '23 00:01 anshxlm