Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Use number instead of string in ID
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 4, 2020
1 parent 8cae267 commit 0efe1c8
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 137 deletions.
8 changes: 4 additions & 4 deletions src/_staart/helpers/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const can = async (
if (typeof target === "number") {
if (targetType === "membership") {
const membership = await prisma.memberships.findOne({
where: { id: parseInt(target) },
where: { id: target },
});
if (!membership) throw new Error(USER_NOT_FOUND);
target = membership;
Expand All @@ -205,7 +205,7 @@ export const can = async (
target = group;
} else {
// Target is a user
if (requestFromType === "users" && user.id === parseInt(target)) {
if (requestFromType === "users" && user.id === target) {
target = user as users;
} else {
const targetUser = await getUserById(target);
Expand All @@ -217,13 +217,13 @@ export const can = async (

if (requestFromType === "apiKeys") {
const apiKeyDetails = await prisma.apiKeys.findOne({
where: { id: parseInt((user as ApiKeyResponse).id) },
where: { id: (user as ApiKeyResponse).id },
});
if (!apiKeyDetails || !target) throw new Error(INVALID_TOKEN);
return canApiKeyGroup(apiKeyDetails, action as OrgScopes, target as groups);
} else if (requestFromType === "accessTokens") {
const accessTokenDetails = await prisma.accessTokens.findOne({
where: { id: parseInt((user as ApiKeyResponse).id) },
where: { id: (user as ApiKeyResponse).id },
});
if (!accessTokenDetails || !target) throw new Error(INVALID_TOKEN);
return canAccessTokenUser(
Expand Down
10 changes: 5 additions & 5 deletions src/_staart/helpers/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ export const generateToken = (
});

export interface TokenResponse {
id: string;
id: number;
ipAddress?: string;
}
export interface ApiKeyResponse {
id: string;
id: number;
groupId: string;
scopes: string;
jti: string;
Expand All @@ -81,7 +81,7 @@ export interface ApiKeyResponse {
referrerRestrictions?: string;
}
export interface AccessTokenResponse {
id: string;
id: number;
userId: string;
scopes: string;
jti: string;
Expand Down Expand Up @@ -120,13 +120,13 @@ export const couponCodeJwt = (
/**
* Generate a new email verification JWT
*/
export const emailVerificationToken = (id: string) =>
export const emailVerificationToken = (id: number) =>
generateToken({ id }, TOKEN_EXPIRY_EMAIL_VERIFICATION, Tokens.EMAIL_VERIFY);

/**
* Generate a new email verification JWT
*/
export const resendEmailVerificationToken = (id: string) =>
export const resendEmailVerificationToken = (id: number) =>
generateToken({ id }, TOKEN_EXPIRY_EMAIL_VERIFICATION, Tokens.EMAIL_RESEND);

/**
Expand Down
18 changes: 9 additions & 9 deletions src/_staart/rest/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { SudoScopes } from "../interfaces/enum";

export const getAllGroupForUser = async (
tokenUserId: string,
tokenUserId: number,
queryParams: any
) => {
if (await can(tokenUserId, SudoScopes.READ, "sudo"))
Expand All @@ -29,7 +29,7 @@ export const getAllGroupForUser = async (
};

export const getAllUsersForUser = async (
tokenUserId: string,
tokenUserId: number,
queryParams: any
) => {
if (await can(tokenUserId, SudoScopes.READ, "sudo"))
Expand All @@ -41,7 +41,7 @@ export const getAllUsersForUser = async (
};

export const getAllCouponsForUser = async (
tokenUserId: string,
tokenUserId: number,
queryParams: any
) => {
if (await can(tokenUserId, SudoScopes.READ, "sudo"))
Expand All @@ -53,7 +53,7 @@ export const getAllCouponsForUser = async (
};

export const getCouponForUser = async (
tokenUserId: string,
tokenUserId: number,
couponId: string
) => {
if (await can(tokenUserId, SudoScopes.READ, "sudo"))
Expand All @@ -62,7 +62,7 @@ export const getCouponForUser = async (
};

export const updateCouponForUser = async (
tokenUserId: string,
tokenUserId: number,
couponId: string,
data: couponCodesUpdateInput
) => {
Expand All @@ -75,15 +75,15 @@ export const updateCouponForUser = async (
};

export const deleteCouponForUser = async (
tokenUserId: string,
tokenUserId: number,
couponId: string
) => {
if (await can(tokenUserId, SudoScopes.READ, "sudo"))
return prisma.couponCodes.delete({ where: { id: parseInt(couponId) } });
throw new Error(INSUFFICIENT_PERMISSION);
};

export const generateCouponForUser = async (tokenUserId: string, body: any) => {
export const generateCouponForUser = async (tokenUserId: number, body: any) => {
if (!(await can(tokenUserId, SudoScopes.READ, "sudo")))
throw new Error(INSUFFICIENT_PERMISSION);
if (body.jwt)
Expand All @@ -97,7 +97,7 @@ export const generateCouponForUser = async (tokenUserId: string, body: any) => {
};

export const getPaymentEventsForUser = async (
tokenUserId: string,
tokenUserId: number,
body: any
) => {
if (!(await can(tokenUserId, SudoScopes.READ, "sudo")))
Expand All @@ -109,7 +109,7 @@ export const getPaymentEventsForUser = async (
* Get an API key
*/
export const getServerLogsForUser = async (
tokenUserId: string,
tokenUserId: number,
query: {
range?: string;
from?: string;
Expand Down
8 changes: 4 additions & 4 deletions src/_staart/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {

export const validateRefreshToken = async (token: string, locals: Locals) => {
await checkInvalidatedToken(token);
const data = await verifyToken<{ id: string }>(token, Tokens.REFRESH);
const data = await verifyToken<{ id: number }>(token, Tokens.REFRESH);
if (!data.id) throw new Error(USER_NOT_FOUND);
const user = await getUserById(data.id);
if (!user) throw new Error(USER_NOT_FOUND);
Expand Down Expand Up @@ -93,7 +93,7 @@ export const login = async (
};

export const login2FA = async (code: number, token: string, locals: Locals) => {
const data = await verifyToken<{ id: string }>(token, Tokens.TWO_FACTOR);
const data = await verifyToken<{ id: number }>(token, Tokens.TWO_FACTOR);
const user = await getUserById(data.id);
if (!user) throw new Error(USER_NOT_FOUND);
const secret = user.twoFactorSecret;
Expand Down Expand Up @@ -165,7 +165,7 @@ export const register = async (
data: { prefersEmail: { connect: { id: newEmail.id } } },
});
await deleteItemFromCache(`cache_getUserById_${userId}`);
resendToken = await resendEmailVerificationToken(newEmail.id.toString());
resendToken = await resendEmailVerificationToken(newEmail.id);
}
if (locals) await addApprovedLocation(userId, locals.ipAddress);
return { userId, resendToken };
Expand Down Expand Up @@ -295,7 +295,7 @@ export const approveLocation = async (token: string, locals: Locals) => {
};

export const resendEmailVerificationWithToken = async (token: string) => {
const data = await verifyToken<{ id: string }>(token, Tokens.EMAIL_RESEND);
const data = await verifyToken<{ id: number }>(token, Tokens.EMAIL_RESEND);
if (!data.id) throw new Error(USER_NOT_FOUND);
return resendEmailVerification(data.id);
};
Loading

0 comments on commit 0efe1c8

Please sign in to comment.