add: full auth functionality
add: zustand auth store add: jwt auto refresh
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { checkUserExists } from "@/lib/api/auth"
|
||||
import { checkUserExists } from "@/lib/api/auth/user-exists"
|
||||
import { useQueryClient } from "@tanstack/react-query"
|
||||
|
||||
export const useCheckUserExistsLazy = () => {
|
||||
@@ -7,7 +7,7 @@ export const useCheckUserExistsLazy = () => {
|
||||
const run = (phone: string) =>
|
||||
queryClient.fetchQuery({
|
||||
queryKey: ['checkUserExists', phone],
|
||||
queryFn: () => checkUserExists(phone),
|
||||
queryFn: () => checkUserExists({ phone }),
|
||||
})
|
||||
|
||||
return { run }
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { login } from '@/lib/api/auth'
|
||||
import { useAuthStore } from '@/zustand/userStore'
|
||||
import { useAuthStore } from '@/zustand/authStore'
|
||||
import { login } from '@/lib/api/auth/login'
|
||||
|
||||
export const useLogin = () =>
|
||||
useMutation({
|
||||
mutationFn: login,
|
||||
onSuccess: (data) => {
|
||||
useAuthStore.getState().login(data.user)
|
||||
useAuthStore.getState().login(data)
|
||||
},
|
||||
})
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { requestOtp } from "@/lib/api/auth/request-otp"
|
||||
import { useQueryClient } from "@tanstack/react-query"
|
||||
|
||||
export const useOtpRequest = () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const run = (phone: string) =>
|
||||
queryClient.fetchQuery({
|
||||
queryKey: ['requestOtp', phone],
|
||||
queryFn: () => requestOtp({ phone }),
|
||||
})
|
||||
|
||||
return { run }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { OtpValidationRequestModel, validateOtp } from "@/lib/api/auth/validate-otp"
|
||||
import { useQueryClient } from "@tanstack/react-query"
|
||||
|
||||
export const useOtpValidation = () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const run = (model: OtpValidationRequestModel) =>
|
||||
queryClient.fetchQuery({
|
||||
queryKey: ['requestOtpValidation', model],
|
||||
queryFn: () => validateOtp(model),
|
||||
})
|
||||
|
||||
return { run }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { useAuthStore } from '@/zustand/authStore'
|
||||
import { fetchMe } from '@/lib/api/auth/me'
|
||||
|
||||
export const useProfile = () =>
|
||||
useMutation({
|
||||
mutationFn: fetchMe,
|
||||
onSuccess: (data) => {
|
||||
useAuthStore.getState().update(data)
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,15 @@
|
||||
import { refreshToken } from "@/lib/api/auth/refresh-token"
|
||||
import { useAuthStore } from "@/zustand/authStore"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
|
||||
export const useOtpRequest = () => {
|
||||
useMutation({
|
||||
mutationFn: refreshToken,
|
||||
onSuccess: (data) => {
|
||||
useAuthStore.getState().setToken(data.accessToken)
|
||||
},
|
||||
onError: () => {
|
||||
useAuthStore.getState().logout();
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { signup } from '@/lib/api/auth'
|
||||
import { useAuthStore } from '@/zustand/userStore'
|
||||
import { signup } from '@/lib/api/auth/signup'
|
||||
import { useAuthStore } from '@/zustand/authStore'
|
||||
|
||||
export const useSignup = () =>
|
||||
useMutation({
|
||||
mutationFn: signup,
|
||||
onSuccess: (data) => {
|
||||
useAuthStore.getState().login(data.user)
|
||||
useAuthStore.getState().login(data)
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user