2024-12-08 08:58:57 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} GoogleGetAccessTokenFunctionParams
|
|
|
|
* @property {string} clientId - Google app client ID: {@link https://datasquirel.com/docs}
|
|
|
|
* @property {boolean} [triggerPrompt] - Whether to trigger Google signing popup or not: {@link https://datasquirel.com/docs}
|
|
|
|
* @property {React.Dispatch<React.SetStateAction<boolean>>} [setLoading] - React setState Function: sets whether the google login button is ready or not
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @type {any} */
|
|
|
|
let interval;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Login with Google Function
|
|
|
|
* ===============================================================================
|
|
|
|
* @description This function uses google identity api to login a user with datasquirel
|
|
|
|
*
|
|
|
|
* @async
|
|
|
|
*
|
2024-12-08 08:58:57 +00:00
|
|
|
* @requires script "https://accounts.google.com/gsi/client" async script added to head
|
2023-09-21 14:00:04 +00:00
|
|
|
*
|
2024-12-08 08:58:57 +00:00
|
|
|
* @param {GoogleGetAccessTokenFunctionParams} params - Single object passed
|
|
|
|
|
2024-12-10 18:27:08 +00:00
|
|
|
* @returns {Promise<string>} - Access Token String
|
2023-09-21 14:00:04 +00:00
|
|
|
*/
|
2024-12-08 08:58:57 +00:00
|
|
|
module.exports = async function getAccessToken(params) {
|
|
|
|
params.setLoading?.(true);
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
const response = await new Promise((resolve, reject) => {
|
2024-12-08 08:58:57 +00:00
|
|
|
interval = setInterval(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
let google = window.google;
|
|
|
|
|
2023-09-21 14:00:04 +00:00
|
|
|
if (google) {
|
2024-12-08 08:58:57 +00:00
|
|
|
window.clearInterval(interval);
|
|
|
|
resolve(googleLogin({ ...params, google }));
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
});
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
params.setLoading?.(false);
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
return response;
|
|
|
|
};
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
/**
|
|
|
|
* # Google Login Function
|
|
|
|
*
|
|
|
|
* @param {GoogleGetAccessTokenFunctionParams & { google: any }} params
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
function googleLogin({ google, clientId, setLoading, triggerPrompt }) {
|
|
|
|
setTimeout(() => {
|
|
|
|
setLoading?.(false);
|
|
|
|
}, 3000);
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
/**
|
|
|
|
* # Callback Function
|
|
|
|
* @param {import("../../../package-shared/types").GoogleAccessTokenObject} response
|
|
|
|
*/
|
|
|
|
function handleCredentialResponse(response) {
|
|
|
|
resolve(response.access_token);
|
|
|
|
}
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
const googleAuth = google.accounts.oauth2.initTokenClient({
|
|
|
|
client_id: clientId,
|
|
|
|
scope: "email profile",
|
|
|
|
callback: handleCredentialResponse,
|
|
|
|
});
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
googleAuth.requestAccessToken();
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
if (triggerPrompt) {
|
|
|
|
google.accounts.id.prompt(triggerGooglePromptCallback);
|
|
|
|
}
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-12-08 08:58:57 +00:00
|
|
|
/**
|
|
|
|
* Google prompt notification callback
|
|
|
|
* ========================================================
|
|
|
|
* @param {import("../../../package-shared/types").GoogleIdentityPromptNotification} notification
|
|
|
|
*/
|
|
|
|
function triggerGooglePromptCallback(notification) {
|
|
|
|
console.log(notification);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|