datasquirel/users/validate-token.ts

77 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import http from "http";
import decrypt from "../package-shared/functions/dsql/decrypt";
import { DATASQUIREL_LoggedInUser } from "../package-shared/types";
2023-09-21 14:00:04 +00:00
2025-01-10 19:10:28 +00:00
type Param = {
token: string;
encryptionKey: string;
encryptionSalt: string;
level?: ("deep" | "normal") | null;
database: string;
};
2023-09-21 14:00:04 +00:00
/**
* Validate Token
2024-12-06 11:55:03 +00:00
* ======================================
2023-09-21 14:00:04 +00:00
* @description This Function takes in a encrypted token and returns a user object
*/
2025-01-10 19:10:28 +00:00
export default function validateToken({
token,
encryptionKey,
encryptionSalt,
}: Param): DATASQUIREL_LoggedInUser | null {
2023-09-21 14:00:04 +00:00
try {
/**
* Grab the payload
*
* @description Grab the payload
*/
const key = token;
/**
* Grab the payload
*
* @description Grab the payload
*/
let userPayload = decrypt({
encryptedString: key,
encryptionKey,
encryptionSalt,
});
/**
* Grab the payload
*
* @description Grab the payload
*/
if (!userPayload) {
return null;
}
/**
* Grab the payload
*
* @description Grab the payload
*/
let userObject = JSON.parse(userPayload);
if (!userObject.csrf_k) {
return null;
}
/**
* Return User Object
*
* @description Return User Object
*/
return userObject;
} catch (error) {
/**
* Return User Object
*
* @description Return User Object
*/
return null;
}
}