first commit

This commit is contained in:
@jcarymuhammedow
2026-05-02 18:54:39 +05:00
commit f1f65138ea
45 changed files with 10608 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
import type { AuthUser } from '@/app/store/authStore'
export interface LoginRequest {
email: string
password: string
}
export interface LoginResponse {
token: string
user: AuthUser
}
// Mock credentials
const MOCK_USERS: Array<{ email: string; password: string; user: AuthUser; token: string }> = [
{
email: 'admin@tbbank.tm',
password: 'Admin123!',
token: 'mock-jwt-token-admin-xyz123',
user: {
id: '1',
name: 'Admin User',
email: 'admin@tbbank.tm',
role: 'admin',
},
},
{
email: 'manager@tbbank.tm',
password: 'Manager123!',
token: 'mock-jwt-token-manager-abc456',
user: {
id: '2',
name: 'Branch Manager',
email: 'manager@tbbank.tm',
role: 'manager',
},
},
]
function delay(ms: number) {
return new Promise<void>((resolve) => setTimeout(resolve, ms))
}
export async function loginApi(credentials: LoginRequest): Promise<LoginResponse> {
// Simulate network latency
await delay(900)
const match = MOCK_USERS.find(
(u) => u.email === credentials.email && u.password === credentials.password,
)
if (!match) {
throw new Error('INVALID_CREDENTIALS')
}
return {
token: match.token,
user: match.user,
}
}
export async function logoutApi(): Promise<void> {
await delay(300)
}

View File

@@ -0,0 +1,27 @@
import { useMutation } from '@tanstack/react-query'
import { useNavigate } from 'react-router-dom'
import { useAuthStore } from '@/app/store/authStore'
import { loginApi, type LoginRequest } from '../api/authApi'
export function useLogin() {
const navigate = useNavigate()
const setAuth = useAuthStore((s) => s.setAuth)
return useMutation({
mutationFn: (credentials: LoginRequest) => loginApi(credentials),
onSuccess: ({ token, user }) => {
setAuth(token, user)
navigate('/dashboard', { replace: true })
},
})
}
export function useLogout() {
const clearAuth = useAuthStore((s) => s.clearAuth)
const navigate = useNavigate()
return () => {
clearAuth()
navigate('/login', { replace: true })
}
}