This commit is contained in:
Benjamin Toby
2025-01-10 20:35:05 +01:00
parent 9192dae0b5
commit a3561da53d
286 changed files with 13862 additions and 42590 deletions
+13
View File
@@ -0,0 +1,13 @@
type Param = {
clientId: string;
redirectUrl: string;
setLoading?: (arg0: boolean) => void;
scopes?: string[];
};
/**
* Login with Github Function
* ===============================================================================
* @description This function uses github api to login a user with datasquirel
*/
export default function getAccessToken({ clientId, redirectUrl, setLoading, scopes, }: Param): void;
export {};
+15
View File
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getAccessToken;
/**
* Login with Github Function
* ===============================================================================
* @description This function uses github api to login a user with datasquirel
*/
function getAccessToken({ clientId, redirectUrl, setLoading, scopes, }) {
if (setLoading)
setLoading(true);
const scopeString = scopes ? scopes.join("%20") : "read:user";
const fetchUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=${scopeString}&redirect_uri=${redirectUrl}`;
window.location.assign(fetchUrl);
}
+18
View File
@@ -0,0 +1,18 @@
interface GoogleGetAccessTokenFunctionParams {
clientId: string;
triggerPrompt?: boolean;
setLoading?: React.Dispatch<React.SetStateAction<boolean>>;
}
/**
* Login with Google Function
* ===============================================================================
* @description This function uses google identity api to login a user with datasquirel
*/
export default function getAccessToken(params: GoogleGetAccessTokenFunctionParams): Promise<string>;
/**
* # Google Login Function
*/
export declare function googleLogin({ google, clientId, setLoading, triggerPrompt, }: GoogleGetAccessTokenFunctionParams & {
google: any;
}): Promise<unknown>;
export {};
+71
View File
@@ -0,0 +1,71 @@
"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);
}
});
}
+8
View File
@@ -0,0 +1,8 @@
/**
* Login with Google Function
* ===============================================================================
* @description This function uses google identity api to login a user with datasquirel
*/
export default function logout(params: {
[s: string]: any;
} | null): Promise<boolean>;
+102
View File
@@ -0,0 +1,102 @@
"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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = logout;
const parseClientCookies_1 = __importDefault(require("../utils/parseClientCookies"));
/**
* Login with Google Function
* ===============================================================================
* @description This function uses google identity api to login a user with datasquirel
*/
function logout(params) {
return __awaiter(this, void 0, void 0, function* () {
try {
const localUser = localStorage.getItem("user");
let targetUser;
try {
targetUser = JSON.parse(localUser || "");
}
catch (error) {
console.log(error);
}
if (!targetUser) {
return false;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const cookies = (0, parseClientCookies_1.default)();
const socialId = (cookies === null || cookies === void 0 ? void 0 : cookies.datasquirel_social_id) &&
typeof cookies.datasquirel_social_id == "string" &&
!cookies.datasquirel_social_id.match(/^null$/i)
? cookies.datasquirel_social_id
: null;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
localStorage.setItem("user", "{}");
localStorage.removeItem("csrf");
document.cookie = `datasquirel_social_id=null;samesite=strict;path=/`;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const response = yield new Promise((resolve, reject) => {
if (socialId && !(socialId === null || socialId === void 0 ? void 0 : socialId.match(/^null$/i))) {
const googleClientId = params === null || params === void 0 ? void 0 : params.googleClientId;
if (googleClientId) {
const googleScript = document.createElement("script");
googleScript.src = "https://accounts.google.com/gsi/client";
googleScript.className = "social-script-tag";
document.body.appendChild(googleScript);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
googleScript.onload = function (e) {
// @ts-ignore
const google = window.google;
if (google) {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
google.accounts.id.initialize({
client_id: googleClientId,
});
google.accounts.id.revoke(socialId, (done) => {
console.log(done.error);
resolve(true);
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
};
}
else {
resolve(true);
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
else {
resolve(true);
}
});
return response;
}
catch (error) {
return false;
}
});
}