matiru
Blockchain-Based Supply Chain Transparency for Agricultural Producegit clone https://github.com/maydayv7/matiru
frontend/AuthContext.js (892B)
1import { createContext, useEffect, useState } from "react";
2import AsyncStorage from "@react-native-async-storage/async-storage";
3
4export const AuthContext = createContext();
5
6export function AuthProvider({ children }) {
7 const [user, setUser] = useState(null);
8
9 useEffect(() => {
10 (async () => {
11 const raw = await AsyncStorage.getItem("matiru_session");
12 if (raw) {
13 try {
14 setUser(JSON.parse(raw));
15 } catch (e) {
16 console.log(e);
17 }
18 }
19 })();
20 }, []);
21
22 const login = async (session) => {
23 setUser(session);
24 await AsyncStorage.setItem("matiru_session", JSON.stringify(session));
25 };
26
27 const logout = async () => {
28 setUser(null);
29 await AsyncStorage.removeItem("matiru_session");
30 };
31
32 return (
33 <AuthContext.Provider value={{ user, login, logout }}>
34 {children}
35 </AuthContext.Provider>
36 );
37}