Skip to content
Merged
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
16 changes: 13 additions & 3 deletions src/components/features/auth/sign-in-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { Button } from "../../ui/button";

export function SignInButton({
className,
redirectTo = "/",
...props
}: React.ComponentProps<"button"> & {
className?: string;
redirectTo?: string;
}) {
const router = useRouter();

Expand All @@ -24,13 +26,13 @@ export function SignInButton({
hd: ORGANIZER_EMAIL_DOMAIN,
});
await signInWithPopup(auth, provider);
await router.navigate({
to: getSafeRedirectPath(redirectTo),
});
} catch (error) {
console.error("Error signing in with Google:", error);
} finally {
setLoading(false);
router.navigate({
to: "/",
});
}
};

Expand Down Expand Up @@ -65,3 +67,11 @@ export function SignInButton({
</Button>
);
}

const getSafeRedirectPath = (redirectTo: string) => {
if (!redirectTo.startsWith("/") || redirectTo.startsWith("//")) {
return "/";
}

return redirectTo;
};
14 changes: 13 additions & 1 deletion src/routes/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ import { NwHacksIcon } from "@/components/graphy/icono";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { createFileRoute } from "@tanstack/react-router";

type SigninSearch = {
redirect?: string;
};

export const Route = createFileRoute("/signin")({
validateSearch: (search: Record<string, unknown>): SigninSearch => ({
redirect: typeof search.redirect === "string" ? search.redirect : undefined,
}),
component: Signin,
});

function Signin() {
const { redirect } = Route.useSearch();

return (
<div className="relative h-full w-full bg-background-theme">
<img
Expand Down Expand Up @@ -37,7 +46,10 @@ function Signin() {
<CardDescription className="text-white/80">Welcome back!</CardDescription>
</CardHeader>
<CardContent>
<SignInButton className="border-none bg-gradient-to-br from-theme-green-gradient-light/80 to-theme-green-gradient-dark text-theme" />
<SignInButton
redirectTo={redirect}
className="border-none bg-gradient-to-br from-theme-green-gradient-light/80 to-theme-green-gradient-dark text-theme"
/>
</CardContent>
</Card>
</div>
Expand Down
Loading