mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-02-27 11:21:59 +00:00
* refactor: rework frontend use effect calls * fix: rabbit comments * fix: handle empty oauth url in login page
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import { TotpForm } from "@/components/auth/totp-form";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Card,
|
|
CardContent,
|
|
CardDescription,
|
|
CardFooter,
|
|
CardHeader,
|
|
CardTitle,
|
|
} from "@/components/ui/card";
|
|
import { useUserContext } from "@/context/user-context";
|
|
import { TotpSchema } from "@/schemas/totp-schema";
|
|
import { useMutation } from "@tanstack/react-query";
|
|
import axios from "axios";
|
|
import { useEffect, useId, useRef } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Navigate, useLocation } from "react-router";
|
|
import { toast } from "sonner";
|
|
import { useOIDCParams } from "@/lib/hooks/oidc";
|
|
|
|
export const TotpPage = () => {
|
|
const { totpPending } = useUserContext();
|
|
const { t } = useTranslation();
|
|
const { search } = useLocation();
|
|
const formId = useId();
|
|
|
|
const redirectTimer = useRef<number | null>(null);
|
|
|
|
const searchParams = new URLSearchParams(search);
|
|
const {
|
|
values: props,
|
|
isOidc,
|
|
compiled: compiledOIDCParams,
|
|
} = useOIDCParams(searchParams);
|
|
|
|
const totpMutation = useMutation({
|
|
mutationFn: (values: TotpSchema) => axios.post("/api/user/totp", values),
|
|
mutationKey: ["totp"],
|
|
onSuccess: () => {
|
|
toast.success(t("totpSuccessTitle"), {
|
|
description: t("totpSuccessSubtitle"),
|
|
});
|
|
|
|
redirectTimer.current = window.setTimeout(() => {
|
|
if (isOidc) {
|
|
window.location.replace(`/authorize?${compiledOIDCParams}`);
|
|
return;
|
|
}
|
|
|
|
window.location.replace(
|
|
`/continue${props.redirect_uri ? `?redirect_uri=${encodeURIComponent(props.redirect_uri)}` : ""}`,
|
|
);
|
|
}, 500);
|
|
},
|
|
onError: () => {
|
|
toast.error(t("totpFailTitle"), {
|
|
description: t("totpFailSubtitle"),
|
|
});
|
|
},
|
|
});
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (redirectTimer.current) {
|
|
clearTimeout(redirectTimer.current);
|
|
}
|
|
};
|
|
}, [redirectTimer]);
|
|
|
|
if (!totpPending) {
|
|
return <Navigate to="/" replace />;
|
|
}
|
|
|
|
return (
|
|
<Card className="min-w-xs sm:min-w-sm">
|
|
<CardHeader>
|
|
<CardTitle className="text-3xl">{t("totpTitle")}</CardTitle>
|
|
<CardDescription>{t("totpSubtitle")}</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="flex flex-col items-center">
|
|
<TotpForm
|
|
formId={formId}
|
|
onSubmit={(values) => totpMutation.mutate(values)}
|
|
/>
|
|
</CardContent>
|
|
<CardFooter className="flex flex-col items-stretch">
|
|
<Button form={formId} type="submit" loading={totpMutation.isPending}>
|
|
{t("continueTitle")}
|
|
</Button>
|
|
</CardFooter>
|
|
</Card>
|
|
);
|
|
};
|