feat: add staff and user authentication APIs with request handling
This commit is contained in:
parent
1b4ff4c3e7
commit
ba19a4dc5b
21
app/apis/admin/get-staff.ts
Normal file
21
app/apis/admin/get-staff.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
|
const staffSchema = z.object({
|
||||||
|
data: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
email: z.string(),
|
||||||
|
username: z.string(),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
export const getStaff = async (parameters: THttpServer) => {
|
||||||
|
try {
|
||||||
|
const { data } = await HttpServer(parameters).get(`/api/staff/profile`)
|
||||||
|
return staffSchema.parse(data)
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
20
app/apis/admin/login-staff.ts
Normal file
20
app/apis/admin/login-staff.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import { type TLoginSchema } from '~/layouts/news/form-login'
|
||||||
|
import { HttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
|
const loginResponseSchema = z.object({
|
||||||
|
data: z.object({
|
||||||
|
token: z.string(),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
export const staffLoginRequest = async (payload: TLoginSchema) => {
|
||||||
|
try {
|
||||||
|
const { data } = await HttpServer().post('/api/staff/login', payload)
|
||||||
|
return loginResponseSchema.parse(data)
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,7 +9,7 @@ const loginResponseSchema = z.object({
|
|||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const newsLoginRequest = async (payload: TLoginSchema) => {
|
export const userLoginRequest = async (payload: TLoginSchema) => {
|
||||||
try {
|
try {
|
||||||
const { data } = await HttpServer().post('/api/user/login', payload)
|
const { data } = await HttpServer().post('/api/user/login', payload)
|
||||||
return loginResponseSchema.parse(data)
|
return loginResponseSchema.parse(data)
|
||||||
@ -9,7 +9,7 @@ const loginResponseSchema = z.object({
|
|||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const newsRegisterRequest = async (payload: TRegisterSchema) => {
|
export const userRegisterRequest = async (payload: TRegisterSchema) => {
|
||||||
try {
|
try {
|
||||||
const { subscribe_plan, ...restPayload } = payload
|
const { subscribe_plan, ...restPayload } = payload
|
||||||
const transformedPayload = {
|
const transformedPayload = {
|
||||||
@ -1,7 +1,26 @@
|
|||||||
import { Outlet } from 'react-router'
|
import { Outlet } from 'react-router'
|
||||||
|
|
||||||
|
import { getStaff } from '~/apis/admin/get-staff'
|
||||||
import { AdminProvider } from '~/contexts/admin'
|
import { AdminProvider } from '~/contexts/admin'
|
||||||
import { AdminDefaultLayout } from '~/layouts/admin/default'
|
import { AdminDefaultLayout } from '~/layouts/admin/default'
|
||||||
|
import { handleCookie } from '~/libs/cookies'
|
||||||
|
|
||||||
|
import type { Route } from './+types/_admin.lg-admin'
|
||||||
|
|
||||||
|
export const loader = async ({ request }: Route.LoaderArgs) => {
|
||||||
|
const { adminToken } = await handleCookie(request)
|
||||||
|
let adminData
|
||||||
|
if (adminToken) {
|
||||||
|
const { data } = await getStaff({
|
||||||
|
accessToken: adminToken,
|
||||||
|
})
|
||||||
|
adminData = data
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
adminData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const AdminLayout = () => {
|
const AdminLayout = () => {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import { getValidatedFormData } from 'remix-hook-form'
|
|||||||
import { XiorError } from 'xior'
|
import { XiorError } from 'xior'
|
||||||
|
|
||||||
import { getUser } from '~/apis/news/get-user'
|
import { getUser } from '~/apis/news/get-user'
|
||||||
import { newsLoginRequest } from '~/apis/news/login'
|
import { userLoginRequest } from '~/apis/news/login-user'
|
||||||
import { loginSchema, type TLoginSchema } from '~/layouts/news/form-login'
|
import { loginSchema, type TLoginSchema } from '~/layouts/news/form-login'
|
||||||
import { generateTokenCookie } from '~/utils/token'
|
import { generateTokenCookie } from '~/utils/token'
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ export const action = async ({ request }: Route.ActionArgs) => {
|
|||||||
return data({ success: false, errors, defaultValues }, { status: 400 })
|
return data({ success: false, errors, defaultValues }, { status: 400 })
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: loginData } = await newsLoginRequest(payload)
|
const { data: loginData } = await userLoginRequest(payload)
|
||||||
const { token } = loginData
|
const { token } = loginData
|
||||||
const { data: userData } = await getUser({
|
const { data: userData } = await getUser({
|
||||||
accessToken: token,
|
accessToken: token,
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import { getValidatedFormData } from 'remix-hook-form'
|
|||||||
import { XiorError } from 'xior'
|
import { XiorError } from 'xior'
|
||||||
|
|
||||||
import { getUser } from '~/apis/news/get-user'
|
import { getUser } from '~/apis/news/get-user'
|
||||||
import { newsRegisterRequest } from '~/apis/news/register'
|
import { userRegisterRequest } from '~/apis/news/register-user'
|
||||||
import {
|
import {
|
||||||
registerSchema,
|
registerSchema,
|
||||||
type TRegisterSchema,
|
type TRegisterSchema,
|
||||||
@ -29,7 +29,7 @@ export const action = async ({ request }: Route.ActionArgs) => {
|
|||||||
return data({ success: false, errors, defaultValues }, { status: 400 })
|
return data({ success: false, errors, defaultValues }, { status: 400 })
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: registerData } = await newsRegisterRequest(payload)
|
const { data: registerData } = await userRegisterRequest(payload)
|
||||||
const { token } = registerData
|
const { token } = registerData
|
||||||
const { data: userData } = await getUser({
|
const { data: userData } = await getUser({
|
||||||
accessToken: token,
|
accessToken: token,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user