datasquirel/client/auth/github/getAccessToken.js

47 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-08-06 11:51:43 +00:00
// @ts-check
/**
* Login with Github Function
* ===============================================================================
2023-08-06 12:55:02 +00:00
* @description This function uses github api to login a user with datasquirel
2023-08-06 11:51:43 +00:00
*
* @async
*
* @param {object} params - Single object passed
* @param {string} params.clientId - Github app client ID: {@link https://datasquirel.com/docs}
* @param {string} params.redirectUrl - Github Redirect URL as listed in your oauth app settings: {@link https://datasquirel.com/docs}
* @param {function(boolean): void} [params.setLoading] - React setState Function: sets whether the google login button is ready or not
2023-08-08 11:36:32 +00:00
* @param {string[]} [params.scopes] - Scopes to be requested from the user
2023-08-08 11:39:41 +00:00
* @param {{key: string, value: string}[] | string} [params.extraUriParams] - Extra params to be added to the url
2023-08-06 11:51:43 +00:00
*
* @returns {void} - Return
*/
2023-08-08 11:39:41 +00:00
module.exports = function getAccessToken({ clientId, redirectUrl, setLoading, scopes, extraUriParams }) {
2023-08-06 11:51:43 +00:00
/**
* == Initialize
*
* @description Initialize
*/
if (setLoading) setLoading(true);
2023-08-08 11:51:09 +00:00
const scopeString = scopes ? scopes.join("%20") : "read:user";
2023-08-08 11:28:34 +00:00
2023-08-08 11:39:41 +00:00
const extraUriParamsString = extraUriParams
? typeof extraUriParams == "string"
? extraUriParams
: `?${Object.keys(extraUriParams)
.map((key) => `${key}=${extraUriParams[key]}`)
.join("&")}`
2023-08-08 11:36:32 +00:00
: "";
2023-08-08 11:39:41 +00:00
const fetchUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=${scopeString}&redirect_uri=${redirectUrl}${window.location.pathname}${extraUriParamsString}`;
2023-08-06 11:51:43 +00:00
window.location.assign(fetchUrl);
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
};