misc
This commit is contained in:
parent
580b15da00
commit
5e6d16d101
10
client/src/Routes/Private.js
Normal file
10
client/src/Routes/Private.js
Normal 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;
|
||||||
|
};
|
26
client/src/Structures/UserContext.js
Normal file
26
client/src/Structures/UserContext.js
Normal 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>
|
||||||
|
);
|
||||||
|
|
||||||
|
};
|
@ -21,11 +21,13 @@ export const setSession = (user, token) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const fetchUser = async () => {
|
export const fetchUser = async () => {
|
||||||
const host = options.dev ? options.api.baseUrl : '/api';
|
console.log('fetching user')
|
||||||
const res = await fetch(host + '/restricted/user', {
|
const host = `${proto}://${options.domain}`;
|
||||||
|
const res = await fetch(host + '/api/user', {
|
||||||
credentials: 'include'
|
credentials: 'include'
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
}).catch(console.error);
|
}).catch(console.error);
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
const user = await res.json();
|
const user = await res.json();
|
||||||
user.tag = `${user.username}#${user.discriminator}`;
|
user.tag = `${user.username}#${user.discriminator}`;
|
||||||
@ -100,13 +102,27 @@ const plural = (amt, word) => {
|
|||||||
return `${word}s`;
|
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 = () => {
|
export const login = () => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const popup = window.open(
|
const popup = window.open(
|
||||||
`${options.api.baseUrl}/auth/login`,
|
`${proto}://${options.domain}/api/login`,
|
||||||
'Discord login',
|
'Discord login',
|
||||||
'menubar=no,location=no,width=500,height=800,left=500,top=200'
|
'menubar=no,location=no,width=500,height=800,left=500,top=200'
|
||||||
);
|
);
|
||||||
|
|
||||||
const poller = setInterval(async () => {
|
const poller = setInterval(async () => {
|
||||||
if (popup.closed) {
|
if (popup.closed) {
|
||||||
clearInterval(poller);
|
clearInterval(poller);
|
||||||
@ -115,6 +131,8 @@ export const login = () => {
|
|||||||
else clearSession();
|
else clearSession();
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 5000);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const proto = process.env.NODE_ENV === 'production' ? 'https' : 'http' ;
|
||||||
|
Loading…
Reference in New Issue
Block a user