datasquirel/dist/client/auth/google/getAccessToken.js
Benjamin Toby a3561da53d Updates
2025-01-10 20:35:05 +01:00

72 lines
3.0 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getAccessToken;
exports.googleLogin = googleLogin;
let interval;
/**
* Login with Google Function
* ===============================================================================
* @description This function uses google identity api to login a user with datasquirel
*/
function getAccessToken(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
(_a = params.setLoading) === null || _a === void 0 ? void 0 : _a.call(params, true);
const response = (yield 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
*/
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);
}
});
}