-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added auth context for login --------- Co-authored-by: Chris Torres <[email protected]>
- Loading branch information
1 parent
6d4bb48
commit fbff3e7
Showing
10 changed files
with
1,311 additions
and
1,055 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
37 changes: 30 additions & 7 deletions
37
src/components/GoogleSignOutButton/GoogleSignOutButton.tsx
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,56 @@ | ||
import React, { | ||
createContext, | ||
ReactNode, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
||
type AuthState = { | ||
isAuthenticated: boolean; | ||
setAuthenticated: (value: boolean) => Promise<void>; | ||
}; | ||
|
||
const AuthContext = createContext<AuthState | undefined>(undefined); | ||
|
||
export const AuthContextProvider = ({ children }: { children: ReactNode }) => { | ||
const [isAuthenticated, setIsAuthenticated] = useState(false); | ||
|
||
useEffect(() => { | ||
const syncAuthState = async () => { | ||
try { | ||
const storedAuth = await AsyncStorage.getItem('authenticated'); | ||
console.log('Auth state from storage:', storedAuth); | ||
setIsAuthenticated(storedAuth === 'true'); | ||
} catch (error) { | ||
console.error('Error loading authentication state:', error); | ||
} | ||
}; | ||
|
||
syncAuthState(); | ||
}, []); | ||
|
||
const setAuthenticated = async (value: boolean) => { | ||
try { | ||
await AsyncStorage.setItem('authenticated', value ? 'true' : 'false'); | ||
setIsAuthenticated(value); | ||
} catch (error) { | ||
console.error('Failed to update authentication state:', error); | ||
} | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={{ isAuthenticated, setAuthenticated }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuth = (): AuthState => { | ||
const context = useContext(AuthContext); | ||
if (!context) { | ||
throw new Error('useAuth must be used within an AuthProvider'); | ||
} | ||
return context; | ||
}; |
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