datasquirel/package-shared/actions/users/social/google-auth.ts
Benjamin Toby f9725a1d6f Updates
2025-07-10 12:13:03 +01:00

149 lines
4.2 KiB
TypeScript

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";
/**
* # SERVER FUNCTION: Login with google Function
*/
export default async function googleAuth({
apiKey,
token,
database,
response,
encryptionKey,
encryptionSalt,
additionalFields,
additionalData,
apiUserID,
debug,
secureCookie,
loginOnly,
useLocal,
apiVersion,
}: GoogleAuthParams): Promise<APIResponseObject> {
const COOKIE_EXPIRY_DATE = grabCookieExpiryDate();
const finalEncryptionKey =
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
const finalEncryptionSalt =
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
if (!finalEncryptionKey?.match(/.{8,}/)) {
console.log("Encryption key is invalid");
return {
success: false,
payload: null,
msg: "Encryption key is invalid",
};
}
if (!finalEncryptionSalt?.match(/.{8,}/)) {
console.log("Encryption salt is invalid");
return {
success: false,
payload: null,
msg: "Encryption salt is invalid",
};
}
/**
* Check inputs
*
* @description Check inputs
*/
if (!token || token?.match(/ /)) {
return {
success: false,
payload: null,
msg: "Please enter Google Access Token",
};
}
/**
* Initialize HTTP response variable
*/
let httpResponse: APIResponseObject = {
success: false,
};
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(googleAuthParams);
} else {
httpResponse = await queryDSQLAPI({
path: grabUserDSQLAPIPath({
paradigm: "auth",
action: "google-login",
database,
apiVersion,
}),
apiKey,
body: googleAuthParams,
method: "POST",
});
}
/**
* Make https request
*
* @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({
database,
userId: apiUserID || process.env.DSQL_API_USER_ID,
});
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;
}