feat(auth-form): enhance user registration and login process

This commit introduces some improvements to the user registration and login processes. It standardizes the update interval in the user data upon registration, fixes the missing 'toString' call for 'access_token' and improves code readability by correcting indents and adding extra spaces. Additionally, this commit refactors the redirection logic after successful login or registration, making it more robust and reliable.
This commit is contained in:
Mathis H (Avnyr) 2024-06-18 21:32:49 +02:00
parent 1898d554f9
commit d54d05403b
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -4,18 +4,22 @@ import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs";
import AutoForm, { AutoFormSubmit } from "@/components/auto-form";
import { UserDataContext } from "@/components/providers/userdata-provider";
import type {IApiLoginReq, IApiLoginRes, IApiRegisterReq, IApiRegisterRes} from "@/interfaces/api.interface";
import { ToastBox, toastType } from "@/components/ui/toast-box";
import { useToast } from "@/components/ui/use-toast";
import type {
IApiLoginReq,
IApiLoginRes,
IApiRegisterReq,
IApiRegisterRes,
} from "@/interfaces/api.interface";
import { EReturnState, type IStandardisedReturn } from "@/interfaces/general.interface";
import type { IUserData } from "@/interfaces/userdata.interface";
import ApiRequest from "@/services/apiRequest";
import {EReturnState, type IStandardisedReturn} from "@/services/general.interface";
import { useLocalStorage } from "@/services/localStorage";
import { Bug, RefreshCw } from "lucide-react";
import Link from "next/link";
import { type Dispatch, type SetStateAction, useContext, useState } from "react";
import * as z from "zod";
import {ToastBox, toastType} from "@/components/ui/toast-box";
import {useToast} from "@/components/ui/use-toast";
const loginSchema = z.object({
email: z
@ -62,7 +66,7 @@ export function AuthForms() {
const [isLoading, setIsLoading] = useState(false);
const [sub, setSub] = useLocalStorage<string | undefined>("sub", "");
const userContext = useContext(UserDataContext);
const { toast } = useToast()
const { toast } = useToast();
async function doRegister(
registerData: IApiRegisterReq,
@ -76,8 +80,15 @@ export function AuthForms() {
>("auth/signup", registerData);
console.trace(ReqRes.data);
if (ReqRes.data.user) {
userDataSetter(ReqRes.data.user);
setSub(ReqRes.data.access_token);
userDataSetter({
...ReqRes.data.user,
wallet: {
uat: Date.now(),
update_interval: 30_000,
owned_cryptos: [],
},
});
setSub(ReqRes.data.access_token?.toString());
}
console.debug(ReqRes.data.message || "Not additional message from request");
return {
@ -147,19 +158,19 @@ export function AuthForms() {
toast({
description: res.message || "An unexpected error occurred..",
variant: "destructive",
})
setIsLoading(false)
return
});
setIsLoading(false);
return;
}
//toast.custom(<ToastBox message={"Login successful ! \n You will be redirected."} type={toastType.success}/>)
toast({
description: "Login successful ! \n You will be redirected."
})
description: "Login successful ! \n You will be redirected.",
});
setTimeout(() => {
setIsLoading(false)
location.href = "/"
console.log('Moving to home.')
}, 3_000)
setIsLoading(false);
location.href = "/";
console.log("Moving to home.");
}, 3_000);
});
}}
fieldConfig={{
@ -193,15 +204,15 @@ export function AuthForms() {
).then((res) => {
if (res.state !== EReturnState.done) {
//toast.custom(<ToastBox message={res.message || "An unexpected error occurred.."} type={toastType.error}/>)
setIsLoading(false)
return
setIsLoading(false);
return;
}
//toast.custom(<ToastBox message={"Register successful ! \n You will be redirected."} type={toastType.success}/>)
setTimeout(() => {
setIsLoading(false)
setIsLoading(false);
//location.href = "/"
console.log('Moving to home.')
}, 5_000)
console.log("Moving to home.");
}, 5_000);
});
}}
fieldConfig={{