-
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.
feat(Frontend): Implemented mock Login/Logout
- Loading branch information
Showing
10 changed files
with
366 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import SecureStorage from "react-secure-storage"; | ||
|
||
const useSecureStorage = <T>(key: string, initialValue: string): [T, (value: T) => void] => { | ||
const [value, setValue] = useState<T>(() => { | ||
try { | ||
const storedValue = SecureStorage.getItem(key)?.toString(); | ||
return storedValue ? JSON.parse(storedValue) : initialValue; | ||
} catch (error) { | ||
console.error("Error retrieving from secure storage:", error); | ||
return initialValue; | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
try { | ||
if (value === null) { | ||
SecureStorage.removeItem(key); | ||
} else { | ||
SecureStorage.setItem(key, JSON.stringify(value)); | ||
} | ||
} catch (error) { | ||
console.error("Error saving to secure storage:", error); | ||
} | ||
}, [key, value]); | ||
|
||
const updateValue = (newData: T) => { | ||
console.log("updating value", newData); | ||
setValue(newData); | ||
}; | ||
|
||
return [value, updateValue]; | ||
}; | ||
|
||
export default useSecureStorage; |
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,82 @@ | ||
import { createContext, ReactNode } from 'react' | ||
import useSecureStorage from '../hooks/use-local-storage' | ||
|
||
type Props = { | ||
children?: ReactNode | ||
} | ||
|
||
interface AuthContextUser { | ||
firstName: string | ||
lastName: string | ||
email: string | ||
token: string | ||
refreshToken: string | ||
} | ||
|
||
interface IAuthContext { | ||
//authenticated: boolean; | ||
user: AuthContextUser | null | ||
login: ({ | ||
firstName, | ||
lastName, | ||
email, | ||
token, | ||
refreshToken, | ||
}: AuthContextUser) => void | ||
logout: () => void | ||
} | ||
|
||
const initialUser = { | ||
firstName: '', | ||
lastName: '', | ||
email: '', | ||
token: '', | ||
refreshToken: '', | ||
} | ||
|
||
const initialValue = { | ||
//authenticated: false, | ||
user: initialUser, | ||
login: ({ | ||
firstName, | ||
lastName, | ||
email, | ||
token, | ||
refreshToken, | ||
}: AuthContextUser) => {}, | ||
logout: () => {}, | ||
} | ||
|
||
const AuthContext = createContext<IAuthContext>(initialValue) | ||
|
||
const AuthProvider = ({ children }: Props) => { | ||
const [user, setUser] = useSecureStorage<AuthContextUser | null>('user', '') | ||
|
||
const login = ({ | ||
firstName, | ||
lastName, | ||
email, | ||
token, | ||
refreshToken, | ||
}: AuthContextUser) => { | ||
setUser({ | ||
firstName: firstName, | ||
lastName: lastName, | ||
email: email, | ||
token: token, | ||
refreshToken: refreshToken, | ||
}) | ||
} | ||
|
||
const logout = () => { | ||
setUser(null) | ||
} | ||
|
||
return ( | ||
<AuthContext.Provider value={{ user, login, logout }}> | ||
{children} | ||
</AuthContext.Provider> | ||
) | ||
} | ||
|
||
export { AuthContext, AuthProvider } |
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
Oops, something went wrong.