Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions services/hackbot-ui/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
"use client";

import { useState } from "react";
import { use, useState } from "react";

import { signIn } from "@/lib/auth-client";

export default function LoginPage() {
type LoginPageProps = {
searchParams: Promise<{
callbackURL?: string | string[];
error?: string | string[];
}>;
};

export default function LoginPage({ searchParams }: LoginPageProps) {
const params = use(searchParams);
const callbackURL =
typeof params.callbackURL === "string" ? params.callbackURL : "/";
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

async function onGoogle() {
setError(null);
setLoading(true);

// Keep the target across a denied sign-in so a retry still lands on it.
const errorParams = new URLSearchParams({ error: "denied", callbackURL });

try {
await signIn.social({
provider: "google",
callbackURL: "/",
errorCallbackURL: "/login?error=denied",
callbackURL,
errorCallbackURL: `/login?${errorParams.toString()}`,
});
} catch (err) {
setError((err as Error).message);
setLoading(false);
}
}

const denied =
typeof window !== "undefined" &&
new URLSearchParams(window.location.search).get("error");
const denied = params.error === "denied";

return (
<div style={{ maxWidth: 420, margin: "64px auto" }}>
Expand Down
2 changes: 2 additions & 0 deletions services/hackbot-ui/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export function middleware(req: NextRequest) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

// Remember the requested URL so the login page can send the user back here
const loginUrl = new URL("/login", req.url);
loginUrl.searchParams.set("callbackURL", req.nextUrl.toString());
return NextResponse.redirect(loginUrl);
}

Expand Down