57 lines
2.1 KiB
JavaScript
57 lines
2.1 KiB
JavaScript
let interval;
|
|
/**
|
|
* Login with Google Function
|
|
* ===============================================================================
|
|
* @description This function uses google identity api to login a user with datasquirel
|
|
*/
|
|
export default async function getAccessToken(params) {
|
|
var _a, _b;
|
|
(_a = params.setLoading) === null || _a === void 0 ? void 0 : _a.call(params, true);
|
|
const response = (await new Promise((resolve, reject) => {
|
|
interval = setInterval(() => {
|
|
// @ts-ignore
|
|
let google = window.google;
|
|
if (google) {
|
|
window.clearInterval(interval);
|
|
resolve(googleLogin(Object.assign(Object.assign({}, params), { google })));
|
|
}
|
|
}, 500);
|
|
}));
|
|
(_b = params.setLoading) === null || _b === void 0 ? void 0 : _b.call(params, false);
|
|
return response;
|
|
}
|
|
/**
|
|
* # Google Login Function
|
|
*/
|
|
export function googleLogin({ google, clientId, setLoading, triggerPrompt, }) {
|
|
setTimeout(() => {
|
|
setLoading === null || setLoading === void 0 ? void 0 : setLoading(false);
|
|
}, 3000);
|
|
return new Promise((resolve, reject) => {
|
|
/**
|
|
* # Callback Function
|
|
* @param {import("../../../package-shared/types").GoogleAccessTokenObject} response
|
|
*/
|
|
function handleCredentialResponse(response) {
|
|
resolve(response.access_token);
|
|
}
|
|
const googleAuth = google.accounts.oauth2.initTokenClient({
|
|
client_id: clientId,
|
|
scope: "email profile",
|
|
callback: handleCredentialResponse,
|
|
});
|
|
googleAuth.requestAccessToken();
|
|
if (triggerPrompt) {
|
|
google.accounts.id.prompt(triggerGooglePromptCallback);
|
|
}
|
|
/**
|
|
* Google prompt notification callback
|
|
* ========================================================
|
|
* @param {import("../../../package-shared/types").GoogleIdentityPromptNotification} notification
|
|
*/
|
|
function triggerGooglePromptCallback(notification) {
|
|
console.log(notification);
|
|
}
|
|
});
|
|
}
|