Refactor Code to typescript
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
declare function _exports({ code, clientId, clientSecret, database, additionalFields, email, additionalData, }: {
|
||||
code?: string;
|
||||
clientId?: string;
|
||||
clientSecret?: string;
|
||||
database?: string;
|
||||
additionalFields?: string[];
|
||||
additionalData?: {
|
||||
[x: string]: string | number;
|
||||
};
|
||||
email?: string;
|
||||
}): Promise<import("../../../../types").APILoginFunctionReturn>;
|
||||
export = _exports;
|
||||
+17
-18
@@ -1,23 +1,22 @@
|
||||
// @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 {Object<string,string|number>} [param.additionalData]
|
||||
* @param {string} [param.email]
|
||||
*
|
||||
* @returns {Promise<import("../../../../types").APILoginFunctionReturn>}
|
||||
* # API Login with Github
|
||||
*/
|
||||
module.exports = async function apiGithubLogin({
|
||||
export default async function apiGithubLogin({
|
||||
code,
|
||||
clientId,
|
||||
clientSecret,
|
||||
@@ -25,7 +24,7 @@ module.exports = async function apiGithubLogin({
|
||||
additionalFields,
|
||||
email,
|
||||
additionalData,
|
||||
}) {
|
||||
}: Param): Promise<APILoginFunctionReturn> {
|
||||
if (!code || !clientId || !clientSecret || !database) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -101,4 +100,4 @@ module.exports = async function apiGithubLogin({
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return { ...loggedInGithubUser };
|
||||
};
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
declare const _exports: import("../../../../types").APIGoogleLoginFunction;
|
||||
export = _exports;
|
||||
+40
-34
@@ -1,41 +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.");
|
||||
|
||||
@@ -60,7 +66,7 @@ module.exports = async function apiGoogleLogin({
|
||||
const { given_name, family_name, email, sub, picture } = gUser;
|
||||
|
||||
/** @type {Object<string, any>} */
|
||||
let payloadObject = {
|
||||
let payloadObject: { [s: string]: any } = {
|
||||
email: email,
|
||||
first_name: given_name,
|
||||
last_name: family_name,
|
||||
@@ -89,7 +95,7 @@ module.exports = async function apiGoogleLogin({
|
||||
////////////////////////////////////////
|
||||
|
||||
return { ...loggedInGoogleUser };
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log(`apo-google-login.js ERROR: ${error.message}`);
|
||||
|
||||
return {
|
||||
@@ -98,4 +104,4 @@ module.exports = async function apiGoogleLogin({
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user