Add onboarding screen + token check
This commit is contained in:
parent
b951d0a0bc
commit
f5c59f602a
19 changed files with 433 additions and 279 deletions
31
context/AppProvider.tsx
Normal file
31
context/AppProvider.tsx
Normal file
|
@ -0,0 +1,31 @@
|
|||
import {createContext, PropsWithChildren, useContext} from "react";
|
||||
import {useStorageState} from '@/context/UseStorageState';
|
||||
|
||||
type TokenType = {
|
||||
token: string | null;
|
||||
setToken: (token: string | null) => void;
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
const TokenContext = createContext<TokenType>({
|
||||
setToken: () => {
|
||||
},
|
||||
token: null,
|
||||
isLoading: true,
|
||||
});
|
||||
|
||||
export const useToken = () => useContext(TokenContext);
|
||||
|
||||
export function AppProvider({ children }: PropsWithChildren) {
|
||||
const [[isLoading, token], setSession] = useStorageState('appToken');
|
||||
|
||||
const tokenContext: TokenType = {
|
||||
token,
|
||||
setToken: (token) => {
|
||||
setSession(token);
|
||||
},
|
||||
isLoading,
|
||||
};
|
||||
|
||||
return <TokenContext.Provider value={tokenContext}>{children}</TokenContext.Provider>;
|
||||
}
|
67
context/UseStorageState.tsx
Normal file
67
context/UseStorageState.tsx
Normal file
|
@ -0,0 +1,67 @@
|
|||
import * as SecureStore from 'expo-secure-store';
|
||||
import * as React from 'react';
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
type UseStateHook<T> = [[boolean, T | null], (value: T | null) => void];
|
||||
|
||||
function useAsyncState<T>(
|
||||
initialValue: [boolean, T | null] = [true, null],
|
||||
): UseStateHook<T> {
|
||||
return React.useReducer(
|
||||
(state: [boolean, T | null], action: T | null = null): [boolean, T | null] => [false, action],
|
||||
initialValue
|
||||
) as UseStateHook<T>;
|
||||
}
|
||||
|
||||
export async function setStorageItemAsync(key: string, value: string | null) {
|
||||
if (Platform.OS === 'web') {
|
||||
try {
|
||||
if (value === null) {
|
||||
localStorage.removeItem(key);
|
||||
} else {
|
||||
localStorage.setItem(key, value);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Local storage is unavailable:', e);
|
||||
}
|
||||
} else {
|
||||
if (value == null) {
|
||||
await SecureStore.deleteItemAsync(key);
|
||||
} else {
|
||||
await SecureStore.setItemAsync(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function useStorageState(key: string): UseStateHook<string> {
|
||||
// Public
|
||||
const [state, setState] = useAsyncState<string>();
|
||||
|
||||
// Get
|
||||
React.useEffect(() => {
|
||||
if (Platform.OS === 'web') {
|
||||
try {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
setState(localStorage.getItem(key));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Local storage is unavailable:', e);
|
||||
}
|
||||
} else {
|
||||
SecureStore.getItemAsync(key).then((value: string | null) => {
|
||||
setState(value);
|
||||
});
|
||||
}
|
||||
}, [key]);
|
||||
|
||||
// Set
|
||||
const setValue = React.useCallback(
|
||||
(value: string | null) => {
|
||||
setState(value);
|
||||
setStorageItemAsync(key, value);
|
||||
},
|
||||
[key]
|
||||
);
|
||||
|
||||
return [state, setValue];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue