This commit is contained in:
Erik 2022-03-21 23:42:19 +02:00
parent 580b15da00
commit 5e6d16d101
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589
3 changed files with 59 additions and 5 deletions

View File

@ -0,0 +1,10 @@
import React from 'react';
import { Navigate, useLocation } from "react-router-dom";
import { getUser } from "../util/Util";
export const PrivateRoute = ({children}) => {
const user = getUser();
const location = useLocation();
if (!user) return <Navigate to='/login' replace state={{from:location}} />;
return children;
};

View File

@ -0,0 +1,26 @@
import React, { useContext, useState } from 'react';
import { getUser } from '../util/Util';
const LoginContext = React.createContext();
const LoginUpdateContext = React.createContext();
export const useLoginContext = () => {
return [useContext(LoginContext), useContext(LoginUpdateContext)];
};
export const UserContext = ({ children }) => {
const [user, setLoginState] = useState(getUser());
const updateLoginState = () => {
setLoginState(getUser());
};
return (
<LoginContext.Provider value={user}>
<LoginUpdateContext.Provider value={updateLoginState}>
{children}
</LoginUpdateContext.Provider>
</LoginContext.Provider>
);
};

View File

@ -21,11 +21,13 @@ export const setSession = (user, token) => {
};
export const fetchUser = async () => {
const host = options.dev ? options.api.baseUrl : '/api';
const res = await fetch(host + '/restricted/user', {
console.log('fetching user')
const host = `${proto}://${options.domain}`;
const res = await fetch(host + '/api/user', {
credentials: 'include'
// eslint-disable-next-line no-console
}).catch(console.error);
if (res.status === 200) {
const user = await res.json();
user.tag = `${user.username}#${user.discriminator}`;
@ -100,13 +102,27 @@ const plural = (amt, word) => {
return `${word}s`;
};
export const logout = async () => {
const response = await fetch('/api/logout', {
method: 'POST',
credentials: 'include'
});
if (response.status === 200) {
clearSession();
window.location.replace('/');
} else console.error('Failed to logout');
}
export const login = () => {
return new Promise((resolve) => {
const popup = window.open(
`${options.api.baseUrl}/auth/login`,
`${proto}://${options.domain}/api/login`,
'Discord login',
'menubar=no,location=no,width=500,height=800,left=500,top=200'
);
const poller = setInterval(async () => {
if (popup.closed) {
clearInterval(poller);
@ -115,6 +131,8 @@ export const login = () => {
else clearSession();
resolve();
}
}, 500);
}, 5000);
});
};
};
export const proto = process.env.NODE_ENV === 'production' ? 'https' : 'http' ;