Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix express session types based on latest definitions #2463

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
"@types/express": "^4.17.13",
"@types/express-rate-limit": "^5.1.3",
"@types/express-request-id": "^1.4.2",
"@types/express-session": "^1.17.0",
"@types/express-session": "^1.17.4",
"@types/has-ansi": "^3.0.0",
"@types/helmet": "4.0.0",
"@types/ip": "^1.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/app/modules/auth/auth.middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AuthedSessionData } from 'express-session'
import { StatusCodes } from 'http-status-codes'

import { createLoggerWithLabel } from '../../config/logger'
Expand Down Expand Up @@ -33,7 +34,7 @@ export const logAdminAction: ControllerHandler<{ formId: string }> = async (
res,
next,
) => {
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id
const body = req.body
const method = req.method
const query = req.query
Expand Down
7 changes: 4 additions & 3 deletions src/app/modules/auth/auth.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AuthedSessionData, SessionData } from 'express-session'
import { StatusCodes } from 'http-status-codes'

import { MapRouteError } from '../../../types/routing'
Expand Down Expand Up @@ -52,13 +53,13 @@ export const mapRouteError: MapRouteError = (error, coreErrorMessage) => {
}

export const isUserInSession = (
session?: Express.Session,
): session is Express.AuthedSession => {
session?: SessionData,
): session is AuthedSessionData => {
return !!session?.user?._id
}

export const getUserIdFromSession = (
session?: Express.Session,
session?: SessionData,
): string | undefined => {
return session?.user?._id as string | undefined
}
3 changes: 2 additions & 1 deletion src/app/modules/billing/billing.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AuthedSessionData } from 'express-session'
import { StatusCodes } from 'http-status-codes'
import moment from 'moment-timezone'

