Updates
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
// @ts-check
|
||||
|
||||
const handleSocialDb = require("../../social-login/handleSocialDb");
|
||||
const githubLogin = require("../../social-login/githubLogin");
|
||||
const camelJoinedtoCamelSpace = require("../../../../utils/camelJoinedtoCamelSpace");
|
||||
|
||||
/**
|
||||
* # Login with Github
|
||||
* @param {object} param
|
||||
* @param {string} [param.code]
|
||||
* @param {string} [param.clientId]
|
||||
* @param {string} [param.clientSecret]
|
||||
* @param {string} [param.database]
|
||||
* @param {Object<string, any>} [param.additionalFields]
|
||||
* @param {any} [param.res]
|
||||
* @param {string} [param.email]
|
||||
* @param {string | number} [param.userId]
|
||||
*
|
||||
* @returns {Promise<import("../../../../types").APIGoogleLoginFunctionReturn>}
|
||||
*/
|
||||
module.exports = async function apiGithubLogin({
|
||||
code,
|
||||
clientId,
|
||||
clientSecret,
|
||||
database,
|
||||
additionalFields,
|
||||
res,
|
||||
email,
|
||||
userId,
|
||||
}) {
|
||||
if (!code || !clientId || !clientSecret || !database) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Missing query params",
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
typeof code !== "string" ||
|
||||
typeof clientId !== "string" ||
|
||||
typeof clientSecret !== "string" ||
|
||||
typeof database !== "string"
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Wrong Parameters",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
const gitHubUser = await githubLogin({
|
||||
code: code,
|
||||
clientId: clientId,
|
||||
clientSecret: clientSecret,
|
||||
});
|
||||
|
||||
if (!gitHubUser) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "No github user returned",
|
||||
};
|
||||
}
|
||||
|
||||
const socialId = gitHubUser.name || gitHubUser.id || gitHubUser.login;
|
||||
const targetName = gitHubUser.name || gitHubUser.login;
|
||||
const nameArray = targetName?.match(/ /)
|
||||
? targetName?.split(" ")
|
||||
: targetName?.match(/\-/)
|
||||
? targetName?.split("-")
|
||||
: [targetName];
|
||||
|
||||
const payload = {
|
||||
email: gitHubUser.email,
|
||||
first_name: camelJoinedtoCamelSpace(nameArray[0]),
|
||||
last_name: camelJoinedtoCamelSpace(nameArray[1]),
|
||||
social_id: socialId,
|
||||
social_platform: "github",
|
||||
image: gitHubUser.avatar_url,
|
||||
image_thumbnail: gitHubUser.avatar_url,
|
||||
username: "github-user-" + socialId,
|
||||
};
|
||||
|
||||
if (additionalFields && Object.keys(additionalFields).length > 0) {
|
||||
Object.keys(additionalFields).forEach((key) => {
|
||||
// @ts-ignore
|
||||
payload[key] = additionalFields[key];
|
||||
});
|
||||
}
|
||||
|
||||
const loggedInGithubUser = await handleSocialDb({
|
||||
database,
|
||||
email: gitHubUser.email,
|
||||
payload: payload,
|
||||
social_platform: "github",
|
||||
res: res,
|
||||
social_id: socialId,
|
||||
supEmail: email,
|
||||
additionalFields,
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return { success: true, ...loggedInGithubUser, dsqlUserId: userId };
|
||||
};
|
||||
@@ -0,0 +1,88 @@
|
||||
// @ts-check
|
||||
|
||||
const { OAuth2Client } = require("google-auth-library");
|
||||
const handleSocialDb = require("../../social-login/handleSocialDb");
|
||||
|
||||
/** @type {import("../../../../types").APIGoogleLoginFunction} */
|
||||
module.exports = async function apiGoogleLogin({
|
||||
clientId,
|
||||
token,
|
||||
database,
|
||||
userId,
|
||||
additionalFields,
|
||||
res,
|
||||
}) {
|
||||
const client = new OAuth2Client(clientId);
|
||||
|
||||
const ticket = await client.verifyIdToken({
|
||||
idToken: token,
|
||||
audience: clientId,
|
||||
});
|
||||
|
||||
if (!ticket?.getPayload()?.email_verified) {
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
};
|
||||
}
|
||||
|
||||
const payload = ticket.getPayload();
|
||||
|
||||
if (!payload) throw new Error("No Payload");
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!database || typeof database != "string" || database?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
user: undefined,
|
||||
msg: "Please provide a database slug(database name in lowercase with no spaces)",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
const targetDbName = `datasquirel_user_${userId}_${database}`;
|
||||
|
||||
const { given_name, family_name, email, sub, picture, email_verified } =
|
||||
payload;
|
||||
|
||||
/** @type {Object<string, any>} */
|
||||
const payloadObject = {
|
||||
email: email,
|
||||
first_name: given_name,
|
||||
last_name: family_name,
|
||||
social_id: sub,
|
||||
social_platform: "google",
|
||||
image: picture,
|
||||
image_thumbnail: picture,
|
||||
username: `google-user-${sub}`,
|
||||
};
|
||||
|
||||
if (additionalFields && Object.keys(additionalFields).length > 0) {
|
||||
Object.keys(additionalFields).forEach((key) => {
|
||||
payloadObject[key] = additionalFields[key];
|
||||
});
|
||||
}
|
||||
|
||||
const loggedInGoogleUser = await handleSocialDb({
|
||||
res,
|
||||
database: targetDbName,
|
||||
email: email || "",
|
||||
payload: payloadObject,
|
||||
social_platform: "google",
|
||||
social_id: sub,
|
||||
additionalFields,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return { success: true, ...loggedInGoogleUser, dsqlUserId: userId };
|
||||
};
|
||||
Reference in New Issue
Block a user