Skip to content

Instantly share code, notes, and snippets.

@CodingFu
Created September 28, 2023 11:23
Show Gist options
  • Save CodingFu/88042ddebd2c106a47732ec4cd23d308 to your computer and use it in GitHub Desktop.
Save CodingFu/88042ddebd2c106a47732ec4cd23d308 to your computer and use it in GitHub Desktop.
"use server";
import { Magic } from "@magic-sdk/admin";
import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
import { headers, cookies } from "next/headers";
import { NextResponse } from "next/server";
const mAdmin = new Magic(process.env.MAGIC_SECRET_KEY);
export async function POST() {
const headersList = headers();
const authorization = headersList.get("authorization");
const supabase = createRouteHandlerClient({ cookies });
if (!authorization) {
return NextResponse.json(
{ error: "No authorization header" },
{ status: 401 }
);
}
const didToken = mAdmin.utils.parseAuthorizationHeader(authorization);
try {
await mAdmin.token.validate(didToken);
} catch (e) {
console.error("Error validating token:", e);
return NextResponse.json(
{ error: "Invalid authorization header" },
{ status: 401 }
);
}
const address = mAdmin.token.getPublicAddress(didToken);
const { data: user, error } = await supabase
.from("users")
.select("*")
.eq("address", address)
.single();
if (user) {
// Update user password.
// Log in the user.
} else {
// Create a user with random password
}
return NextResponse.json({ address });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment