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

Auth doesn't work in Server Actions in NextJS #1537

Closed
5 of 20 tasks
judygab opened this issue Jul 28, 2023 · 14 comments
Closed
5 of 20 tasks

Auth doesn't work in Server Actions in NextJS #1537

judygab opened this issue Jul 28, 2023 · 14 comments
Labels
needs-reproduction Awaiting a minimal reproduction Stale

Comments

@judygab
Copy link

judygab commented Jul 28, 2023

Package + Version

  • @clerk/clerk-js
  • @clerk/clerk-react
  • @clerk/nextjs
  • @clerk/remix
  • @clerk/types
  • @clerk/themes
  • @clerk/localizations
  • @clerk/clerk-expo
  • @clerk/backend
  • @clerk/clerk-sdk-node
  • @clerk/shared
  • @clerk/fastify
  • @clerk/chrome-extension
  • gatsby-plugin-clerk
  • build/tooling/chore
  • other:

Dependencies + versions

{  
  "dependencies": {
    "@clerk/nextjs": "^4.22.0",
    "next": "13.4.4",
    "postcss": "8.4.23",
    "react": "18.2.0",
  },
  "devDependencies": {
    "@types/node": "^20.4.2",
    "@types/react": "18.2.15",
    "ts-node": "^10.9.1",
    "typescript": "^5.1.6"
  }
}

Description

Per documentation, I am trying to get user data inside Server Actions. I have app router and I tried both "auth" and "currentUser" inside my server actions, which are created in app/actins, they work for calling other apis but not clerk and the error that I'm getting is:

Error: Clerk: auth() and currentUser() are only supported in App Router (/app directory).
If you're using /pages, try getAuth() instead.
Original error: Error: Invariant: Method expects to have requestAsyncStorage, none available

and the code that I tried was from 'Server Actions' tab:

'use server'
import { auth } from '@clerk/nextjs'
export async function getUserId() {
  const userid = auth().userId
  console.log(userid)
}
@KairuDeibisu
Copy link

KairuDeibisu commented Jul 31, 2023

That's extremely odd, as this is working for me. I'm on nextjs canary

You'll need to do npm install --force next@canary

This is because Clark has a dependency on the version number and it doesn't recognize this experimental version. I should also mention it's basically real time updates to nextjs.

async function fetchLessonsFromDBByStudentId(id: string) {
  const user = await currentUser();
  const records = await prisma.$queryRaw(sqltag`
  SELECT lesson.lesson_id, name, description, lesson_datetime, created_by, created_on, updated_by, updated_on
  FROM lesson
  WHERE created_by = ${user?.id}
  AND lesson.lesson_id IN (
    SELECT lesson_id
    FROM lesson_participant
    WHERE user_id = ${id})`) as unknown as Array<Lesson>
  return records ?? []
}

import { getStudentLessons as getStudentLessonsDB, getTutorLessons as getTutorLessonsDB } from "@/lib/helpers";

export async function getStudentLessons(data: BaseData) : Promise<Array<Lesson>> {
    return getStudentLessonsDB(data.user_id);
}

@KairuDeibisu
Copy link

KairuDeibisu commented Jul 31, 2023

I found a similar looking issue that may be related.

#1481

@jescalan
Copy link
Contributor

Hey friends - just want to let you know that we're looking into this. So sorry for the long delay 😞

@jescalan jescalan added the needs-triage A ticket that needs to be triaged by a team member label Aug 25, 2023
@jescalan jescalan added needs-reproduction Awaiting a minimal reproduction and removed needs-triage A ticket that needs to be triaged by a team member labels Sep 1, 2023
@jescalan
Copy link
Contributor

jescalan commented Sep 1, 2023

Our most recent tests with server actions were working - is there any chance that someone would be willing to create a minimal reproduction for us on this one? 🙏

@clerk-cookie
Copy link
Collaborator

Hello 👋

We currently close issues after 40 days of inactivity. It's been 30 days since the last update here. If we missed this issue, please reply here. Otherwise, we'll close this issue in 10 days.

As a friendly reminder: The best way to see an issue fixed is to open a pull request. If you're not sure how to do that, please check out our contributing guide.

Thanks for being a part of the Clerk community! 🙏

@clerk-cookie
Copy link
Collaborator

Hello again 👋

After 40 days of no activity, we'll close this issue. Keep in mind, I'm just a robot, so if I've closed this issue in error, please reply here and my human colleagues will reopen it.

As a friendly reminder: The best way to see an issue fixed is to open a pull request. If you're not sure how to do that, please check out our contributing guide.

Thanks for being a part of the Clerk community! 🙏

@clerk-cookie clerk-cookie closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2023
@JavaG23
Copy link

JavaG23 commented Aug 1, 2024

in case anyone else runs into this....

import { auth } from '@clerk/nextjs/server';

@Leonardo-Mazzuca
Copy link

Hi there!

"dependencies": {
"@clerk/nextjs": "^6.4.2",
"next": "^15.0.4-canary.23",
"react": "19.0.0-rc-66855b96-20241106",
"react-dom": "19.0.0-rc-66855b96-20241106"
},

I'm running into all foruns that is talking about it, because i'm still facing this problem using clerk helpers with server side components.....

Every time i try to access the user info, using auth or currentUser, it returns null for me. But, if i call it on a client side component, it works perfectly.....please, someone can help me??

@raph90
Copy link

raph90 commented Nov 22, 2024

@Leonardo-Mazzuca identical situation

@raph90
Copy link

raph90 commented Nov 22, 2024

In fact - this is happening at exactly the same time for both of us, which makes me think this might be a Clerk server problem

@BRKalow
Copy link
Member

BRKalow commented Nov 22, 2024

Hi @Leonardo-Mazzuca and @raph90 can you open a separate issue with a minimal reproduction if possible? Thanks!

@raph90
Copy link

raph90 commented Nov 22, 2024

@BRKalow I don't have time to create a proper issue unfortunately - I can if this problem persists. But basically doing this:

const clerkCurrentUser = await currentUser();

is returning null on the server, even though there is an active session. I just thought it was interesting that Leonardo and I commented the same thing so close together (the post previously was August 1)

EDIT if it helps, I'm running:

"next": "14.2.3",
"@clerk/nextjs": "^6.4.0",

@raph90
Copy link

raph90 commented Nov 22, 2024

@Leonardo-Mazzuca Craziest thing but I'm leaving this here just in case this happens to anyone else.

DO NOT set an environment variable called CLERK_API_URL in your code 😆 . That is used internally by Clerk, so if you do what I did and set it to CLERK_API_URL=https://api.clerk.com/v1 stuff will break.

Took me a good few hours to find that one...

@Leonardo-Mazzuca
Copy link

@Leonardo-Mazzuca Craziest thing but I'm leaving this here just in case this happens to anyone else.

DO NOT set an environment variable called CLERK_API_URL in your code 😆 . That is used internally by Clerk, so if you do what I did and set it to CLERK_API_URL=https://api.clerk.com/v1 stuff will break.

Took me a good few hours to find that one...

Oh gosh, a small thing that made u lost a lot of time.
Well its how i say to others, somentimes, u can turn into the joker if u are a programmer.

But well done my friend, good that u found that issue😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-reproduction Awaiting a minimal reproduction Stale
Projects
None yet
Development

No branches or pull requests

8 participants