Supabase getUser() returns undefined

Code: https://github.com/jwaddell10/PracticeScheduler

Hello! Currently building a React Native, Expo app with an express backend, using supabase auth.

I am able to (I believe) log a user in, when I create my session and export it throughout the app with useContext I'm able to see an access token, I pass this token to my backend in my authorization header as a bearer token
Authorization: `Bearer ${session?.access_token}
`, and it logs on my backend as well

However, when I call getUser(token) server side with express

const { data, error } = await supabase.auth.getUser(token);

I get this error
Auth error: TypeError: Cannot read properties of undefined (reading 'getUser')

I've reread the docs, and it says if you pass in access token it should be able to get the user. I've tried removing the token from getUser call and it has the same result. Any ideas?

I tested my RLS policies with impersonation and it worked, so I think it's because getUser is undefined and not finding the user that my issue is happening

const authenticateUser = async (req, res, next) => {
    const authHeader = req.headers.authorization;

    if (!authHeader || !authHeader.startsWith("Bearer ")) {
        return res
            .status(401)
            .json({ error: "Missing or malformed auth header" });
    }

    const token = authHeader.split(" ")[1];

    try {
        const { data, error } = await supabase.auth.getUser(token);
        if (error || !data?.user) {
            console.error("Auth verification failed:", error);
            return res.status(401).json({
                error: "Invalid or expired token",
                details: error?.message,
            });
        }

        req.user = data.user; // make user available to route handlers
        next();
    } catch (err) {
        console.error("Auth error:", err);
        return res.status(401).json({ error: "Authentication failed" });
    }
};
GitHub
Contribute to jwaddell10/PracticeScheduler development by creating an account on GitHub.
Was this page helpful?