From 15e720b0f41b231513432a46a5838476b8196463 Mon Sep 17 00:00:00 2001 From: Joshua Sigona Date: Fri, 10 Sep 2021 16:27:21 +0900 Subject: [PATCH] Discord login integration completed. --- run | 1 + src/App.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++- start | 1 + 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/run b/run index c95cc05..e290dee 100644 --- a/run +++ b/run @@ -1,3 +1,4 @@ rm .env +echo "REACT_APP_LOCAL_REDIRECT=true" > .env npm install HTTPS=true npm start \ No newline at end of file diff --git a/src/App.js b/src/App.js index 79d5544..b12240d 100644 --- a/src/App.js +++ b/src/App.js @@ -14,7 +14,7 @@ import { HashRouter, Switch, Route, - useHistory + useHistory, } from "react-router-dom"; import { HashLink as Link } from 'react-router-hash-link'; @@ -652,6 +652,15 @@ function VerifyLogin(p) { } function LoginForm(p) { + + function useHashQuery() { + const urlSearchParams = new URLSearchParams(window.location.search); + const params = Object.fromEntries(urlSearchParams.entries()); + return params + } + + const query = useHashQuery() + const [username,setUsername] = useState("") const [password,setPassword] = useState("") const [rememberMe,setRememberMe] = useState(false) @@ -731,6 +740,59 @@ function LoginForm(p) { } } + function DiscordLoginButton(){ + + const [checkForLogin,setCheckForLogin] = useState(false) + + const history = useHistory() + + useEffect(()=>{ + if (query.code) { + setCheckForLogin(true) + } + },[]) + + useEffect(()=>{ + if (checkForLogin) { + console.log("Attempting login...") + + axios.get(GetBackendURL(p)+"/userData?token="+query.code) + .then((data)=>{ + p.setLOGGEDINUSER(data.data.username) + p.setLOGGEDINHASH(data.data.token) + console.log(data.data.token) + cookies.set("username",data.data.username,30,'d') + cookies.set("password",data.data.token,30,'d') + cookies.set("userID",data.data.id,30,'d') + console.log(cookies.get("username")) + console.log(cookies.get("password")) + console.log(cookies.get("userID")) + return axios.post(GetBackendURL(p)+"/registerUser",{ + username:data.data.username, + email:data.data.email, + password:data.data.token, + avatar:"https://cdn.discordapp.com/avatars/"+data.data.id+"/"+data.data.avatar+".png", + userID:data.data.id, + recoveryhash:data.data.id + }) + //window.location.href="" + //console.log(data.data) + }) + .then((data)=>{ + //window.location.href="" + history.push("/") + }) + .catch((err)=>{ + console.log(err.message) + }) + } + },[checkForLogin]) + + return <> + + + } + return <> {loading? @@ -750,6 +812,7 @@ function LoginForm(p) { onFailure={responseGoogle} cookiePolicy={'single_host_origin'} /> + } diff --git a/start b/start index 620f5e3..e0cd6d6 100755 --- a/start +++ b/start @@ -1,3 +1,4 @@ echo "REACT_APP_GITPOD_WORKSPACE_URL=https://ngsplanner.herokuapp.com" > .env +echo "REACT_APP_LOCAL_REDIRECT=true" > .env npm install HTTPS=true npm start \ No newline at end of file