-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: session state * export * fix: use session metadata * fix: export session slice * indexes * api not func * fix: rename env to settings * fix: unused arg * reinstate auth utils
- Loading branch information
Showing
12 changed files
with
131 additions
and
42 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { type EndpointBuilder, type Api } from "@reduxjs/toolkit/query/react" | ||
|
||
import { login, logout } from "../../slices/session" | ||
|
||
export function buildLoginEndpoint<ResultType, QueryArg>( | ||
build: EndpointBuilder<any, any, any>, | ||
url: string = "session/login/", | ||
) { | ||
return build.mutation<ResultType, QueryArg>({ | ||
query: body => ({ url, method: "POST", body }), | ||
async onQueryStarted(_, { dispatch, queryFulfilled }) { | ||
try { | ||
await queryFulfilled | ||
dispatch(login()) | ||
} catch (error) { | ||
console.error("Failed to call login endpoint...", error) | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
export function buildLogoutEndpoint<ResultType, QueryArg>( | ||
api: Api<any, any, any, any, any>, | ||
build: EndpointBuilder<any, any, any>, | ||
url: string = "session/logout/", | ||
) { | ||
return build.mutation<ResultType, QueryArg>({ | ||
query: () => ({ url, method: "POST" }), | ||
async onQueryStarted(_, { dispatch, queryFulfilled }) { | ||
try { | ||
await queryFulfilled | ||
} catch (error) { | ||
console.error("Failed to call logout endpoint...", error) | ||
} finally { | ||
dispatch(logout()) | ||
dispatch(api.util.resetApiState()) | ||
} | ||
}, | ||
}) | ||
} |
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 @@ | ||
export * from "./session" |
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,16 @@ | ||
import { type Middleware, isAction } from "@reduxjs/toolkit" | ||
|
||
import { logout } from "../utils/auth" | ||
|
||
export const logoutMiddleware: Middleware = _ => next => action => { | ||
const response = next(action) | ||
|
||
// The backend should delete these cookie upon calling the logout endpoint. | ||
// However, as a precaution, we also delete the session cookies in case the | ||
// backend fails to delete the cookies. | ||
if (isAction(action) && action.type === "session/logout") { | ||
logout() | ||
} | ||
|
||
return response | ||
} |
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,8 @@ | ||
import { asyncThunkCreator, buildCreateSlice } from "@reduxjs/toolkit" | ||
|
||
// `buildCreateSlice` allows us to create a slice with async thunks. | ||
const createSlice = buildCreateSlice({ | ||
creators: { asyncThunk: asyncThunkCreator }, | ||
}) | ||
|
||
export default createSlice |
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,2 @@ | ||
export { default as createSlice } from "./createSlice" | ||
export { default as sessionSlice, type SessionState } from "./session" |
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,32 @@ | ||
import Cookies from "js-cookie" | ||
|
||
import { SESSION_METADATA_COOKIE_NAME } from "../settings" | ||
import createSlice from "./createSlice" | ||
|
||
export interface SessionState { | ||
isLoggedIn: boolean | ||
} | ||
|
||
const initialState: SessionState = { | ||
isLoggedIn: Boolean(Cookies.get(SESSION_METADATA_COOKIE_NAME)), | ||
} | ||
|
||
const sessionSlice = createSlice({ | ||
name: "session", | ||
initialState, | ||
reducers: create => ({ | ||
login: create.reducer(state => { | ||
state.isLoggedIn = true | ||
}), | ||
logout: create.reducer(state => { | ||
state.isLoggedIn = false | ||
}), | ||
}), | ||
selectors: { | ||
selectIsLoggedIn: session => session.isLoggedIn, | ||
}, | ||
}) | ||
|
||
export default sessionSlice | ||
export const { login, logout } = sessionSlice.actions | ||
export const { selectIsLoggedIn } = sessionSlice.selectors |
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 |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import Cookies from "js-cookie" | ||
|
||
import { | ||
SESSION_COOKIE_NAME, | ||
SESSION_METADATA_COOKIE_NAME, | ||
CSRF_COOKIE_NAME, | ||
} from "../settings" | ||
|
||
export function logout() { | ||
Cookies.remove("session_key") | ||
Cookies.remove("session_metadata") | ||
Cookies.remove(SESSION_COOKIE_NAME) | ||
Cookies.remove(SESSION_METADATA_COOKIE_NAME) | ||
} | ||
|
||
// https://docs.djangoproject.com/en/3.2/ref/csrf/ | ||
export function getCsrfCookie() { | ||
return Cookies.get(`${import.meta.env.VITE_SERVICE_NAME}_csrftoken`) | ||
return Cookies.get(CSRF_COOKIE_NAME) | ||
} |