forked from Mike/freno-dev
migrating
This commit is contained in:
54
src/routes/api/auth/callback/google.ts
Normal file
54
src/routes/api/auth/callback/google.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import type { APIEvent } from "@solidjs/start/server";
|
||||
import { appRouter } from "~/server/api/root";
|
||||
import { createTRPCContext } from "~/server/api/utils";
|
||||
|
||||
export async function GET(event: APIEvent) {
|
||||
const url = new URL(event.request.url);
|
||||
const code = url.searchParams.get("code");
|
||||
const error = url.searchParams.get("error");
|
||||
|
||||
// Handle OAuth error (user denied access, etc.)
|
||||
if (error) {
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: { Location: `/login?error=${encodeURIComponent(error)}` },
|
||||
});
|
||||
}
|
||||
|
||||
// Missing authorization code
|
||||
if (!code) {
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: { Location: "/login?error=missing_code" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
// Create tRPC caller to invoke the googleCallback procedure
|
||||
const ctx = await createTRPCContext(event);
|
||||
const caller = appRouter.createCaller(ctx);
|
||||
|
||||
// Call the Google callback handler
|
||||
const result = await caller.auth.googleCallback({ code });
|
||||
|
||||
if (result.success) {
|
||||
// Redirect to account page on success
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: { Location: result.redirectTo || "/account" },
|
||||
});
|
||||
} else {
|
||||
// Redirect to login with error
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: { Location: "/login?error=auth_failed" },
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Google OAuth callback error:", error);
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: { Location: "/login?error=server_error" },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user