feat: client testing api , add ouath2

This commit is contained in:
Abdalhamid Alhamad
2025-01-16 13:35:22 +03:00
parent 87bb1a2709
commit ebd4b293e9
11 changed files with 160 additions and 52 deletions

View File

@ -1,11 +1,11 @@
import React, { createContext, useContext, useState, useCallback } from 'react';
import React, { createContext, useCallback, useContext, useState } from 'react';
import { authApi } from '../api/client';
import { User, LoginResponse } from '../types/auth';
import { LoginRequest, LoginResponse, User } from '../types/auth';
interface AuthContextType {
isAuthenticated: boolean;
user: User | null;
login: (email: string, password: string) => Promise<void>;
login: (loginRequest: LoginRequest) => Promise<void>;
logout: () => void;
register: (countryCode: string, phoneNumber: string) => Promise<void>;
verifyOtp: (countryCode: string, phoneNumber: string, otp: string) => Promise<string>;
@ -27,9 +27,9 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [user, setUser] = useState<User | null>(null);
const login = useCallback(async (email: string, password: string) => {
const login = useCallback(async (loginRequest: LoginRequest) => {
try {
const response = await authApi.login(email, password);
const response = await authApi.login(loginRequest);
const loginData = response.data.data as LoginResponse;
setUser(loginData.user);
// Store tokens
@ -76,7 +76,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
console.log('Access token:', accessToken);
// Store token in localStorage immediately
localStorage.setItem('accessToken', accessToken);
setRegistrationData(prev => ({ ...prev, token: accessToken }));
setRegistrationData((prev) => ({ ...prev, token: accessToken }));
return accessToken;
} catch (error) {
console.error('OTP verification failed:', error);
@ -87,7 +87,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const setEmail = useCallback(async (email: string) => {
try {
await authApi.setEmail(email);
setRegistrationData(prev => ({ ...prev, email }));
setRegistrationData((prev) => ({ ...prev, email }));
} catch (error) {
console.error('Setting email failed:', error);
throw error;
@ -112,7 +112,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
register,
verifyOtp,
setEmail,
setPasscode
setPasscode,
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;