diff options
Diffstat (limited to 'src/apiSlice.ts')
-rw-r--r-- | src/apiSlice.ts | 38 |
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; |