Skip to content

Commit

Permalink
Merge branch 'master' into 124-stripe-fetch-products
Browse files Browse the repository at this point in the history
  • Loading branch information
asun555 authored Mar 31, 2024
2 parents dddcad8 + f14ddb4 commit d2f30b4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
20 changes: 20 additions & 0 deletions client/src/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import { initializeApp, type FirebaseOptions } from "@firebase/app"
import { getAuth, connectAuthEmulator } from "@firebase/auth"
import { getFirestore, connectFirestoreEmulator } from "@firebase/firestore"
import { UserClaims } from "models/User"
import fetchClient from "services/OpenApiFetchClient"
import { StoreInstance } from "store/store"

const firebaseConfig: FirebaseOptions = {
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
Expand All @@ -22,4 +25,21 @@ if (import.meta.env.VITE_NODE_ENV !== "production") {
connectAuthEmulator(auth, "http://localhost:9099")
}

auth.onIdTokenChanged(async (user) => {
if (user === null) {
// suggests a log out
StoreInstance.actions.resetCurrentUserState()
return
}

try {
const { claims } = await user.getIdTokenResult()
const { data: userData } = await fetchClient.GET("/users/self")

StoreInstance.actions.setCurrentUser(user, userData, claims as UserClaims)
} catch (error) {
console.error(error)
}
})

export { auth, db }
8 changes: 8 additions & 0 deletions client/src/models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { components } from "./__generated__/schema"

export type UserAdditionalInfo = components["schemas"]["UserAdditionalInfo"]

export type UserClaims = {
admin?: boolean
member?: boolean
}
38 changes: 34 additions & 4 deletions client/src/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
import { User } from "firebase/auth"
import { UserAdditionalInfo, UserClaims } from "models/User"
import {
defaultRegistry,
createStore,
Action,
createContainer,
createHook
} from "react-sweet-state"

type State = { current: number }
type State = {
currentUser: User | null // firebase type
currentUserData?: UserAdditionalInfo
currentUserClaims?: UserClaims
}

const defaultUserState = {
currentUser: null,
currentUserClaims: undefined,
currentUserData: undefined
}

const initialState: State = {
current: 1000
...defaultUserState
}

const actions = {
loadInfo:
setCurrentUser:
(
user: User | null,
userData: UserAdditionalInfo | undefined,
userClaims: UserClaims | undefined
): Action<State> =>
({ setState }) => {
setState({
currentUser: user,
currentUserData: userData,
currentUserClaims: userClaims
})
},
resetCurrentUserState:
(): Action<State> =>
async ({ setState }) => {}
({ setState }) => {
setState({ ...defaultUserState })
}
}

type Actions = typeof actions
Expand All @@ -27,4 +55,6 @@ const Store = createStore<State, Actions>({
containedBy: AppDataContainer
})

export const StoreInstance = defaultRegistry.getStore(Store)

export const useAppData = createHook(Store)

0 comments on commit d2f30b4

Please sign in to comment.