refactor: Update JSONWebToken.sign() method to accept an object with data and expiresInSeconds properties

This commit is contained in:
Simon Larsen
2024-05-04 20:37:58 +01:00
parent b3346a9702
commit bc46370f7c
6 changed files with 40 additions and 28 deletions

View File

@@ -182,10 +182,12 @@ router.post(
savedUser.id!
);
const token: string = JSONWebToken.sign(
savedUser,
OneUptimeDate.getSecondsInDays(new PositiveNumber(30))
);
const token: string = JSONWebToken.sign({
data: savedUser,
expiresInSeconds: OneUptimeDate.getSecondsInDays(
new PositiveNumber(30)
),
});
// Set a cookie with token.
CookieUtil.setCookie(res, CookieUtil.getUserTokenKey(), token, {
@@ -573,10 +575,12 @@ router.post(
alreadySavedUser.password.toString() ===
user.password!.toString()
) {
const token: string = JSONWebToken.sign(
alreadySavedUser,
OneUptimeDate.getSecondsInDays(new PositiveNumber(30))
);
const token: string = JSONWebToken.sign({
data: alreadySavedUser,
expiresInSeconds: OneUptimeDate.getSecondsInDays(
new PositiveNumber(30)
),
});
// Set a cookie with token.
CookieUtil.setCookie(

View File

@@ -62,10 +62,10 @@ router.post(
// if found then generate a token and return it.
const token: string = JSONWebToken.sign(
{ resellerId: resellerId },
OneUptimeDate.getDayInSeconds(365)
);
const token: string = JSONWebToken.sign({
data: { resellerId: resellerId },
expiresInSeconds: OneUptimeDate.getDayInSeconds(365),
});
return Response.sendJsonObjectResponse(req, res, {
access: token,

View File

@@ -367,15 +367,17 @@ const loginUserWithSso: LoginUserWithSsoFunction = async (
req.params['projectId'] as string
);
const token: string = JSONWebToken.sign(
{
const token: string = JSONWebToken.sign({
data: {
userId: alreadySavedUser.id!,
projectId: projectId,
email: email,
isMasterAdmin: false,
},
OneUptimeDate.getSecondsInDays(new PositiveNumber(30))
);
expiresInSeconds: OneUptimeDate.getSecondsInDays(
new PositiveNumber(30)
),
});
// Refresh Permissions for this user here.
await AccessTokenService.refreshUserAllPermissions(

View File

@@ -389,10 +389,12 @@ router.post(
});
if (alreadySavedUser) {
const token: string = JSONWebToken.sign(
alreadySavedUser,
OneUptimeDate.getSecondsInDays(new PositiveNumber(30))
);
const token: string = JSONWebToken.sign({
data: alreadySavedUser,
expiresInSeconds: OneUptimeDate.getSecondsInDays(
new PositiveNumber(30)
),
});
CookieUtil.setCookie(
res,

View File

@@ -270,10 +270,12 @@ router.post(
});
}
const token: string = JSONWebToken.sign(
alreadySavedUser,
OneUptimeDate.getSecondsInDays(new PositiveNumber(30))
);
const token: string = JSONWebToken.sign({
data: alreadySavedUser,
expiresInSeconds: OneUptimeDate.getSecondsInDays(
new PositiveNumber(30)
),
});
CookieUtil.setCookie(
res,

View File

@@ -11,15 +11,17 @@ import StatusPagePrivateUser from 'Model/Models/StatusPagePrivateUser';
import JSONFunctions from 'Common/Types/JSONFunctions';
class JSONWebToken {
public static sign(
public static sign(props: {
data:
| JSONWebTokenData
| User
| StatusPagePrivateUser
| string
| JSONObject,
expiresInSeconds: number
): string {
| JSONObject;
expiresInSeconds: number;
}): string {
const { data, expiresInSeconds } = props;
let jsonObj: JSONObject;
if (typeof data === 'string') {