1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import type { RootState } from '../../store';
import type { Credentials, AdminLogin } from '../../models';
export const adminSlice = createApi({
reducerPath: 'adminApi',
baseQuery: fetchBaseQuery({
baseUrl: import.meta.env.VITE_BASE_URL,
prepareHeaders: (headers, { getState }) => {
const token = (getState() as RootState).admin.token;
if (token) {
headers.set('authorization', `${token}`);
}
return headers;
},
}),
endpoints: (builder) => ({
loginAdmin: builder.mutation<AdminLogin, Credentials>({
query: (credentials) => ({
url: 'admin/login',
method: 'POST',
body: credentials,
}),
}),
}),
});
export const { useLoginAdminMutation } = adminSlice;
|