-
Notifications
You must be signed in to change notification settings - Fork 5
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
55cce5f
commit ac559e6
Showing
9 changed files
with
176 additions
and
55 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
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,45 +1,67 @@ | ||
import { | ||
onAuthStateChanged, | ||
signInWithEmailAndPassword, | ||
signOut as firebaseSignOut, | ||
type User, | ||
} from 'firebase/auth'; | ||
import { create } from 'zustand'; | ||
|
||
import { auth } from '../../api/common/firebase'; // Ensure this imports your Firebase instance | ||
import { createSelectors } from '../utils'; | ||
import type { TokenType } from './utils'; | ||
import { getToken, removeToken, setToken } from './utils'; | ||
|
||
interface AuthState { | ||
token: TokenType | null; | ||
user: User | null; | ||
status: 'idle' | 'signOut' | 'signIn'; | ||
signIn: (data: TokenType) => void; | ||
signOut: () => void; | ||
signIn: (email: string, password: string) => Promise<void>; | ||
signOut: () => Promise<void>; | ||
hydrate: () => void; | ||
} | ||
|
||
const _useAuth = create<AuthState>((set, get) => ({ | ||
const _useAuth = create<AuthState>((set) => ({ | ||
user: null, | ||
status: 'idle', | ||
token: null, | ||
signIn: (token) => { | ||
setToken(token); | ||
set({ status: 'signIn', token }); | ||
|
||
signIn: async (email, password) => { | ||
try { | ||
const userCredential = await signInWithEmailAndPassword( | ||
auth, | ||
email, | ||
password, | ||
); | ||
set({ user: userCredential.user, status: 'signIn' }); | ||
} catch (error) { | ||
console.error('SignIn Error:', error); | ||
throw error; | ||
} | ||
}, | ||
signOut: () => { | ||
removeToken(); | ||
set({ status: 'signOut', token: null }); | ||
|
||
signOut: async () => { | ||
try { | ||
await firebaseSignOut(auth); | ||
set({ user: null, status: 'signOut' }); | ||
} catch (error) { | ||
console.error('SignOut Error:', error); | ||
} | ||
}, | ||
|
||
hydrate: () => { | ||
try { | ||
const userToken = getToken(); | ||
if (userToken !== null) { | ||
get().signIn(userToken); | ||
set({ status: 'idle' }); | ||
|
||
// Subscribe to Firebase auth state changes | ||
onAuthStateChanged(auth, (user) => { | ||
if (user) { | ||
set({ user, status: 'signIn' }); | ||
} else { | ||
get().signOut(); | ||
set({ user: null, status: 'signOut' }); | ||
} | ||
} catch (e) { | ||
// catch error here | ||
// Maybe sign_out user! | ||
} | ||
}); | ||
}, | ||
})); | ||
|
||
export const useAuth = createSelectors(_useAuth); | ||
|
||
// Functions for easier usage in components | ||
export const signOut = () => _useAuth.getState().signOut(); | ||
export const signIn = (token: TokenType) => _useAuth.getState().signIn(token); | ||
export const signIn = (email: string, password: string) => | ||
_useAuth.getState().signIn(email, password); | ||
export const hydrateAuth = () => _useAuth.getState().hydrate(); |
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
Oops, something went wrong.