Added: fully working signin/signup system with routing
This commit is contained in:
103
frontend/app/context/AuthContext.js
Normal file
103
frontend/app/context/AuthContext.js
Normal file
@ -0,0 +1,103 @@
|
||||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
import axios from "axios";
|
||||
import storageUtil from "./storage";
|
||||
|
||||
const TOKEN_KEY = "my-jwt";
|
||||
export const API_URL = "http://10.69.1.137:6060/api/v1";
|
||||
const AuthContext = createContext(null);
|
||||
|
||||
export function useAuth() {
|
||||
const authContext = useContext(AuthContext);
|
||||
if (authContext === undefined) {
|
||||
throw new Error("Context is outside of provider");
|
||||
}
|
||||
return authContext;
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }) {
|
||||
const [authState, setAuthState] = useState({
|
||||
token: null,
|
||||
authenticated: null,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
// tohle se zavola jen poprve pri startu appky
|
||||
async function loadToken() {
|
||||
const token = await storageUtil.getItem(TOKEN_KEY);
|
||||
console.log(`stored: ${token}`);
|
||||
|
||||
if (token) {
|
||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
|
||||
setAuthState({
|
||||
token: token,
|
||||
authenticated: true,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
setAuthState({
|
||||
authenticated: false,
|
||||
token: null,
|
||||
});
|
||||
}
|
||||
loadToken();
|
||||
}, []);
|
||||
|
||||
async function register(username, email, password) {
|
||||
try {
|
||||
const res = await axios.post(`${API_URL}/auth/signup`, {
|
||||
username,
|
||||
email,
|
||||
password,
|
||||
});
|
||||
return res
|
||||
} catch (err) {
|
||||
return { error: true, msg: err.response.data};
|
||||
}
|
||||
}
|
||||
|
||||
async function login(email, password) {
|
||||
try {
|
||||
const res = await axios.post(`${API_URL}/auth/signin`, {
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
setAuthState({
|
||||
token: res.data.data.jwt,
|
||||
authenticated: true,
|
||||
});
|
||||
|
||||
//axios.defaults.headers.common[
|
||||
// "Authorization"
|
||||
//] = `Bearer ${res.data.data.jwt}`;
|
||||
|
||||
await storageUtil.setItem(TOKEN_KEY, res.data.data.jwt);
|
||||
|
||||
return res
|
||||
} catch (err) {
|
||||
return { error: true, msg: err.res };
|
||||
}
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
await storageUtil.delItem(TOKEN_KEY);
|
||||
|
||||
axios.defaults.headers.common["Authorization"] = "";
|
||||
|
||||
setAuthState({
|
||||
token: null,
|
||||
authenticated: false,
|
||||
});
|
||||
}
|
||||
|
||||
const value = {
|
||||
onSignin: register,
|
||||
onLogin: login,
|
||||
onLogout: logout,
|
||||
authState,
|
||||
};
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
34
frontend/app/context/storage.js
Normal file
34
frontend/app/context/storage.js
Normal file
@ -0,0 +1,34 @@
|
||||
import * as SecureStore from "expo-secure-store";
|
||||
import { Platform } from "react-native";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
|
||||
const storageUtil = {
|
||||
setItem: async (k, v) => {
|
||||
if (Platform.OS === "web") {
|
||||
// web
|
||||
await AsyncStorage.setItem(k, v);
|
||||
} else {
|
||||
// mobile
|
||||
await SecureStore.setItemAsync(k, v.toString()); // v must be string,
|
||||
}
|
||||
},
|
||||
getItem: async (k) => {
|
||||
if (Platform.OS === "web") {
|
||||
// web
|
||||
return await AsyncStorage.getItem(k);
|
||||
} else {
|
||||
// mobile
|
||||
return await SecureStore.getItemAsync(k);
|
||||
}
|
||||
},
|
||||
delItem: async (k) => {
|
||||
if (Platform.OS === "web") {
|
||||
// web
|
||||
await AsyncStorage.removeItem(k);
|
||||
} else {
|
||||
// mobile
|
||||
await SecureStore.deleteItemAsync(k);
|
||||
}
|
||||
},
|
||||
};
|
||||
export default storageUtil;
|
Reference in New Issue
Block a user