datasquirel/dist/package-shared/functions/api/users/social/api-google-login.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

73 lines
2.4 KiB
JavaScript

import https from "https";
import handleSocialDb from "../../social-login/handleSocialDb";
import EJSON from "../../../../utils/ejson";
/**
* # API google login
*/
export default async function apiGoogleLogin({ token, database, additionalFields, additionalData, debug, loginOnly, }) {
try {
const gUser = await new Promise((resolve, reject) => {
https
.request({
method: "GET",
hostname: "www.googleapis.com",
path: "/oauth2/v3/userinfo",
headers: {
Authorization: `Bearer ${token}`,
},
}, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve(EJSON.parse(data));
});
})
.end();
});
if (!(gUser === null || gUser === void 0 ? void 0 : gUser.email_verified))
throw new Error("No Google User.");
/**
* Create new user folder and file
*
* @description Create new user folder and file
*/
const { given_name, family_name, email, sub, picture } = gUser;
let 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 (additionalData) {
payloadObject = Object.assign(Object.assign({}, payloadObject), additionalData);
}
const loggedInGoogleUser = await handleSocialDb({
database,
email: email || "",
payload: payloadObject,
social_platform: "google",
additionalFields,
debug,
loginOnly,
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return Object.assign({}, loggedInGoogleUser);
}
catch ( /** @type {any} */error) {
console.log(`api-google-login.ts ERROR: ${error.message}`);
return {
success: false,
payload: undefined,
msg: error.message,
};
}
}