summaryrefslogtreecommitdiff
path: root/src/apiSlice.ts
diff options
context:
space:
mode:
authorMichael Hunteman <michael@huntm.net>2024-02-24 10:04:10 -0600
committerMichael Hunteman <michael@huntm.net>2024-02-24 10:18:33 -0600
commit68a86b2f9c41717767443b6b9e1860cb73b2aa30 (patch)
tree33c8d6033a26ec70a1e116d2e1669f2dc12b3744 /src/apiSlice.ts
parent589e53f152d7363074049dfd1bd5a34286ae74d6 (diff)
Use RTK query
Diffstat (limited to 'src/apiSlice.ts')
-rw-r--r--src/apiSlice.ts38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/apiSlice.ts b/src/apiSlice.ts
index 6d779ed..fde9fff 100644
--- a/src/apiSlice.ts
+++ b/src/apiSlice.ts
@@ -1,8 +1,34 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
+import { setCredentials, logOut } from './features/auth/authSlice';
+
+export interface User {
+ firstName: string
+ lastName: string
+}
+
+export interface UserResponse {
+ user: User
+ token: string
+}
+
+export interface LoginRequest {
+ firstName: string
+ lastName: string
+}
export const apiSlice = createApi({
reducerPath: 'api',
- baseQuery: fetchBaseQuery({ baseUrl: 'http://localhost:3000' }),
+ baseQuery: fetchBaseQuery({
+ // baseUrl: 'http://localhost:3000',
+ baseUrl: '/',
+ prepareHeaders: (headers, { getState }) => {
+ const token = (getState() as RootState).auth.token;
+ if (token) {
+ headers.set('authorization', `Bearer ${token}`);
+ }
+ return headers;
+ }
+ }),
tagTypes: ['Guests'],
endpoints: builder => ({
getGuests: builder.query({
@@ -16,11 +42,19 @@ export const apiSlice = createApi({
body: guest,
providesTags: ['Guests']
})
+ }),
+ login: builder.mutation<UserResponse, LoginRequest>({
+ query: credentials => ({
+ url: '/guest-login',
+ method: 'POST',
+ body: credentials
+ })
})
})
});
export const {
useGetGuestsQuery,
- useUpdateGuestMutation
+ useUpdateGuestMutation,
+ useLoginMutation
} = apiSlice;