Updates
This commit is contained in:
@@ -145,7 +145,7 @@ export default async function loginUser<
|
||||
|
||||
const cookieNames = getAuthCookieNames({
|
||||
database,
|
||||
userId: apiUserID,
|
||||
userId: apiUserID || process.env.DSQL_API_USER_ID,
|
||||
});
|
||||
|
||||
if (httpResponse.csrf && !skipWriteAuthFile) {
|
||||
|
||||
@@ -1,36 +1,21 @@
|
||||
import http from "http";
|
||||
import encrypt from "../../../functions/dsql/encrypt";
|
||||
import grabHostNames from "../../../utils/grab-host-names";
|
||||
import apiGoogleLogin from "../../../functions/api/users/social/api-google-login";
|
||||
import getAuthCookieNames from "../../../functions/backend/cookies/get-auth-cookie-names";
|
||||
import { writeAuthFile } from "../../../functions/backend/auth/write-auth-files";
|
||||
import { APILoginFunctionReturn } from "../../../types";
|
||||
import {
|
||||
APIGoogleLoginFunctionParams,
|
||||
APIResponseObject,
|
||||
GoogleAuthParams,
|
||||
} from "../../../types";
|
||||
import grabCookieExpiryDate from "../../../utils/grab-cookie-expirt-date";
|
||||
|
||||
type Param = {
|
||||
key?: string;
|
||||
token: string;
|
||||
database?: string;
|
||||
response?: http.ServerResponse;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
additionalFields?: string[];
|
||||
additionalData?: { [s: string]: string | number };
|
||||
apiUserID?: string | number;
|
||||
debug?: boolean;
|
||||
secureCookie?: boolean;
|
||||
loginOnly?: boolean;
|
||||
/**
|
||||
* Login without calling external API
|
||||
*/
|
||||
forceLocal?: boolean;
|
||||
};
|
||||
import queryDSQLAPI from "../../../functions/api/query-dsql-api";
|
||||
import grabUserDSQLAPIPath from "../../../utils/backend/users/grab-api-path";
|
||||
|
||||
/**
|
||||
* # SERVER FUNCTION: Login with google Function
|
||||
*/
|
||||
export default async function googleAuth({
|
||||
key,
|
||||
apiKey,
|
||||
token,
|
||||
database,
|
||||
response,
|
||||
@@ -42,12 +27,9 @@ export default async function googleAuth({
|
||||
debug,
|
||||
secureCookie,
|
||||
loginOnly,
|
||||
forceLocal,
|
||||
}: Param): Promise<APILoginFunctionReturn> {
|
||||
const grabedHostNames = grabHostNames({
|
||||
userId: apiUserID || process.env.DSQL_API_USER_ID,
|
||||
});
|
||||
const { host, port, scheme, user_id } = grabedHostNames;
|
||||
useLocal,
|
||||
apiVersion,
|
||||
}: GoogleAuthParams): Promise<APIResponseObject> {
|
||||
const COOKIE_EXPIRY_DATE = grabCookieExpiryDate();
|
||||
|
||||
const finalEncryptionKey =
|
||||
@@ -90,72 +72,37 @@ export default async function googleAuth({
|
||||
* Initialize HTTP response variable
|
||||
*/
|
||||
|
||||
let httpResponse: APILoginFunctionReturn = {
|
||||
let httpResponse: APIResponseObject = {
|
||||
success: false,
|
||||
};
|
||||
|
||||
if (forceLocal) {
|
||||
const googleAuthParams: APIGoogleLoginFunctionParams = {
|
||||
token,
|
||||
additionalFields,
|
||||
additionalData,
|
||||
debug,
|
||||
loginOnly,
|
||||
database,
|
||||
apiUserId: apiUserID || process.env.DSQL_API_USER_ID,
|
||||
};
|
||||
|
||||
if (useLocal) {
|
||||
if (debug) {
|
||||
console.log(`Google login with Local Paradigm ...`);
|
||||
}
|
||||
|
||||
httpResponse = await apiGoogleLogin({
|
||||
token,
|
||||
additionalFields,
|
||||
additionalData,
|
||||
debug,
|
||||
loginOnly,
|
||||
});
|
||||
httpResponse = await apiGoogleLogin(googleAuthParams);
|
||||
} else {
|
||||
httpResponse = await new Promise((resolve, reject) => {
|
||||
const reqPayload = JSON.stringify({
|
||||
token,
|
||||
httpResponse = await queryDSQLAPI({
|
||||
path: grabUserDSQLAPIPath({
|
||||
paradigm: "auth",
|
||||
action: "google-login",
|
||||
database,
|
||||
additionalFields,
|
||||
additionalData,
|
||||
});
|
||||
|
||||
const httpsRequest = scheme.request(
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Content-Length": Buffer.from(reqPayload).length,
|
||||
Authorization:
|
||||
key ||
|
||||
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
||||
process.env.DSQL_API_KEY,
|
||||
},
|
||||
port,
|
||||
hostname: host,
|
||||
path: `/api/user/${
|
||||
apiUserID || grabedHostNames.user_id
|
||||
}/google-login`,
|
||||
},
|
||||
|
||||
/**
|
||||
* Callback Function
|
||||
*
|
||||
* @description https request callback
|
||||
*/
|
||||
(response) => {
|
||||
var str = "";
|
||||
|
||||
response.on("data", function (chunk) {
|
||||
str += chunk;
|
||||
});
|
||||
|
||||
response.on("end", function () {
|
||||
resolve(JSON.parse(str));
|
||||
});
|
||||
|
||||
response.on("error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
}
|
||||
);
|
||||
httpsRequest.write(reqPayload);
|
||||
httpsRequest.end();
|
||||
apiVersion,
|
||||
}),
|
||||
apiKey,
|
||||
body: googleAuthParams,
|
||||
method: "POST",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -173,7 +120,7 @@ export default async function googleAuth({
|
||||
|
||||
const cookieNames = getAuthCookieNames({
|
||||
database,
|
||||
userId: user_id,
|
||||
userId: apiUserID || process.env.DSQL_API_USER_ID,
|
||||
});
|
||||
|
||||
if (httpResponse.csrf) {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import http from "http";
|
||||
import getAuthCookieNames from "../../functions/backend/cookies/get-auth-cookie-names";
|
||||
import parseCookies from "../../utils/backend/parseCookies";
|
||||
import decrypt from "../../functions/dsql/decrypt";
|
||||
import EJSON from "../../utils/ejson";
|
||||
import { SendOneTimeCodeEmailResponse } from "../../types";
|
||||
|
||||
type Param = {
|
||||
request?: http.IncomingMessage & { [s: string]: any };
|
||||
cookieString?: string;
|
||||
email?: string;
|
||||
code?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Verify the temp email code sent to the user's email address
|
||||
*/
|
||||
export default async function validateTempEmailCode({
|
||||
request,
|
||||
email,
|
||||
cookieString,
|
||||
code,
|
||||
}: Param): Promise<SendOneTimeCodeEmailResponse | null> {
|
||||
try {
|
||||
const keyNames = getAuthCookieNames();
|
||||
const oneTimeCodeCookieName = keyNames.oneTimeCodeName;
|
||||
|
||||
const cookies = parseCookies({ request, cookieString });
|
||||
const encryptedOneTimeCode = cookies[oneTimeCodeCookieName];
|
||||
|
||||
const encryptedPayload = decrypt({
|
||||
encryptedString: encryptedOneTimeCode,
|
||||
});
|
||||
|
||||
const payload = EJSON.parse(encryptedPayload) as
|
||||
| SendOneTimeCodeEmailResponse
|
||||
| undefined;
|
||||
|
||||
if (payload?.email && !email) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
if (payload?.email && payload.email === email) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error: any) {
|
||||
console.log("validateTempEmailCode error:", error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user