Expand Down Expand Up @@ -25,7 +26,7 @@ export const handleGetBillInfo: ControllerHandler<
BillingQueryDto
> = async (req, res) => {
const { esrvcId, mth, yr } = req.query
const authedUser = (req.session as Express.AuthedSession).user
const authedUser = (req.session as AuthedSessionData).user

const startOfMonth = moment
.tz([parseInt(yr), parseInt(mth)], 'Asia/Singapore')
Expand Down
69 changes: 35 additions & 34 deletions src/app/modules/form/admin-form/admin-form.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import JoiDate from '@joi/date'
import { celebrate, Joi as BaseJoi, Segments } from 'celebrate'
import { AuthedSessionData } from 'express-session'
import { StatusCodes } from 'http-status-codes'
import JSONStream from 'JSONStream'
import { ResultAsync } from 'neverthrow'
Expand Down Expand Up @@ -199,7 +200,7 @@ export const handleListDashboardForms: ControllerHandler<
unknown,
AdminDashboardFormMetaDto[] | ErrorDto
> = async (req, res) => {
const authedUserId = (req.session as Express.AuthedSession).user._id
const authedUserId = (req.session as AuthedSessionData).user._id

return AdminFormService.getDashboardForms(authedUserId)
.map((dashboardView) => res.json(dashboardView))
Expand Down Expand Up @@ -233,7 +234,7 @@ export const handleGetAdminForm: ControllerHandler<{ formId: string }> = (
res,
) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -279,7 +280,7 @@ export const handleGetFormCollaborators: ControllerHandler<
PermissionsUpdateDto | ErrorDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -327,7 +328,7 @@ export const handlePreviewAdminForm: ControllerHandler<{ formId: string }> = (
res,
) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id
return (
// Step 1: Retrieve currently logged in user.
UserService.getPopulatedUserById(sessionUserId)
Expand Down Expand Up @@ -383,7 +384,7 @@ export const createPresignedPostUrlForImages: ControllerHandler<
> = async (req, res) => {
const { formId } = req.params
const { fileId, fileMd5Hash, fileType } = req.body
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -448,7 +449,7 @@ export const createPresignedPostUrlForLogos: ControllerHandler<
> = async (req, res) => {
const { formId } = req.params
const { fileId, fileMd5Hash, fileType } = req.body
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -521,7 +522,7 @@ export const countFormSubmissions: ControllerHandler<
> = async (req, res) => {
const { formId } = req.params
const { startDate, endDate } = req.query
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

const logMeta = {
action: 'handleCountFormSubmissions',
Expand Down Expand Up @@ -596,7 +597,7 @@ export const handleCountFormFeedback: ControllerHandler<
number | ErrorDto
> = async (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -645,7 +646,7 @@ export const handleStreamFormFeedback: ControllerHandler<{
formId: string
}> = async (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
const hasReadPermissionResult = await UserService.getPopulatedUserById(
Expand Down Expand Up @@ -740,7 +741,7 @@ export const handleGetFormFeedback: ControllerHandler<
FormFeedbackMetaDto | ErrorDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return UserService.getPopulatedUserById(sessionUserId)
.andThen((user) =>
Expand Down Expand Up @@ -784,7 +785,7 @@ export const handleArchiveForm: ControllerHandler<{ formId: string }> = async (
res,
) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -837,7 +838,7 @@ export const duplicateAdminForm: ControllerHandler<
DuplicateFormBodyDto
> = (req, res) => {
const { formId } = req.params
const userId = (req.session as Express.AuthedSession).user._id
const userId = (req.session as AuthedSessionData).user._id
const overrideParams = req.body

return (
Expand Down Expand Up @@ -901,7 +902,7 @@ export const handleGetTemplateForm: ControllerHandler<
PreviewFormViewDto | ErrorDto | PrivateFormErrorDto
> = (req, res) => {
const { formId } = req.params
const userId = (req.session as Express.AuthedSession).user._id
const userId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve form only if form is currently public.
Expand Down Expand Up @@ -961,7 +962,7 @@ export const handleCopyTemplateForm: ControllerHandler<
DuplicateFormBodyDto
> = (req, res) => {
const { formId } = req.params
const userId = (req.session as Express.AuthedSession).user._id
const userId = (req.session as AuthedSessionData).user._id
const overrideParams = req.body

return (
Expand Down Expand Up @@ -1023,7 +1024,7 @@ export const transferFormOwnership: ControllerHandler<
> = (req, res) => {
const { formId } = req.params
const { email: newOwnerEmail } = req.body
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -1082,7 +1083,7 @@ export const createForm: ControllerHandler<
{ form: CreateFormBodyDto }
> = async (req, res) => {
const { form: formParams } = req.body
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -1139,7 +1140,7 @@ export const handleUpdateForm: ControllerHandler<
> = (req, res) => {
const { formId } = req.params
const { form: formUpdateParams } = req.body
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return UserService.getPopulatedUserById(sessionUserId)
Expand Down Expand Up @@ -1207,7 +1208,7 @@ export const handleDuplicateFormField: ControllerHandler<
FormFieldDto | ErrorDto
> = (req, res) => {
const { formId, fieldId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return UserService.getPopulatedUserById(sessionUserId)
Expand Down Expand Up @@ -1261,7 +1262,7 @@ export const handleUpdateSettings: ControllerHandler<
SettingsUpdateDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id
const settingsToPatch = req.body

// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -1309,7 +1310,7 @@ export const _handleUpdateFormField: ControllerHandler<
FieldUpdateDto
> = (req, res) => {
const { formId, fieldId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
Expand Down Expand Up @@ -1364,7 +1365,7 @@ export const handleGetSettings: ControllerHandler<
FormSettings | ErrorDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return UserService.getPopulatedUserById(sessionUserId)
.andThen((user) =>
Expand Down Expand Up @@ -1410,7 +1411,7 @@ export const submitEncryptPreview: ControllerHandler<
EncryptSubmissionDto
> = async (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id
// No need to process attachments as we don't do anything with them
const { encryptedContent, responses, version } = req.body
const logMeta = {
Expand Down Expand Up @@ -1505,7 +1506,7 @@ export const submitEmailPreview: ControllerHandler<
{ captchaResponse?: unknown }
> = async (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id
// No need to process attachments as we don't do anything with them
const { responses } = req.body
const logMeta = {
Expand Down Expand Up @@ -1697,7 +1698,7 @@ export const _handleCreateFormField: ControllerHandler<
FieldCreateDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
Expand Down Expand Up @@ -1752,7 +1753,7 @@ export const _handleCreateLogic: ControllerHandler<
> = (req, res) => {
const { formId } = req.params
const createLogicBody = req.body
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
Expand Down Expand Up @@ -1853,7 +1854,7 @@ export const handleDeleteLogic: ControllerHandler<{
logicId: string
}> = (req, res) => {
const { formId, logicId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
Expand Down Expand Up @@ -1935,7 +1936,7 @@ export const _handleReorderFormField: ControllerHandler<
> = (req, res) => {
const { formId, fieldId } = req.params
const { to } = req.query
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
Expand Down Expand Up @@ -2003,7 +2004,7 @@ export const _handleUpdateLogic: ControllerHandler<
> = (req, res) => {
const { formId, logicId } = req.params
const updatedLogic = { ...req.body }
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
Expand Down Expand Up @@ -2079,7 +2080,7 @@ export const handleDeleteFormField: ControllerHandler<
ErrorDto | void
> = (req, res) => {
const { formId, fieldId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -2132,7 +2133,7 @@ export const _handleUpdateEndPage: ControllerHandler<
EndPageUpdateDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
Expand Down Expand Up @@ -2202,7 +2203,7 @@ export const handleGetFormField: ControllerHandler<
ErrorDto | FormFieldDto
> = (req, res) => {
const { formId, fieldId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

return (
// Step 1: Retrieve currently logged in user.
Expand Down Expand Up @@ -2256,7 +2257,7 @@ export const _handleUpdateCollaborators: ControllerHandler<
PermissionsUpdateDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id
// Step 1: Get the form after permission checks
return (
UserService.getPopulatedUserById(sessionUserId)
Expand Down Expand Up @@ -2329,7 +2330,7 @@ export const handleRemoveSelfFromCollaborators: ControllerHandler<
PermissionsUpdateDto | ErrorDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id
let currentUserEmail = ''
// Step 1: Get the form after permission checks
return (
Expand Down Expand Up @@ -2393,7 +2394,7 @@ export const _handleUpdateStartPage: ControllerHandler<
StartPageUpdateDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
Expand Down
Loading