Added biome for formatting, code formatted

This commit is contained in:
2024-05-08 16:50:25 +02:00
parent dc446fe8dc
commit e62ef8695b
15 changed files with 678 additions and 510 deletions

View File

@ -6,134 +6,134 @@ 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;
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,
});
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}`);
useEffect(() => {
// tohle se zavola jen poprve pri startu appky
async function loadToken() {
const token = await storageUtil.getItem(TOKEN_KEY);
console.log(`stored: ${token}`);
const resUser = await fetch(`${API_URL}/auth/status`, {
credentials: "include",
});
const resUser = await fetch(`${API_URL}/auth/status`, {
credentials: "include",
});
const userData = await resUser.json();
const userData = await resUser.json();
if (token && resUser.status == 200) {
setAuthState({
token: token,
authenticated: true,
user: userData.data,
});
if (token && resUser.status == 200) {
setAuthState({
token: token,
authenticated: true,
user: userData.data,
});
return;
}
setAuthState({
authenticated: false,
token: null,
user: null,
});
}
loadToken();
}, []);
return;
}
setAuthState({
authenticated: false,
token: null,
user: null,
});
}
loadToken();
}, []);
async function register(username, email, password) {
try {
const res = await fetch(`${API_URL}/auth/signup`, {
method: 'POST',
credentials: 'include',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username,
email,
password
})
})
return res;
} catch (err) {
return { error: true, msg: err.response.data };
}
}
async function register(username, email, password) {
try {
const res = await fetch(`${API_URL}/auth/signup`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username,
email,
password,
}),
});
return res;
} catch (err) {
return { error: true, msg: err.response.data };
}
}
async function login(email, password) {
try {
const resLogin = await fetch(`${API_URL}/auth/signin`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email,
password,
}),
});
async function login(email, password) {
try {
const resLogin = await fetch(`${API_URL}/auth/signin`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email,
password,
}),
});
const loginData = await resLogin.json();
const loginData = await resLogin.json();
const resUser = await fetch(`${API_URL}/auth/status`, {
credentials: "include",
});
const resUser = await fetch(`${API_URL}/auth/status`, {
credentials: "include",
});
if (resUser.status != 200) {
throw Error("user does not have user data");
}
if (resUser.status != 200) {
throw Error("user does not have user data");
}
const userData = await resUser.json();
const userData = await resUser.json();
setAuthState({
token: loginData.data.jwt,
authenticated: true,
user: userData.data,
});
setAuthState({
token: loginData.data.jwt,
authenticated: true,
user: userData.data,
});
await storageUtil.setItem(TOKEN_KEY, loginData.data.jwt);
} catch (err) {
console.error("Failed to log in", err)
return { error: true, msg: err.res };
}
}
await storageUtil.setItem(TOKEN_KEY, loginData.data.jwt);
} catch (err) {
console.error("Failed to log in", err);
return { error: true, msg: err.res };
}
}
async function logout() {
let res = await fetch(`${API_URL}/auth/logout`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
res = await res.json();
async function logout() {
let res = await fetch(`${API_URL}/auth/logout`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
res = await res.json();
await storageUtil.delItem(TOKEN_KEY);
await storageUtil.delItem(TOKEN_KEY);
setAuthState({
token: null,
authenticated: false,
user: null,
});
}
setAuthState({
token: null,
authenticated: false,
user: null,
});
}
const value = {
onSignin: register,
onLogin: login,
onLogout: logout,
authState,
};
const value = {
onSignin: register,
onLogin: login,
onLogout: logout,
authState,
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}

View File

@ -3,32 +3,32 @@ 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);
}
},
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;