File size: 2,295 Bytes
b213d9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { createClient } from "@supabase/supabase-js";
import { FastifyRequest, FastifyReply } from "fastify";
import { prisma } from "@repo/db";
import * as dotenv from "dotenv";
import * as path from "path";

// Load API's own .env first (production), then root .env as fallback (local dev)
dotenv.config({ path: path.resolve(__dirname, "../.env") });
dotenv.config({ path: path.resolve(__dirname, "../../../.env") });


const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || "";
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || "";

if (!supabaseUrl || !supabaseKey) {
  console.warn("Missing Supabase credentials in backend!");
}

export const supabase = createClient(supabaseUrl, supabaseKey);

export async function verifySupabaseAuth(request: FastifyRequest, reply: FastifyReply) {
  const authHeader = request.headers.authorization;
  if (!authHeader || !authHeader.startsWith("Bearer ")) {
    return reply.code(401).send({ error: "Missing or invalid authorization header" });
  }

  const token = authHeader.substring(7);

  try {
    const { data, error } = await supabase.auth.getUser(token);
    
    if (error || !data.user) {
      return reply.code(401).send({ error: "Invalid token" });
    }

    const sbUser = data.user;

    // Check if user exists in Prisma
    let dbUser = await prisma.user.findUnique({
      where: { id: sbUser.id }
    });

    // Just-in-Time Provisioning: If user doesn't exist in Prisma DB, create them
    if (!dbUser) {
      const email = sbUser.email || "";
      const name = sbUser.user_metadata?.full_name || email.split("@")[0] || "User";
      
      // Upsert to handle edge cases where email might already exist with a different ID
      // But ideally ID should be consistent
      dbUser = await prisma.user.upsert({
        where: { id: sbUser.id },
        update: { name, email },
        create: {
          id: sbUser.id,
          email,
          name,
          passwordHash: "supabase-auth", // placeholder for Supabase users
        }
      });
    }

    // Attach user to request
    (request as any).user = { id: dbUser.id, email: dbUser.email, name: dbUser.name };
  } catch (err) {
    console.error("Auth verification error:", err);
    return reply.code(401).send({ error: "Unauthorized" });
  }
}