`supabase.auth.api.getUserByCookie()` doesn't work in Next.JS server-side environment
See original GitHub issueBug report
Describe the bug
supabase.auth.api.getUserByCookie()
doesn’t work in Next.JS server-side environment (_middleware.ts
)
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Create a supabase project
- Create a Next.JS project
- Create
Add following code to _middleware.ts
export async function middleware(req: NextRequest, ev: NextFetchEvent) {
let supabase = createClient(...);
let user = await supabase.auth.api.getUserByCookie(req);
console.log('Auth ', user)
}
- See logs
Auth {
user: null,
data: null,
error: ReferenceError: XMLHttpRequest is not defined
at eval (webpack-internal:///./node_modules/cross-fetch/dist/browser-ponyfill.js?d2fb:462:17)
at new Promise (<anonymous>)
at fetch (webpack-internal:///./node_modules/cross-fetch/dist/browser-ponyfill.js?d2fb:455:12)
at eval (webpack-internal:///./node_modules/@supabase/gotrue-js/dist/module/lib/fetch.js?85f4:44:63)
at new Promise (<anonymous>)
at eval (webpack-internal:///./node_modules/@supabase/gotrue-js/dist/module/lib/fetch.js?85f4:43:16)
at Generator.next (<anonymous>)
at eval (webpack-internal:///./node_modules/@supabase/gotrue-js/dist/module/lib/fetch.js?85f4:16:71)
at new Promise (<anonymous>)
at __awaiter (webpack-internal:///./node_modules/@supabase/gotrue-js/dist/module/lib/fetch.js?85f4:12:12)
}
Expected behavior
getUserByCookie() should either work (use fetch()
internally instead of XMLHttpRequest
), OR message should be more clear (check the presence of XMLHttpRequest
in getUserByCookie()
and throw Sorry, you shouldn't call this method in server side environment
System information
Next version is 12.0.1
Additional context
Add any other context about the problem here.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:43 (18 by maintainers)
Top Results From Across the Web
Get supabase `user` server side in next.js - Stack Overflow
If you need to get the user in server-side, you need to set the Auth Cookie in the server using the given Next.js...
Read more >Supabase Auth with Next.js
Authentication helpers for Next.js API routes, middleware, and SSR. ... and only run your query once the user is defined client-side in the...
Read more >Supabase Auth with Next.js 12 middleware - Jitsu
Unlike with React, you can write server-side code with Next.js too. ... getUserByCookie , but unfortunately it doesn't work for Next.js.
Read more >Setting up Server Side Auth with Supabase and NextJS
We are using again the supabase object that gives access to the set of API related to auth. setAuthCookie is using the under...
Read more >Magic Link Authentication and Route Controls with Supabase ...
In this post, we'll build out a Next.js app that enables navigation, authentication, authorization, redirects (client and server-side), ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
FYI, this workaroung does the job:
after more thorough testing I found the error was persisiting. sorry about the false posititve.
I added another commit to the PR, based on Thor’s link to the cross-fetch package - in particular, this comment. I can confirm that this works in Next.js middleware, on a local environment.
I’ll follow up on the linked package and see if that change is OK to merge