datasquirel/package-shared/actions/users/social/google-auth.ts
Benjamin Toby 20a390e4a8 Updates
2025-07-18 18:34:04 +01:00

127 lines
3.2 KiB
TypeScript

import apiGoogleLogin from "../../../functions/api/users/social/api-google-login";
import {
APIGoogleLoginFunctionParams,
APIResponseObject,
GoogleAuthParams,
} from "../../../types";
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
*/
export default async function googleAuth({
apiKey,
token,
database,
response,
encryptionKey,
encryptionSalt,
additionalFields,
additionalData,
apiUserID,
debug,
secureCookie,
loginOnly,
useLocal,
apiVersion,
skipWriteAuthFile,
cleanupTokens,
}: GoogleAuthParams): Promise<APIResponseObject> {
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 && database) {
postLoginResponseHandler({
database,
httpResponse,
cleanupTokens,
debug,
encryptionKey,
encryptionSalt,
response,
secureCookie,
skipWriteAuthFile,
});
}
return httpResponse;
}