Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get server session is not working with a Nextjs 13 server request #8251

Closed
LoannPowell opened this issue Aug 7, 2023 · 1 comment
Closed
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@LoannPowell
Copy link

LoannPowell commented Aug 7, 2023

Environment

System:
OS: Windows 10 10.0.22621
CPU: (16) x64 AMD Ryzen 7 7700 8-Core Processor
Memory: 19.88 GB / 31.15 GB
Binaries:
Node: 20.3.1 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.22621.1992.0), Chromium (115.0.1901.188)
Internet Explorer: 11.0.22621.1

Reproduction URL

i dont have one

Describe the issue

I'm right now testing the new features and when i try a server nextjs component like this:

const Profile = async () => {
    const user: UserSession | null = await getServerSession(authOptions);
    const userInfo = await fetch(process.env.API_URL + 'user', {
        method: 'GET'
    }).then((res)=>res.json())
    .then ((data) => data)
    .catch((error) => console.error(error))
    return ( 
    <div className={styles.profileContainer}>
        <div>
            <h1>Profile picture {userInfo}</h1><br/>
            <EditImage imageUrl={user?.image as string}/>
        </div>
        <EditableInput endpoint='changename' text={user?.name} placeholder='Put the new name' title='Name'/>
    </div>)
}

export default Profile

the user endpoint always return unauthorized, when you try it into a client component it works normally.

How to reproduce

Create an endpoint with authentication in Nextjs 13 with session handling:

import { NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";
import { getServerSession } from 'next-auth'
import { UserSession } from "@/interfaces/user";
import { authOptions } from "../auth/[...nextauth]/authOptions";

const prisma = new PrismaClient()

export async function GET () {
    const userSession: userSession | null = await getServerSession(authOptions)
    if(userSession)
    {
        try {
            const user = await prisma.user.findUnique({
                where: {
                    id: userSession?.id
                },
            })
            return NextResponse.json({user})
        }
        catch {
            return NextResponse.json("Not found", { status: 400 })
        } 

    }
    else return NextResponse.json("Not Authorized", { status: 401 })
}

Call it inside a next server component always userSession is null

Expected behavior

It should return the session information and is returning null.

@LoannPowell LoannPowell added the triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. label Aug 7, 2023
@balazsorban44
Copy link
Member

check out the pinned issue #7423 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

2 participants