-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-auth): add retrieval of auth status on startup
- Loading branch information
1 parent
2af1267
commit a615b3c
Showing
8 changed files
with
101 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { reducer, actions, name } from "./slice"; | ||
import saga from "./saga"; | ||
|
||
export { reducer, actions, name, saga }; |
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,28 @@ | ||
import { call, put, takeEvery } from "redux-saga/effects"; | ||
import { actions, User } from "./slice"; | ||
import firebase from "firebase/app"; | ||
import "firebase/auth"; | ||
|
||
function getCurrentUser(): Promise<User | null> { | ||
return new Promise<User | null>((resolve) => { | ||
firebase.auth().onAuthStateChanged((user) => { | ||
if (user) { | ||
resolve({ | ||
id: user.uid, | ||
email: user.email!, | ||
}); | ||
} else { | ||
resolve(null); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function* initializeAuthSaga() { | ||
const user: User | null = yield call(getCurrentUser); | ||
yield put(actions.setLoggedUser(user)); | ||
} | ||
|
||
export default function* authSaga() { | ||
yield takeEvery(actions.initialize, initializeAuthSaga); | ||
} |
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,49 @@ | ||
import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit"; | ||
|
||
export interface User { | ||
id: string; | ||
email: string; | ||
} | ||
|
||
export interface State { | ||
isInitialized: boolean; | ||
loggedUser: User | null; | ||
} | ||
|
||
const initialState = { | ||
isInitialized: false, | ||
loggedUser: null, | ||
} as State; | ||
|
||
const slice = createSlice({ | ||
name: "auth", | ||
initialState: initialState, | ||
reducers: { | ||
setLoggedUser(state, action: PayloadAction<User | null>) { | ||
state.isInitialized = true; | ||
state.loggedUser = action.payload; | ||
}, | ||
initialize(state, action: PayloadAction<undefined>) { | ||
state.isInitialized = false; | ||
state.loggedUser = null; | ||
}, | ||
}, | ||
}); | ||
|
||
const getCurrentUser = createSelector( | ||
(state: { [name]: State }) => state[name], | ||
(authState) => authState.loggedUser | ||
); | ||
|
||
const isInitialized = createSelector( | ||
(state: { [name]: State }) => state[name], | ||
(authState) => authState.isInitialized | ||
); | ||
|
||
export const reducer = slice.reducer; | ||
export const actions = slice.actions; | ||
export const name = slice.name; | ||
export const selectors = { | ||
getCurrentUser, | ||
isInitialized, | ||
}; |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import {name as i18nName, reducer as i18nReducer} from '@pipeline/i18n'; | ||
import { name as i18nName, reducer as i18nReducer } from "@pipeline/i18n"; | ||
import { name as authName, reducer as authReducer } from "@pipeline/auth"; | ||
|
||
const reducers = { | ||
[i18nName]: i18nReducer | ||
[i18nName]: i18nReducer, | ||
[authName]: authReducer, | ||
}; | ||
|
||
export default reducers; |
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