import { RequestHandler , ErrorRequestHandler } from "express" ; import { db } from "./db" ; import { PWD_CONFIRM_EXPIRES_IN_MS } from "./config" ; export const notFound = ( req , res , next ) => { res . status ( 404 ) . json ( { message : "Not Found" } ) ; } ; export const serverError = ( err , req , res , next ) => { if ( err instanceof SyntaxError ) { return res . status ( 400 ) . json ( { message : "Bad Request" } ) ; } console . error ( err ) ; res . status ( 500 ) . json ( { message : "Server Error" } ) ; } ; export const auth = ( req , res , next ) => { if ( req . session . userId ) { return next ( ) ; } res . status ( 401 ) . json ( { message : "Unauthorized" } ) ; } ; export const guest = ( req , res , next ) => { if ( req . session . userId ) { return res . status ( 403 ) . json ( { message : "Forbidden" } ) ; } next ( ) ; } ; export const verified = ( req , res , next ) => { const { userId } = req . session ; const { verifiedAt } = db . users . find ( ( user ) => user . id === userId ) || { } ; if ( ! verifiedAt ) { return res . status ( 403 ) . json ( { message : "Forbidden" } ) ; } next ( ) ; } ; export const pwdConfirmed = ( req , res , next ) => { const { confirmedAt } = req . session ; if ( ! confirmedAt || confirmedAt + PWD_CONFIRM_EXPIRES_IN_MS <= Date . now ( ) ) { return res . status ( 403 ) . json ( { message : "Forbidden" } ) ; } next ( ) ; } ;