I am trying to integrate Supabase Github OAuth with Nuxt 3 but I am having an error "500 auth session missing" and it's very slow!
The server middleware causing the Error: (user.ts)
import { serverSupabaseUser } from "#supabase/server";
export default defineEventHandler(async (event) => {
event.context.user = await serverSupabaseUser(event);
});
I am passing the cookies from the client to server.
You can check the Code here:
https://github.com/mohamed-ali-masmoudi/supa-nuxt-500 Or https://stackblitz.com/~/github.com/mohamed-ali-masmoudi/supa-nuxt-500 (just reload the page if you get 404)
You just need to add SUPABASE_URL and SUPABASE_KEY to .env
Thanks
The problem is that Nuxt executes the files under "/server/middleware" on every request for all the routes also for "/" where in our case the user is not logged yet.
Here are two other potential workarounds:
1- Set a list for the protected routes (that should include the user)
import { serverSupabaseUser } from '#supabase/server';
const protectedRoutes = [
"/api/admin",
"/api/user"
]
export default defineEventHandler(async (event) => {
const url = event.node.req.url
if(protectedRoutes.some(route => url?.includes(route)) ) {
console.log("Auth needed: ", event.node.req.url)
try {
const user = await serverSupabaseUser(event);
if (user) {
event.context.user = user;
}
} catch (error) {
console.info(' ##### Auth error #####');
console.error(error);
}
}
else{
console.log("No Auth needed: ", event.node.req.url)
}
});
2- Just make it non blocking by catching the error
import { serverSupabaseUser, serverSupabaseClient } from "#supabase/server";
export default defineEventHandler(async (event) => {
try {
const user = await serverSupabaseUser(event);
event.context.user = user;
} catch (error) {
console.error("Auth error: ", error);
}
});
2- Just make it non blocking by catching the error
import { serverSupabaseUser, serverSupabaseClient } from "#supabase/server";
export default defineEventHandler(async (event) => {
try {
const user = await serverSupabaseUser(event);
event.context.user = user;
} catch (error) {
console.error("Auth error: ", error);
}
});