I have a table in supabase called Users
which needs email
and authId
.I want to update these user data from the clerk. However, I am not able to retrieve the email address from the clerk’s middleware. I need help on how to get the email address.
I have tried to retrieve the email from UserId using getAuth
by importing it from "@clerk/nextjs/client"
. But not successful.
import { clerkMiddleware } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import { supabase } from "./utils/supabase/supabase";
import { getUser } from "@clerk/nextjs/client";
const isProtectedRoute = (req) => {
const protectedPaths = ["/add-listing", "/(api|trpc)(.*)"];
return protectedPaths.some((path) => req.url.includes(path));
};
export default clerkMiddleware(async (auth, req) => {
console.log("Middleware is triggered");
const { userId, sessionClaims } = auth() || {};
console.log("userId:", userId);
console.log("sessionClaims:", sessionClaims);
if (userId) {
console.log("User is authenticated:", userId);
const user = await getAuth({ userId });
const email = user.email;
const authId = userId;
console.log("Email:", email);
console.log("authId:", authId);
const { error } = await supabase.from("Users").upsert({ email, authId });
if (error) {
console.error("Error inserting user into Supabase:", error);
} else {
console.log("User inserted or updated in Supabase successfully");
}
} else {
console.log("User is not authenticated");
}
if (isProtectedRoute(req) && !userId) {
return new Response("Unauthorized", { status: 401 });
}
return NextResponse.next();
});
export const config = {
matcher: [
// Match API routes or specific protected paths
"/add-listing",
"/(api|trpc)(.*)",
"/((?!_next|.*\.).*)",
]
};
- We import getAuth from “@clerk/nextjs/server” instead of the client version.
- We use await getAuth(req) to get the userId.
- To get the user’s email, we use req.auth.user which gives us access to the full user object.
- The email is accessed via user.emailAddresses[0].emailAddress. This assumes the user has at least one email address associated with their account.
import { clerkMiddleware, getAuth } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import { supabase } from "./utils/supabase/supabase";
const isProtectedRoute = (req) => {
const protectedPaths = ["/add-listing", "/(api|trpc)(.*)"];
return protectedPaths.some((path) => req.url.includes(path));
};
export default clerkMiddleware(async (req, evt) => {
console.log("Middleware is triggered");
const { userId } = await getAuth(req);
console.log("userId:", userId);
if (userId) {
console.log("User is authenticated:", userId);
const user = await req.auth.user;
const email = user.emailAddresses[0].emailAddress;
const authId = userId;
console.log("Email:", email);
console.log("authId:", authId);
const { error } = await supabase.from("Users").upsert({ email, authId });
if (error) {
console.error("Error inserting user into Supabase:", error);
} else {
console.log("User inserted or updated in Supabase successfully");
}
} else {
console.log("User is not authenticated");
}
if (isProtectedRoute(req) && !userId) {
return new Response("Unauthorized", { status: 401 });
}
return NextResponse.next();
});
export const config = {
matcher: [
"/add-listing",
"/(api|trpc)(.*)",
"/((?!_next|.*\.).*)",
],
};
cihanolmalibu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.