From d54d05403b8c56bbaf0240c0c4e6c651e7d117f8 Mon Sep 17 00:00:00 2001 From: Mathis Date: Tue, 18 Jun 2024 21:32:49 +0200 Subject: [PATCH] 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. --- src/components/auth-form.tsx | 73 +++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/src/components/auth-form.tsx b/src/components/auth-form.tsx index 05859cb..929d9a5 100644 --- a/src/components/auth-form.tsx +++ b/src/components/auth-form.tsx @@ -1,21 +1,25 @@ "use client"; -import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs"; +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 type {IUserData} from "@/interfaces/userdata.interface"; +import AutoForm, { AutoFormSubmit } from "@/components/auto-form"; +import { UserDataContext } from "@/components/providers/userdata-provider"; +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 { 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 { 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("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() toast({ - description: "Login successful ! \n You will be redirected." - }) - setTimeout(()=>{ - setIsLoading(false) - location.href = "/" - console.log('Moving to home.') - }, 3_000) + description: "Login successful ! \n You will be redirected.", + }); + setTimeout(() => { + 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() - setIsLoading(false) - return + setIsLoading(false); + return; } //toast.custom() - setTimeout(()=>{ - setIsLoading(false) + setTimeout(() => { + setIsLoading(false); //location.href = "/" - console.log('Moving to home.') - }, 5_000) + console.log("Moving to home."); + }, 5_000); }); }} fieldConfig={{