Updates
This commit is contained in:
+24
-23
@@ -1,33 +1,30 @@
|
||||
// @ts-check
|
||||
import handleSocialDb from "../../social-login/handleSocialDb";
|
||||
import githubLogin from "../../social-login/githubLogin";
|
||||
import camelJoinedtoCamelSpace from "../../../../utils/camelJoinedtoCamelSpace";
|
||||
import { APILoginFunctionReturn } from "../../../../types";
|
||||
|
||||
const handleSocialDb = require("../../social-login/handleSocialDb");
|
||||
const githubLogin = require("../../social-login/githubLogin");
|
||||
const camelJoinedtoCamelSpace = require("../../../../utils/camelJoinedtoCamelSpace");
|
||||
type Param = {
|
||||
code?: string;
|
||||
clientId?: string;
|
||||
clientSecret?: string;
|
||||
database?: string;
|
||||
additionalFields?: string[];
|
||||
additionalData?: { [s: string]: string | number };
|
||||
email?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Login with Github
|
||||
* @param {object} param
|
||||
* @param {string} [param.code]
|
||||
* @param {string} [param.clientId]
|
||||
* @param {string} [param.clientSecret]
|
||||
* @param {string} [param.database]
|
||||
* @param {string[]} [param.additionalFields]
|
||||
* @param {any} [param.res]
|
||||
* @param {string} [param.email]
|
||||
* @param {string | number} [param.userId]
|
||||
*
|
||||
* @returns {Promise<import("../../../../types").APILoginFunctionReturn>}
|
||||
* # API Login with Github
|
||||
*/
|
||||
module.exports = async function apiGithubLogin({
|
||||
export default async function apiGithubLogin({
|
||||
code,
|
||||
clientId,
|
||||
clientSecret,
|
||||
database,
|
||||
additionalFields,
|
||||
res,
|
||||
email,
|
||||
userId,
|
||||
}) {
|
||||
additionalData,
|
||||
}: Param): Promise<APILoginFunctionReturn> {
|
||||
if (!code || !clientId || !clientSecret || !database) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -73,7 +70,7 @@ module.exports = async function apiGithubLogin({
|
||||
? targetName?.split("-")
|
||||
: [targetName];
|
||||
|
||||
const payload = {
|
||||
let payload = {
|
||||
email: gitHubUser.email,
|
||||
first_name: camelJoinedtoCamelSpace(nameArray[0]),
|
||||
last_name: camelJoinedtoCamelSpace(nameArray[1]),
|
||||
@@ -84,10 +81,14 @@ module.exports = async function apiGithubLogin({
|
||||
username: "github-user-" + socialId,
|
||||
};
|
||||
|
||||
if (additionalData) {
|
||||
payload = { ...payload, ...additionalData };
|
||||
}
|
||||
|
||||
const loggedInGithubUser = await handleSocialDb({
|
||||
database,
|
||||
email: gitHubUser.email,
|
||||
payload: payload,
|
||||
payload,
|
||||
social_platform: "github",
|
||||
social_id: socialId,
|
||||
supEmail: email,
|
||||
@@ -99,4 +100,4 @@ module.exports = async function apiGithubLogin({
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return { ...loggedInGithubUser };
|
||||
};
|
||||
}
|
||||
+46
-35
@@ -1,40 +1,47 @@
|
||||
// @ts-check
|
||||
import https from "https";
|
||||
import handleSocialDb from "../../social-login/handleSocialDb";
|
||||
import EJSON from "../../../../utils/ejson";
|
||||
import {
|
||||
APIGoogleLoginFunctionParams,
|
||||
APILoginFunctionReturn,
|
||||
GoogleOauth2User,
|
||||
} from "../../../../types";
|
||||
|
||||
const https = require("https");
|
||||
const handleSocialDb = require("../../social-login/handleSocialDb");
|
||||
const EJSON = require("../../../../utils/ejson");
|
||||
|
||||
/** @type {import("../../../../types").APIGoogleLoginFunction} */
|
||||
module.exports = async function apiGoogleLogin({
|
||||
/**
|
||||
* # API google login
|
||||
*/
|
||||
export default async function apiGoogleLogin({
|
||||
token,
|
||||
database,
|
||||
additionalFields,
|
||||
}) {
|
||||
additionalData,
|
||||
}: APIGoogleLoginFunctionParams): Promise<APILoginFunctionReturn> {
|
||||
try {
|
||||
/** @type {import("../../../../types").GoogleOauth2User | undefined} */
|
||||
const gUser = await new Promise((resolve, reject) => {
|
||||
https
|
||||
.request(
|
||||
{
|
||||
method: "GET",
|
||||
hostname: "www.googleapis.com",
|
||||
path: "/oauth2/v3/userinfo",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
const gUser: GoogleOauth2User | undefined = 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(/** @type {any} */ (EJSON.parse(data)));
|
||||
});
|
||||
}
|
||||
)
|
||||
.end();
|
||||
});
|
||||
(res) => {
|
||||
let data = "";
|
||||
res.on("data", (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
res.on("end", () => {
|
||||
resolve(EJSON.parse(data) as any);
|
||||
});
|
||||
}
|
||||
)
|
||||
.end();
|
||||
}
|
||||
);
|
||||
|
||||
if (!gUser?.email_verified) throw new Error("No Google User.");
|
||||
|
||||
@@ -59,7 +66,7 @@ module.exports = async function apiGoogleLogin({
|
||||
const { given_name, family_name, email, sub, picture } = gUser;
|
||||
|
||||
/** @type {Object<string, any>} */
|
||||
const payloadObject = {
|
||||
let payloadObject: { [s: string]: any } = {
|
||||
email: email,
|
||||
first_name: given_name,
|
||||
last_name: family_name,
|
||||
@@ -70,6 +77,10 @@ module.exports = async function apiGoogleLogin({
|
||||
username: `google-user-${sub}`,
|
||||
};
|
||||
|
||||
if (additionalData) {
|
||||
payloadObject = { ...payloadObject, ...additionalData };
|
||||
}
|
||||
|
||||
const loggedInGoogleUser = await handleSocialDb({
|
||||
database,
|
||||
email: email || "",
|
||||
@@ -84,8 +95,8 @@ module.exports = async function apiGoogleLogin({
|
||||
////////////////////////////////////////
|
||||
|
||||
return { ...loggedInGoogleUser };
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`apo-google-login.js ERROR: ${error.message}`);
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log(`api-google-login.ts ERROR: ${error.message}`);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
@@ -93,4 +104,4 @@ module.exports = async function apiGoogleLogin({
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user