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

Commit

Permalink
♻️ Use any for res.locals
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 28, 2020
1 parent 71b3129 commit d598a1f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 48 deletions.
9 changes: 6 additions & 3 deletions src/_staart/helpers/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const refreshToken = (id: number) =>

export const postLoginTokens = async (
user: users,
locals: Locals,
locals: Locals | any,
refreshTokenString?: string
) => {
if (!user.id) throw new Error(USER_NOT_FOUND);
Expand Down Expand Up @@ -236,7 +236,7 @@ export const getLoginResponse = async (
user: users,
type: EventType,
strategy: string,
locals: Locals
locals: Locals | any
): Promise<LoginResponse> => {
if (!user.id) throw new Error(USER_NOT_FOUND);
const verifiedEmails = await prisma.emails.findMany({
Expand Down Expand Up @@ -301,7 +301,10 @@ export const invalidateToken = async (token: string) => {
);
};

export const checkIpRestrictions = (apiKey: ApiKeyResponse, locals: Locals) => {
export const checkIpRestrictions = (
apiKey: ApiKeyResponse,
locals: Locals | any
) => {
if (!apiKey.ipRestrictions) return;
if (
!ipRangeCheck(
Expand Down
2 changes: 1 addition & 1 deletion src/_staart/helpers/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const clearSecurityEventsData = () => {
securityEventsData = [];
};

export const trackEvent = (event: Event, locals?: Locals) => {
export const trackEvent = (event: Event, locals?: Locals | any) => {
event.date = new Date();
if (locals) {
event.ipAddress = locals.ipAddress;
Expand Down
35 changes: 24 additions & 11 deletions src/_staart/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ import {
resendEmailVerification,
} from "../services/user.service";

export const validateRefreshToken = async (token: string, locals: Locals) => {
export const validateRefreshToken = async (
token: string,
locals: Locals | any
) => {
await checkInvalidatedToken(token);
const data = await verifyToken<{ id: number }>(token, Tokens.REFRESH);
if (!data.id) throw new Error(USER_NOT_FOUND);
Expand All @@ -53,7 +56,10 @@ export const validateRefreshToken = async (token: string, locals: Locals) => {
return postLoginTokens(user, locals, token);
};

export const invalidateRefreshToken = async (token: string, locals: Locals) => {
export const invalidateRefreshToken = async (
token: string,
locals: Locals | any
) => {
const data = await verifyToken<{ id: number }>(token, Tokens.REFRESH);
if (!data.id) throw new Error(USER_NOT_FOUND);
await prisma.sessions.deleteMany({
Expand All @@ -65,7 +71,7 @@ export const invalidateRefreshToken = async (token: string, locals: Locals) => {
export const login = async (
email: string,
password: string,
locals: Locals
locals: Locals | any
) => {
let user: users;
try {
Expand Down Expand Up @@ -99,7 +105,11 @@ export const login = async (
throw new Error(INVALID_LOGIN);
};

export const login2FA = async (code: number, token: string, locals: Locals) => {
export const login2FA = async (
code: number,
token: string,
locals: Locals | any
) => {
const data = await verifyToken<{ id: number }>(token, Tokens.TWO_FACTOR);
const user = await getUserById(data.id);
if (!user) throw new Error(USER_NOT_FOUND);
Expand All @@ -126,7 +136,7 @@ export const login2FA = async (code: number, token: string, locals: Locals) => {

export const register = async (
user: usersCreateInput,
locals?: Locals,
locals?: Locals | any,
email?: string,
groupId?: number,
role?: MembershipRole,
Expand Down Expand Up @@ -178,7 +188,10 @@ export const register = async (
return { userId, resendToken };
};

export const sendPasswordReset = async (email: string, locals?: Locals) => {
export const sendPasswordReset = async (
email: string,
locals?: Locals | any
) => {
const user = await getUserByEmail(email);
const token = await passwordResetToken(user.id);
await mail({
Expand Down Expand Up @@ -215,7 +228,7 @@ export const sendNewPassword = async (userId: number, email: string) => {
return;
};

export const verifyEmail = async (token: string, locals: Locals) => {
export const verifyEmail = async (token: string, locals: Locals | any) => {
const emailId = (
await verifyToken<{ id: number }>(token, Tokens.EMAIL_VERIFY)
).id;
Expand All @@ -237,7 +250,7 @@ export const verifyEmail = async (token: string, locals: Locals) => {
});
};

export const loginLink = async (token: string, locals: Locals) => {
export const loginLink = async (token: string, locals: Locals | any) => {
const userId = (await verifyToken<{ id: number }>(token, Tokens.LOGIN_LINK))
.id;
const user = await prisma.users.findOne({
Expand All @@ -258,7 +271,7 @@ export const loginLink = async (token: string, locals: Locals) => {
export const updatePassword = async (
token: string,
password: string,
locals: Locals
locals: Locals | any
) => {
const userId = (
await verifyToken<{ id: number }>(token, Tokens.PASSWORD_RESET)
Expand All @@ -281,7 +294,7 @@ export const updatePassword = async (
export const impersonate = async (
tokenUserId: number,
impersonateUserId: number,
locals: Locals
locals: Locals | any
) => {
if (
!(await can(tokenUserId, UserScopes.IMPERSONATE, "user", impersonateUserId))
Expand All @@ -294,7 +307,7 @@ export const impersonate = async (
throw new Error(USER_NOT_FOUND);
};

export const approveLocation = async (token: string, locals: Locals) => {
export const approveLocation = async (token: string, locals: Locals | any) => {
const tokenUser = await verifyToken<TokenResponse>(
token,
Tokens.APPROVE_LOCATION
Expand Down
42 changes: 21 additions & 21 deletions src/_staart/rest/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const getGroupForUser = async (
export const newGroupForUser = async (
userId: number,
group: groupsCreateInput,
locals: Locals
locals: Locals | any
) => {
if (!(group.name || "").trim()) {
const user = await getUserById(userId);
Expand All @@ -104,7 +104,7 @@ export const updateGroupForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
data: groupsUpdateInput,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.UPDATE_ORG, "group", groupId)) {
const result = await prisma.groups.update({
Expand All @@ -123,7 +123,7 @@ export const updateGroupForUser = async (
export const deleteGroupForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.DELETE_ORG, "group", groupId)) {
const groupDetails = await getGroupById(groupId);
Expand Down Expand Up @@ -168,7 +168,7 @@ export const updateGroupBillingForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
data: any,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.UPDATE_ORG_BILLING, "group", groupId)) {
const group = await getGroupById(groupId);
Expand Down Expand Up @@ -317,7 +317,7 @@ export const updateGroupSubscriptionForUser = async (
groupId: number,
subscriptionId: string,
data: KeyValue,
locals?: Locals
locals?: Locals | any
) => {
if (await can(userId, OrgScopes.UPDATE_ORG_SUBSCRIPTIONS, "group", groupId)) {
const group = await getGroupById(groupId);
Expand Down Expand Up @@ -348,7 +348,7 @@ export const createGroupSubscriptionForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
params: { plan: string; [index: string]: any },
locals?: Locals
locals?: Locals | any
) => {
if (await can(userId, OrgScopes.CREATE_ORG_SUBSCRIPTIONS, "group", groupId)) {
const group = await getGroupById(groupId);
Expand Down Expand Up @@ -387,7 +387,7 @@ export const deleteGroupSourceForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
sourceId: string,
locals?: Locals
locals?: Locals | any
) => {
if (await can(userId, OrgScopes.DELETE_ORG_SOURCES, "group", groupId)) {
const group = await getGroupById(groupId);
Expand Down Expand Up @@ -418,7 +418,7 @@ export const updateGroupSourceForUser = async (
groupId: number,
sourceId: string,
data: any,
locals?: Locals
locals?: Locals | any
) => {
if (await can(userId, OrgScopes.UPDATE_ORG_SOURCES, "group", groupId)) {
const group = await getGroupById(groupId);
Expand Down Expand Up @@ -449,7 +449,7 @@ export const createGroupSourceForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
card: any,
locals?: Locals
locals?: Locals | any
) => {
if (await can(userId, OrgScopes.CREATE_ORG_SOURCES, "group", groupId)) {
const group = await getGroupById(groupId);
Expand Down Expand Up @@ -577,7 +577,7 @@ export const deleteGroupMembershipForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
membershipId: number,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.DELETE_ORG_MEMBERSHIPS, "group", groupId)) {
const members = await prisma.memberships.findMany({
Expand All @@ -596,7 +596,7 @@ export const inviteMemberToGroup = async (
newMemberName: string,
newMemberEmail: string,
role: MembershipRole,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.CREATE_ORG_MEMBERSHIPS, "group", groupId)) {
const group = await getGroupById(groupId);
Expand Down Expand Up @@ -722,7 +722,7 @@ export const updateApiKeyForUser = async (
groupId: number,
apiKeyId: number,
data: apiKeysUpdateInput,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.UPDATE_ORG_API_KEYS, "group", groupId)) {
const result = await prisma.apiKeys.update({
Expand All @@ -740,7 +740,7 @@ export const createApiKeyForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
apiKey: apiKeysCreateInput,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.CREATE_ORG_API_KEYS, "group", groupId)) {
apiKey.apiKey = randomString({ length: 20 });
Expand All @@ -766,7 +766,7 @@ export const deleteApiKeyForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
apiKeyId: number,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.DELETE_ORG_API_KEYS, "group", groupId)) {
const result = await prisma.apiKeys.delete({
Expand Down Expand Up @@ -810,7 +810,7 @@ export const updateDomainForUser = async (
groupId: number,
domainId: number,
data: domainsUpdateInput,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.UPDATE_ORG_DOMAINS, "group", groupId)) {
const result = await prisma.domains.update({
Expand All @@ -828,7 +828,7 @@ export const createDomainForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
domain: domainsCreateInput,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.CREATE_ORG_DOMAINS, "group", groupId)) {
await checkDomainAvailability(domain.domain);
Expand All @@ -855,7 +855,7 @@ export const deleteDomainForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
domainId: number,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.DELETE_ORG_DOMAINS, "group", groupId)) {
const result = await prisma.domains.delete({
Expand All @@ -873,7 +873,7 @@ export const verifyDomainForUser = async (
groupId: number,
domainId: number,
method: "dns" | "file",
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.VERIFY_ORG_DOMAINS, "group", groupId)) {
const domain = await prisma.domains.findOne({
Expand Down Expand Up @@ -957,7 +957,7 @@ export const updateWebhookForUser = async (
groupId: number,
webhookId: number,
data: webhooksUpdateInput,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.UPDATE_ORG_WEBHOOKS, "group", groupId)) {
const result = await prisma.webhooks.update({
Expand All @@ -975,7 +975,7 @@ export const createWebhookForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
webhook: webhooksCreateInput,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.DELETE_ORG_WEBHOOKS, "group", groupId)) {
const result = await prisma.webhooks.create({
Expand All @@ -1002,7 +1002,7 @@ export const deleteWebhookForUser = async (
userId: number | ApiKeyResponse,
groupId: number,
webhookId: number,
locals: Locals
locals: Locals | any
) => {
if (await can(userId, OrgScopes.CREATE_ORG_WEBHOOKS, "group", groupId)) {
const result = prisma.webhooks.delete({
Expand Down
Loading

0 comments on commit d598a1f

Please sign in to comment.