-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fe7a9f
commit a4e993d
Showing
13 changed files
with
232 additions
and
13 deletions.
There are no files selected for viewing
File renamed without changes
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createSlice } from '@reduxjs/toolkit' | ||
import type { PayloadAction } from '@reduxjs/toolkit' | ||
import {User} from "./services/auth"; | ||
import {RootState} from "./store"; | ||
|
||
type AuthState = { | ||
user: User | null | ||
token: string | null | ||
} | ||
|
||
const slice = createSlice({ | ||
name: 'auth', | ||
initialState: { user: null, token: null } as AuthState, | ||
reducers: { | ||
setCredentials: ( | ||
state, | ||
{ | ||
payload: { user, token }, | ||
}: PayloadAction<{ user: User; token: string }>, | ||
) => { | ||
state.user = user | ||
state.token = token | ||
}, | ||
}, | ||
}) | ||
|
||
export const { setCredentials } = slice.actions | ||
|
||
export default slice.reducer | ||
|
||
export const selectCurrentUser = (state: RootState) => state.auth.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' | ||
import { RootState } from '../store' | ||
|
||
export interface User { | ||
first_name: string | ||
last_name: string | ||
} | ||
|
||
export interface UserResponse { | ||
user: User | ||
token: string | ||
} | ||
|
||
export interface LoginRequest { | ||
username: string | ||
password: string | ||
} | ||
|
||
export const api = createApi({ | ||
baseQuery: fetchBaseQuery({ | ||
baseUrl: '/', | ||
prepareHeaders: (headers, { getState }) => { | ||
// By default, if we have a token in the store, let's use that for authenticated requests | ||
const token = (getState() as RootState).auth.token | ||
if (token) { | ||
headers.set('authorization', `Bearer ${token}`) | ||
} | ||
return headers | ||
}, | ||
}), | ||
endpoints: (builder) => ({ | ||
login: builder.mutation<UserResponse, void>({ | ||
query: (credentials) => ({ | ||
url: 'login', | ||
method: 'POST', | ||
body: credentials, | ||
}), | ||
}), | ||
protected: builder.mutation<{ message: string }, void>({ | ||
query: () => 'protected', | ||
}), | ||
}), | ||
}) | ||
|
||
export const { useLoginMutation, useProtectedMutation } = api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters