chore: initial import for test contour
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
||||
import { API_BASE_URL } from '@/config/api'
|
||||
|
||||
export const authHeader = () => {
|
||||
const token = localStorage.getItem('token')
|
||||
return token ? { Authorization: `Bearer ${token}` } : {}
|
||||
}
|
||||
|
||||
export const API = createApi({
|
||||
reducerPath: 'API',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: API_BASE_URL,
|
||||
credentials: 'include',
|
||||
}),
|
||||
endpoints: (builder) => ({
|
||||
login: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: '/user/login',
|
||||
method: 'POST',
|
||||
body: data,
|
||||
}),
|
||||
async onQueryStarted(arg, { queryFulfilled }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled
|
||||
console.log(data)
|
||||
localStorage.setItem('token', data.token)
|
||||
localStorage.setItem('admSovamedUserUID', data.user.uid)
|
||||
localStorage.setItem('admSovamedUserRegionId', data.user.regionId)
|
||||
} catch {
|
||||
//
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
logout: builder.mutation({
|
||||
async queryFn(_arg, _api, _extraOptions, _baseQuery) {
|
||||
localStorage.removeItem('token')
|
||||
return { data: undefined }
|
||||
},
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
export const { useLoginMutation, useLogoutMutation } = API;
|
||||
Reference in New Issue
Block a user