Updates
This commit is contained in:
@@ -12,6 +12,7 @@ import debugLog from "../../utils/logging/debug-log";
|
||||
import grabCookieExpiryDate from "../../utils/grab-cookie-expirt-date";
|
||||
import grabUserDSQLAPIPath from "../../utils/backend/users/grab-api-path";
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import postLoginResponseHandler from "../../functions/backend/auth/post-login-response-handler";
|
||||
|
||||
function debugFn(log: any, label?: string) {
|
||||
debugLog({ log, addTime: true, title: "loginUser", label });
|
||||
@@ -130,55 +131,18 @@ export default async function loginUser<
|
||||
*/
|
||||
|
||||
if (httpResponse?.success) {
|
||||
let encryptedPayload = encrypt({
|
||||
data: JSON.stringify(httpResponse.payload),
|
||||
encryptionKey: finalEncryptionKey,
|
||||
encryptionSalt: finalEncryptionSalt,
|
||||
});
|
||||
|
||||
try {
|
||||
if (token && encryptedPayload)
|
||||
httpResponse["token"] = encryptedPayload;
|
||||
} catch (error: any) {
|
||||
console.log("Login User HTTP Response Error:", error.message);
|
||||
}
|
||||
|
||||
const cookieNames = getAuthCookieNames({
|
||||
postLoginResponseHandler({
|
||||
database,
|
||||
httpResponse,
|
||||
cleanupTokens,
|
||||
debug,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
response,
|
||||
secureCookie,
|
||||
skipWriteAuthFile,
|
||||
token,
|
||||
});
|
||||
|
||||
if (httpResponse.csrf && !skipWriteAuthFile) {
|
||||
writeAuthFile(
|
||||
httpResponse.csrf,
|
||||
JSON.stringify(httpResponse.payload),
|
||||
cleanupTokens && httpResponse.payload?.id
|
||||
? { userId: httpResponse.payload.id }
|
||||
: undefined
|
||||
);
|
||||
}
|
||||
|
||||
httpResponse["cookieNames"] = cookieNames;
|
||||
httpResponse["key"] = String(encryptedPayload);
|
||||
|
||||
const authKeyName = cookieNames.keyCookieName;
|
||||
const csrfName = cookieNames.csrfCookieName;
|
||||
|
||||
if (debug) {
|
||||
debugFn(authKeyName, "authKeyName");
|
||||
debugFn(csrfName, "csrfName");
|
||||
debugFn(encryptedPayload, "encryptedPayload");
|
||||
}
|
||||
|
||||
response?.setHeader("Set-Cookie", [
|
||||
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Expires=${COOKIE_EXPIRY_DATE}${
|
||||
secureCookie ? ";Secure=true" : ""
|
||||
}`,
|
||||
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true;Expires=${COOKIE_EXPIRY_DATE}`,
|
||||
]);
|
||||
|
||||
if (debug) {
|
||||
debugFn("Response Sent!");
|
||||
}
|
||||
}
|
||||
|
||||
return httpResponse;
|
||||
|
||||
@@ -28,6 +28,7 @@ export default async function sendEmailCode(
|
||||
useLocal,
|
||||
apiVersion,
|
||||
dbUserId,
|
||||
html,
|
||||
} = params;
|
||||
|
||||
const defaultTempLoginFieldName = "temp_login_code";
|
||||
@@ -35,7 +36,9 @@ export default async function sendEmailCode(
|
||||
? temp_code_field_name
|
||||
: defaultTempLoginFieldName;
|
||||
|
||||
const emailHtml = `<p>Please use this code to login</p>\n<h2>{{code}}</h2>\n<p>Please note that this code expires after 15 minutes</p>`;
|
||||
const emailHtml =
|
||||
html ||
|
||||
`<p>Please use this code to login</p>\n<h2>{{code}}</h2>\n<p>Please note that this code expires after 15 minutes</p>`;
|
||||
|
||||
const apiSendEmailCodeParams: APISendEmailCodeFunctionParams = {
|
||||
database,
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
import encrypt from "../../../functions/dsql/encrypt";
|
||||
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 {
|
||||
APIGoogleLoginFunctionParams,
|
||||
APIResponseObject,
|
||||
GoogleAuthParams,
|
||||
} from "../../../types";
|
||||
import grabCookieExpiryDate from "../../../utils/grab-cookie-expirt-date";
|
||||
import queryDSQLAPI from "../../../functions/api/query-dsql-api";
|
||||
import grabUserDSQLAPIPath from "../../../utils/backend/users/grab-api-path";
|
||||
import postLoginResponseHandler from "../../../functions/backend/auth/post-login-response-handler";
|
||||
|
||||
/**
|
||||
* # SERVER FUNCTION: Login with google Function
|
||||
@@ -29,9 +26,9 @@ export default async function googleAuth({
|
||||
loginOnly,
|
||||
useLocal,
|
||||
apiVersion,
|
||||
skipWriteAuthFile,
|
||||
cleanupTokens,
|
||||
}: GoogleAuthParams): Promise<APIResponseObject> {
|
||||
const COOKIE_EXPIRY_DATE = grabCookieExpiryDate();
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
@@ -111,36 +108,18 @@ export default async function googleAuth({
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
if (httpResponse?.success && httpResponse?.payload) {
|
||||
let encryptedPayload = encrypt({
|
||||
data: JSON.stringify(httpResponse.payload),
|
||||
encryptionKey: finalEncryptionKey,
|
||||
encryptionSalt: finalEncryptionSalt,
|
||||
});
|
||||
|
||||
const cookieNames = getAuthCookieNames({
|
||||
if (httpResponse?.success && httpResponse?.payload && database) {
|
||||
postLoginResponseHandler({
|
||||
database,
|
||||
httpResponse,
|
||||
cleanupTokens,
|
||||
debug,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
response,
|
||||
secureCookie,
|
||||
skipWriteAuthFile,
|
||||
});
|
||||
|
||||
if (httpResponse.csrf) {
|
||||
writeAuthFile(
|
||||
httpResponse.csrf,
|
||||
JSON.stringify(httpResponse.payload)
|
||||
);
|
||||
}
|
||||
|
||||
httpResponse["cookieNames"] = cookieNames;
|
||||
httpResponse["key"] = String(encryptedPayload);
|
||||
|
||||
const authKeyName = cookieNames.keyCookieName;
|
||||
const csrfName = cookieNames.csrfCookieName;
|
||||
|
||||
response?.setHeader("Set-Cookie", [
|
||||
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;;Expires=${COOKIE_EXPIRY_DATE}${
|
||||
secureCookie ? ";Secure=true" : ""
|
||||
}`,
|
||||
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true;;Expires=${COOKIE_EXPIRY_DATE}`,
|
||||
]);
|
||||
}
|
||||
|
||||
return httpResponse;
|
||||
|
||||
Reference in New Issue
Block a user