add: full auth functionality

add: zustand auth store
add: jwt auto refresh
This commit is contained in:
Mahyar Khanbolooki
2025-07-01 22:49:56 +03:30
parent ef63706116
commit 48c458f012
22 changed files with 340 additions and 53 deletions
+24 -11
View File
@@ -1,7 +1,7 @@
'use client';
import React, { useState, type FormEvent, type ChangeEvent, useEffect } from 'react'
import { useAuthStore } from '@/zustand/userStore';
import { useAuthStore } from '@/zustand/authStore';
import { redirect } from 'next/navigation';
import StepEnterPassword from '@/features/auth/components/StepEnterPassword';
import StepEnterNumber from '@/features/auth/components/StepEnterNumber';
@@ -12,6 +12,8 @@ import StepNewPassword from '@/features/auth/components/StepNewPassword';
import { useLogin } from '@/hooks/auth/useLogin';
import { useSignup } from '@/hooks/auth/useSignup';
import { useCheckUserExistsLazy } from '@/hooks/auth/useCheckUserExists';
import { useOtpRequest } from '@/hooks/auth/useOtpRequest';
import { useOtpValidation } from '@/hooks/auth/useOtpValidation';
type Props = object
@@ -29,7 +31,9 @@ function AuthIndex({ }: Props) {
const isAuthenticated = useAuthStore((state) => state.isAuthenticated)
const loginMutation = useLogin()
const signupMutation = useSignup()
const checkUserExistsMuation = useCheckUserExistsLazy()
const { run: runUserExistCheck } = useCheckUserExistsLazy()
const { run: runOtpRequest } = useOtpRequest();
const { run: runOtpValidation } = useOtpValidation();
@@ -42,6 +46,7 @@ function AuthIndex({ }: Props) {
useEffect(() => {
if (step === AUTH_STEP.ENTER_OTP) {
console.log("REQUEST OTP")
runOtpRequest(number);
}
}, [step]);
@@ -67,7 +72,7 @@ function AuthIndex({ }: Props) {
}
}
const onClick = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
const onClick = async (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
e.stopPropagation();
const target = e.target as HTMLButtonElement;
@@ -82,7 +87,12 @@ function AuthIndex({ }: Props) {
}
else if (target.id === AUTH_PAGE_ELEMENT.RESEND_OTP) {
if (!timerRunning) {
restart();
try {
await runOtpRequest(number);
restart();
} catch {
}
}
}
};
@@ -91,7 +101,7 @@ function AuthIndex({ }: Props) {
e.preventDefault();
if (step == AUTH_STEP.ENTER_NUMBER) {
if (await checkUserExistsMuation.run(number)) {
if (await runUserExistCheck(number)) {
setStep(AUTH_STEP.ENTER_PASSWORD)
} else {
setStep(AUTH_STEP.ENTER_OTP);
@@ -107,13 +117,16 @@ function AuthIndex({ }: Props) {
}
}
else if (step == AUTH_STEP.ENTER_OTP) {
if (!await runOtpValidation({ phone: number, otp })) {
setOtp('');
console.log('Wrong otp');
}
stop();
setStep(AUTH_STEP.ENTER_NEW_PASSWORD)
// if (await validatePhoneNumber(number)) {
// setStep(AUTH_STEP.ENTER_RESET_PASSWORD)
// } else {
// setStep(AUTH_STEP.ENTER_NEW_PASSWORD)
// }
if (await runUserExistCheck(number)) {
setStep(AUTH_STEP.ENTER_RESET_PASSWORD)
} else {
setStep(AUTH_STEP.ENTER_NEW_PASSWORD)
}
}
else if (step == AUTH_STEP.ENTER_RESET_PASSWORD) {
console.log("Password changed")