From 68a86b2f9c41717767443b6b9e1860cb73b2aa30 Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Sat, 24 Feb 2024 10:04:10 -0600 Subject: Use RTK query --- src/apiSlice.ts | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/apiSlice.ts') 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({ + query: credentials => ({ + url: '/guest-login', + method: 'POST', + body: credentials + }) }) }) }); export const { useGetGuestsQuery, - useUpdateGuestMutation + useUpdateGuestMutation, + useLoginMutation } = apiSlice; -- cgit v1.2.3