diff --git a/client/src/Routes/Private.js b/client/src/Routes/Private.js
new file mode 100644
index 0000000..544b1c7
--- /dev/null
+++ b/client/src/Routes/Private.js
@@ -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 ;
+ return children;
+};
\ No newline at end of file
diff --git a/client/src/Structures/UserContext.js b/client/src/Structures/UserContext.js
new file mode 100644
index 0000000..baa8eab
--- /dev/null
+++ b/client/src/Structures/UserContext.js
@@ -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 (
+
+
+ {children}
+
+
+ );
+
+};
\ No newline at end of file
diff --git a/client/src/util/Util.js b/client/src/util/Util.js
index e25a21c..30af1b1 100644
--- a/client/src/util/Util.js
+++ b/client/src/util/Util.js
@@ -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);
});
-};
\ No newline at end of file
+};
+
+export const proto = process.env.NODE_ENV === 'production' ? 'https' : 'http' ;