-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
4 changed files
with
153 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"use client"; | ||
|
||
import { | ||
type ReactNode, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
} from "react"; | ||
import { type StoreApi, useStore } from "zustand"; | ||
import { | ||
type UserDataStore, | ||
createUserDataStore, | ||
defaultUserStoreData, | ||
initUserDataStore, | ||
} from "@/lib/stores/user/userStore"; | ||
|
||
export const UserDataStoreContext = | ||
createContext<StoreApi<UserDataStore> | null>(null); | ||
|
||
export interface UserDataStoreProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const UserDataStoreProvider = ({ | ||
children, | ||
}: UserDataStoreProviderProps) => { | ||
const storeRef = useRef<StoreApi<UserDataStore>>(); | ||
|
||
if (!storeRef.current) { | ||
storeRef.current = createUserDataStore(defaultUserStoreData); | ||
} | ||
|
||
useEffect(() => { | ||
initUserDataStore().then((data) => { | ||
storeRef.current?.setState(data); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<UserDataStoreContext.Provider value={storeRef.current}> | ||
{children} | ||
</UserDataStoreContext.Provider> | ||
); | ||
}; | ||
|
||
export const useUserDataStore = <T,>( | ||
selector: (store: UserDataStore) => T | ||
): T => { | ||
const counterStoreContext = useContext(UserDataStoreContext); | ||
|
||
if (!counterStoreContext) { | ||
throw new Error( | ||
`useUserDataStore must be use within UserDataStoreProvider` | ||
); | ||
} | ||
|
||
return useStore(counterStoreContext, selector); | ||
}; |
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,80 @@ | ||
import { create } from "zustand"; | ||
import { auth } from "@/lib/utils/firebase/config"; | ||
|
||
type UserBasicData = { | ||
name?: string; | ||
email: string; | ||
emailVerified?: boolean; | ||
uid: string; | ||
}; | ||
|
||
type UserDataStoreState = { | ||
userData?: UserBasicData; | ||
isAuthenticated: boolean; | ||
isEmailVerified: boolean; | ||
}; | ||
|
||
type UserDataStoreActions = { | ||
setIsEmailVerified: (value: boolean) => void; | ||
verifyAndSetEmailVerified: (emailAlreadyVerified?: boolean) => Promise<void>; | ||
setIsAuthenticated: (value: boolean) => void; | ||
setUserData: (data: UserBasicData) => void; | ||
fetchAndSetUserData: () => Promise<void>; | ||
}; | ||
|
||
type UserDataStore = UserDataStoreState & UserDataStoreActions; | ||
|
||
const defaultUserStoreData: UserDataStoreState = { | ||
isAuthenticated: false, | ||
isEmailVerified: false, | ||
userData: undefined, | ||
}; | ||
|
||
const initUserDataStore = async (): Promise<UserDataStoreState> => { | ||
await auth.authStateReady(); | ||
const user = auth.currentUser; | ||
if (user) { | ||
return { | ||
userData: { | ||
email: user.email || "", | ||
emailVerified: user.emailVerified, | ||
uid: user.uid, | ||
}, | ||
isEmailVerified: user.emailVerified, | ||
isAuthenticated: true, | ||
}; | ||
} else { | ||
return defaultUserStoreData; | ||
} | ||
}; | ||
|
||
const createUserDataStore = ( | ||
initState: UserDataStoreState = defaultUserStoreData | ||
) => { | ||
return create<UserDataStore>((set) => ({ | ||
...initState, | ||
setIsAuthenticated: (value: boolean) => set({ isAuthenticated: value }), | ||
setIsEmailVerified: (value: boolean) => set({ isEmailVerified: value }), | ||
setUserData: (data: UserBasicData) => set({ userData: data }), | ||
fetchAndSetUserData: async () => { | ||
const data = await initUserDataStore(); | ||
set(data); | ||
}, | ||
verifyAndSetEmailVerified: async (emailAlreadyVerified?: boolean) => { | ||
if (emailAlreadyVerified) { | ||
return; | ||
} | ||
await auth.authStateReady(); | ||
const user = auth.currentUser; | ||
if (user) { | ||
set({ isEmailVerified: user.emailVerified }); | ||
} else { | ||
set({ isEmailVerified: false }); | ||
} | ||
}, | ||
resetData: () => set({ ...defaultUserStoreData }), | ||
})); | ||
}; | ||
|
||
export { initUserDataStore, createUserDataStore, defaultUserStoreData }; | ||
export type { UserDataStore }; |