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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Server Session in Server Actions #7460

Closed
EliasGcf opened this issue May 5, 2023 · 7 comments
Closed

Get Server Session in Server Actions #7460

EliasGcf opened this issue May 5, 2023 · 7 comments
Labels
question Ask how to do something or how something works stale Did not receive any activity for 60 days

Comments

@EliasGcf
Copy link

EliasGcf commented May 5, 2023

Question 馃挰

How to get the session inside a Server Actions?

Show this error:

image

How to reproduce 鈽曪笍

export async function create(formData: FormData) {
  'use server';

  const session = await getServerSession();

  console.log(session);
}

Contributing 馃檶馃徑

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

@EliasGcf EliasGcf added the question Ask how to do something or how something works label May 5, 2023
@EliasGcf
Copy link
Author

EliasGcf commented May 5, 2023

It worked sending it via the function param. Is it a good solution/alternative to the code I first provided for now?

Like this:

<form action={(formData) => create(formData, session)}>

In the network tab:

image

@EliasGcf EliasGcf closed this as completed May 5, 2023
@EliasGcf EliasGcf reopened this May 5, 2023
@Talent30
Copy link

Talent30 commented May 6, 2023

vercel/next.js#49235

@stale
Copy link

stale bot commented Jul 14, 2023

It looks like this issue did not receive any activity for 60 days. It will be closed in 7 days if no further activity occurs. If you think your issue is still relevant, commenting will keep it open. Thanks!

@stale stale bot added the stale Did not receive any activity for 60 days label Jul 14, 2023
@ThangHuuVu
Copy link
Member

this is an upstream issue with Next.js that is linked above. Please try next@13.4.2-canary.2, thanks 馃檹

@mr-schulz
Copy link

It worked sending it via the function param. Is it a good solution/alternative to the code I first provided for now?

Like this:

<form action={(formData) => create(formData, session)}>

In the network tab:

image

i think this is unsecure and everybody should avoid it!
server actions are basicly post requests, a attacker could manipulate or create the session part in create(formData, session)}

maybe it only make sence if there is encrypted token in the session, which can be checked on server side (server action body)

correct me please if i am wrong

@mcapodici
Copy link

mcapodici commented Oct 27, 2023

An adaptation of this worked for me (source: https://www.reddit.com/r/nextjs/comments/13eb1vj/comment/jjptpkx/?utm_source=reddit&utm_medium=web2x&context=3)

My server action looks like this:

export async function addNewChatAgent(data: Values) {
    const session = await getServerSession(authOptions)
    if (!session) {
        throw Error("oops");
    }
    const { userId } = session as any;
    const agent: NewChatAgent = {
        model: data.model,
        name: data.name,
        user_id: session.user.id,       
    }
    await db.insert(chatAgent).values(agent);
}

@liuhe2020
Copy link

An adaptation of this worked for me (source: https://www.reddit.com/r/nextjs/comments/13eb1vj/comment/jjptpkx/?utm_source=reddit&utm_medium=web2x&context=3)

My server action looks like this:

export async function addNewChatAgent(data: Values) {
    const session = await getServerSession(authOptions)
    if (!session) {
        throw Error("oops");
    }
    const { userId } = session as any;
    const agent: NewChatAgent = {
        model: data.model,
        name: data.name,
        user_id: session.user.id,       
    }
    await db.insert(chatAgent).values(agent);
}

Works a treat, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Ask how to do something or how something works stale Did not receive any activity for 60 days
Projects
None yet
Development

No branches or pull requests

6 participants