Started adding Github Oauth2 system for creators

This commit is contained in:
frikky
2022-01-15 00:47:35 +01:00
parent 622baeb59f
commit c406b4453e
2 changed files with 170 additions and 18 deletions
+99 -7
View File
@@ -18,6 +18,7 @@ const SetAuthentication = (props) => {
//session_state
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
console.log("PARAMS: ", params)
const authenticationStore = [];
var appAuthData = {
@@ -29,19 +30,16 @@ const SetAuthentication = (props) => {
},
fields: [],
type: "oauth2",
};
}
if (window !== undefined && window !== null) {
console.log(window.location);
//console.log(window.location);
appAuthData.fields.push({
key: "redirect_uri",
value: window.location.origin + window.location.pathname,
});
}
if (params.code !== undefined && params.code !== null) {
appAuthData.fields.push({ key: "code", value: params.code });
}
if (params.session_state !== undefined && params.session_state !== null) {
appAuthData.fields.push({
@@ -50,12 +48,34 @@ const SetAuthentication = (props) => {
});
}
var externalData = {
handleExternal: false,
user: "",
type: "",
code: "",
}
if (params.code !== undefined && params.code !== null) {
appAuthData.fields.push({ key: "code", value: params.code });
externalData.code = params.code
}
if (params.state !== undefined && params.state !== null) {
const paramsplit = params.state.split("&");
console.log(paramsplit);
for (var key in paramsplit) {
const query = paramsplit[key].split("=");
console.log(query);
console.log("K:V: ", key, query);
if (query.length > 1) {
if (query[0] === "type" && query[1] === "github"){
externalData.handleExternal = true
externalData.type = "github"
}
if (query[0] === "username" || query[0] === "user"){
externalData.user = query[1]
}
}
if (query.length !== 2) {
console.log("INVALID QUERY: ", query);
@@ -116,7 +136,79 @@ const SetAuthentication = (props) => {
}
}
console.log(appAuthData);
if (externalData.handleExternal) {
console.log("RUN EXTERNAL!!: ", externalData)
fetch(globalUrl + "/api/v1/triggers/github/register", {
method: "PUT",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
credentials: "include",
body: JSON.stringify(externalData),
})
.then((response) => {
const cursearch = typeof window === "undefined" || window.location === undefined ? "" : window.location.search;
const tmpView = new URLSearchParams(cursearch).get("state");
if (
tmpView !== undefined &&
tmpView !== null &&
tmpView.length > 0
) {
console.log("State to find app name from: ", tmpView)
}
if (response.status !== 200) {
console.log("Status not 200 for oauth2 authentication");
setFailed(true);
} else {
setFinished(true);
//setTimeout(() => {
// window.close();
//}, 2500);
}
return response.json();
})
.then((responseJson) => {
//setUserSettings(responseJson)
console.log("Resp: ", responseJson);
if (responseJson.reason !== undefined) {
setResponse(responseJson.reason);
setFinished(true);
} else {
const cursearch = typeof window === "undefined" || window.location === undefined ? "" : window.location.search;
var tmpView = new URLSearchParams(cursearch).get("error_description");
if (
tmpView !== undefined &&
tmpView !== null &&
tmpView.length > 0
) {
setResponse(tmpView)
} else {
tmpView = new URLSearchParams(cursearch).get("error");
if (
tmpView !== undefined &&
tmpView !== null &&
tmpView.length > 0
) {
setResponse(tmpView)
}
}
}
})
.catch((error) => {
console.log(error);
});
return
}
console.log(appAuthData);
fetch(globalUrl + "/api/v1/apps/authentication", {
method: "PUT",
+71 -11
View File
@@ -728,17 +728,19 @@ const Settings = (props) => {
<h2>Platform Earnings</h2>
<div style={{ display: runFlex ? "flex" : "", width: "100%" }}>
<div>
<Button
style={{ height: 40, marginTop: 10 }}
variant="outlined"
color="primary"
fullWidth={true}
onClick={() => {
handleEthereumConnection();
}}
>
Connect to Github
</Button>
{isCloud ?
<Button
style={{ height: 40, marginTop: 10 }}
variant="outlined"
color="primary"
fullWidth={true}
onClick={() => {
handleGithubConnection();
}}
>
Connect to Github
</Button>
: null}
</div>
<div style={{ flex: 1, display: "flex" }}>
<div>
@@ -914,6 +916,64 @@ const Settings = (props) => {
});
};
const handleGithubConnection = () => {
console.log("GITHUB CONNECT WOO")
//result = RestClient.post('https://github.com/login/oauth/access_token',
console.log("HOST: ", window.location.host);
console.log("HOST: ", window.location);
const redirectUri = isCloud
? window.location.host === "localhost:3002"
? "http%3A%2F%2Flocalhost:3002%2Fset_authentication"
: "https%3A%2F%2Fshuffler.io%2Fset_authentication"
: window.location.protocol === "http:" ?
`http%3A%2F%2F${window.location.host}%2Fset_authentication`
:
`https%3A%2F%2F${window.location.host}%2Fset_authentication`
const client_id = "3d272b1b782b100b1e61"
const username = userdata.id;
const scopes = "user:email";
const url = `https://github.com/login/oauth/authorize?access_type=offline&prompt=consent&client_id=${client_id}&redirect_uri=${redirectUri}&response_type=code&scope=${scopes}&state=username%3D${username}%26type%3Dgithub`
console.log("URL: ", url);
var newwin = window.open(url, "", "width=800,height=600");
// Check whether we got a callback somewhere
//var id = setInterval(function () {
// fetch(
// globalUrl + "/api/v1/triggers/gmail/" + selectedTrigger.id,
// {
// method: "GET",
// headers: { "content-type": "application/json" },
// credentials: "include",
// }
// )
// .then((response) => {
// if (response.status !== 200) {
// throw new Error("No trigger info :o!");
// }
// return response.json();
// })
// .then((responseJson) => {
// console.log("RESPONSE: ");
// setTriggerAuthentication(responseJson);
// clearInterval(id);
// newwin.close();
// setGmailFolders();
// })
// .catch((error) => {
// console.log(error.toString());
// });
//}, 2500);
//saveWorkflow(workflow);
}
const handleEthereumTokenCreation = async () => {
const provider = await detectEthereumProvider();
if (!provider) {