mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-01 16:45:40 +02:00
option to log out if on verify email screen
This commit is contained in:
parent
bd3d9d2da3
commit
d194e230de
3 changed files with 71 additions and 65 deletions
|
@ -20,7 +20,7 @@ export default async function Page(props: {
|
||||||
}) {
|
}) {
|
||||||
const searchParams = await props.searchParams;
|
const searchParams = await props.searchParams;
|
||||||
const getUser = cache(verifySession);
|
const getUser = cache(verifySession);
|
||||||
const user = await getUser();
|
const user = await getUser({ skipCheckVerifyEmail: true });
|
||||||
|
|
||||||
const isInvite = searchParams?.redirect?.includes("/invite");
|
const isInvite = searchParams?.redirect?.includes("/invite");
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ import { Mail } from "lucide-react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { cache } from "react";
|
import { cache } from "react";
|
||||||
import { getTranslations } from 'next-intl/server';
|
import { getTranslations } from "next-intl/server";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ export default async function Page(props: {
|
||||||
}) {
|
}) {
|
||||||
const searchParams = await props.searchParams;
|
const searchParams = await props.searchParams;
|
||||||
const getUser = cache(verifySession);
|
const getUser = cache(verifySession);
|
||||||
const user = await getUser();
|
const user = await getUser({ skipCheckVerifyEmail: true });
|
||||||
const t = await getTranslations();
|
const t = await getTranslations();
|
||||||
|
|
||||||
const env = pullEnv();
|
const env = pullEnv();
|
||||||
|
@ -56,10 +56,10 @@ export default async function Page(props: {
|
||||||
<div className="flex flex-col items-center">
|
<div className="flex flex-col items-center">
|
||||||
<Mail className="w-12 h-12 mb-4 text-primary" />
|
<Mail className="w-12 h-12 mb-4 text-primary" />
|
||||||
<h2 className="text-2xl font-bold mb-2 text-center">
|
<h2 className="text-2xl font-bold mb-2 text-center">
|
||||||
{t('inviteAlready')}
|
{t("inviteAlready")}
|
||||||
</h2>
|
</h2>
|
||||||
<p className="text-center">
|
<p className="text-center">
|
||||||
{t('inviteAlreadyDescription')}
|
{t("inviteAlreadyDescription")}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -72,7 +72,7 @@ export default async function Page(props: {
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<p className="text-center text-muted-foreground mt-4">
|
<p className="text-center text-muted-foreground mt-4">
|
||||||
{t('signupQuestion')}{" "}
|
{t("signupQuestion")}{" "}
|
||||||
<Link
|
<Link
|
||||||
href={
|
href={
|
||||||
!redirectUrl
|
!redirectUrl
|
||||||
|
@ -81,7 +81,7 @@ export default async function Page(props: {
|
||||||
}
|
}
|
||||||
className="underline"
|
className="underline"
|
||||||
>
|
>
|
||||||
{t('login')}
|
{t("login")}
|
||||||
</Link>
|
</Link>
|
||||||
</p>
|
</p>
|
||||||
</>
|
</>
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {
|
||||||
CardContent,
|
CardContent,
|
||||||
CardDescription,
|
CardDescription,
|
||||||
CardHeader,
|
CardHeader,
|
||||||
CardTitle,
|
CardTitle
|
||||||
} from "@/components/ui/card";
|
} from "@/components/ui/card";
|
||||||
import {
|
import {
|
||||||
Form,
|
Form,
|
||||||
|
@ -19,21 +19,21 @@ import {
|
||||||
FormField,
|
FormField,
|
||||||
FormItem,
|
FormItem,
|
||||||
FormLabel,
|
FormLabel,
|
||||||
FormMessage,
|
FormMessage
|
||||||
} from "@/components/ui/form";
|
} from "@/components/ui/form";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import {
|
import {
|
||||||
InputOTP,
|
InputOTP,
|
||||||
InputOTPGroup,
|
InputOTPGroup,
|
||||||
InputOTPSlot,
|
InputOTPSlot
|
||||||
} from "@/components/ui/input-otp";
|
} from "@/components/ui/input-otp";
|
||||||
import { AxiosResponse } from "axios";
|
import { AxiosResponse } from "axios";
|
||||||
import { VerifyEmailResponse } from "@server/routers/auth";
|
import { VerifyEmailResponse } from "@server/routers/auth";
|
||||||
import { Loader2 } from "lucide-react";
|
import { ArrowRight, IdCard, Loader2 } from "lucide-react";
|
||||||
import { Alert, AlertDescription } from "../../../components/ui/alert";
|
import { Alert, AlertDescription } from "../../../components/ui/alert";
|
||||||
import { toast } from "@app/hooks/useToast";
|
import { toast } from "@app/hooks/useToast";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { formatAxiosError } from "@app/lib/api";;
|
import { formatAxiosError } from "@app/lib/api";
|
||||||
import { createApiClient } from "@app/lib/api";
|
import { createApiClient } from "@app/lib/api";
|
||||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||||
import { cleanRedirect } from "@app/lib/cleanRedirect";
|
import { cleanRedirect } from "@app/lib/cleanRedirect";
|
||||||
|
@ -46,7 +46,7 @@ export type VerifyEmailFormProps = {
|
||||||
|
|
||||||
export default function VerifyEmailForm({
|
export default function VerifyEmailForm({
|
||||||
email,
|
email,
|
||||||
redirect,
|
redirect
|
||||||
}: VerifyEmailFormProps) {
|
}: VerifyEmailFormProps) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const t = useTranslations();
|
const t = useTranslations();
|
||||||
|
@ -58,19 +58,34 @@ export default function VerifyEmailForm({
|
||||||
|
|
||||||
const api = createApiClient(useEnvContext());
|
const api = createApiClient(useEnvContext());
|
||||||
|
|
||||||
|
function logout() {
|
||||||
|
api.post("/auth/logout")
|
||||||
|
.catch((e) => {
|
||||||
|
console.error(t("logoutError"), e);
|
||||||
|
toast({
|
||||||
|
title: t("logoutError"),
|
||||||
|
description: formatAxiosError(e, t("logoutError"))
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
router.push("/auth/login");
|
||||||
|
router.refresh();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const FormSchema = z.object({
|
const FormSchema = z.object({
|
||||||
email: z.string().email({ message: t('emailInvalid') }),
|
email: z.string().email({ message: t("emailInvalid") }),
|
||||||
pin: z.string().min(8, {
|
pin: z.string().min(8, {
|
||||||
message: t('verificationCodeLengthRequirements'),
|
message: t("verificationCodeLengthRequirements")
|
||||||
}),
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof FormSchema>>({
|
const form = useForm<z.infer<typeof FormSchema>>({
|
||||||
resolver: zodResolver(FormSchema),
|
resolver: zodResolver(FormSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
email: email,
|
email: email,
|
||||||
pin: "",
|
pin: ""
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function onSubmit(data: z.infer<typeof FormSchema>) {
|
async function onSubmit(data: z.infer<typeof FormSchema>) {
|
||||||
|
@ -78,19 +93,17 @@ export default function VerifyEmailForm({
|
||||||
|
|
||||||
const res = await api
|
const res = await api
|
||||||
.post<AxiosResponse<VerifyEmailResponse>>("/auth/verify-email", {
|
.post<AxiosResponse<VerifyEmailResponse>>("/auth/verify-email", {
|
||||||
code: data.pin,
|
code: data.pin
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
setError(formatAxiosError(e, t('errorOccurred')));
|
setError(formatAxiosError(e, t("errorOccurred")));
|
||||||
console.error(t('emailErrorVerify'), e);
|
console.error(t("emailErrorVerify"), e);
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res && res.data?.data?.valid) {
|
if (res && res.data?.data?.valid) {
|
||||||
setError(null);
|
setError(null);
|
||||||
setSuccessMessage(
|
setSuccessMessage(t("emailVerified"));
|
||||||
t('emailVerified')
|
|
||||||
);
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
const safe = cleanRedirect(redirect);
|
const safe = cleanRedirect(redirect);
|
||||||
|
@ -107,16 +120,16 @@ export default function VerifyEmailForm({
|
||||||
setIsResending(true);
|
setIsResending(true);
|
||||||
|
|
||||||
const res = await api.post("/auth/verify-email/request").catch((e) => {
|
const res = await api.post("/auth/verify-email/request").catch((e) => {
|
||||||
setError(formatAxiosError(e, t('errorOccurred')));
|
setError(formatAxiosError(e, t("errorOccurred")));
|
||||||
console.error(t('verificationCodeErrorResend'), e);
|
console.error(t("verificationCodeErrorResend"), e);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
setError(null);
|
setError(null);
|
||||||
toast({
|
toast({
|
||||||
variant: "default",
|
variant: "default",
|
||||||
title: t('verificationCodeResend'),
|
title: t("verificationCodeResend"),
|
||||||
description: t('verificationCodeResendDescription'),
|
description: t("verificationCodeResendDescription")
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,40 +140,26 @@ export default function VerifyEmailForm({
|
||||||
<div>
|
<div>
|
||||||
<Card className="w-full max-w-md">
|
<Card className="w-full max-w-md">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>{t('emailVerify')}</CardTitle>
|
<CardTitle>{t("emailVerify")}</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
{t('emailVerifyDescription')}
|
{t("emailVerifyDescription")}
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
|
<p className="text-center text-muted-foreground mb-4">
|
||||||
|
{email}
|
||||||
|
</p>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form
|
<form
|
||||||
onSubmit={form.handleSubmit(onSubmit)}
|
onSubmit={form.handleSubmit(onSubmit)}
|
||||||
className="space-y-4"
|
className="space-y-4"
|
||||||
|
id="verify-email-form"
|
||||||
>
|
>
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="email"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>{t('email')}</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
{...field}
|
|
||||||
disabled
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="pin"
|
name="pin"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>{t('verificationCode')}</FormLabel>
|
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<div className="flex justify-center">
|
<div className="flex justify-center">
|
||||||
<InputOTP
|
<InputOTP
|
||||||
|
@ -197,13 +196,23 @@ export default function VerifyEmailForm({
|
||||||
</div>
|
</div>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
<FormDescription>
|
|
||||||
{t('verificationCodeEmailSent')}
|
|
||||||
</FormDescription>
|
|
||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<div className="text-center text-muted-foreground">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="link"
|
||||||
|
onClick={handleResendCode}
|
||||||
|
disabled={isResending}
|
||||||
|
>
|
||||||
|
{isResending
|
||||||
|
? t("emailVerifyResendProgress")
|
||||||
|
: t("emailVerifyResend")}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
{error && (
|
{error && (
|
||||||
<Alert variant="destructive">
|
<Alert variant="destructive">
|
||||||
<AlertDescription>{error}</AlertDescription>
|
<AlertDescription>{error}</AlertDescription>
|
||||||
|
@ -222,29 +231,26 @@ export default function VerifyEmailForm({
|
||||||
type="submit"
|
type="submit"
|
||||||
className="w-full"
|
className="w-full"
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
|
form="verify-email-form"
|
||||||
>
|
>
|
||||||
{isSubmitting && (
|
{isSubmitting && (
|
||||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||||
)}
|
)}
|
||||||
{t('submit')}
|
{t("submit")}
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant={"secondary"}
|
||||||
|
className="w-full"
|
||||||
|
onClick={logout}
|
||||||
|
>
|
||||||
|
Log in with another account
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<div className="text-center text-muted-foreground mt-2">
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="link"
|
|
||||||
onClick={handleResendCode}
|
|
||||||
disabled={isResending}
|
|
||||||
>
|
|
||||||
{isResending
|
|
||||||
? t('emailVerifyResendProgress')
|
|
||||||
: t('emailVerifyResend')}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue