This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create middleware specitif file for errorHandling, sessions…
…, cors (#2342)
- Loading branch information
1 parent
c0c7d0f
commit 77bf617
Showing
4 changed files
with
69 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const errorHandler = (err, req, res, next) => { | ||
if (err.name === 'UnauthorizedError') { | ||
// redirect to login and keep the requested url in the '?next=' query param | ||
if (req.method === 'GET') { | ||
req.flash( | ||
'message', | ||
'Pour accéder à cette page vous devez vous identifier, vous pouvez le faire en renseignant votre email juste en dessous.' | ||
); | ||
const nextParam = req.url ? `?next=${req.url}` : ''; | ||
return res.redirect(`/login${nextParam}`); | ||
} | ||
} | ||
return next(err); | ||
}; | ||
|
||
export { errorHandler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import session from 'express-session'; | ||
import makeSessionStore from '@/infra/sessionStore/sessionStore'; | ||
import config from '@/config'; | ||
|
||
const setupSessionMiddleware = (app) => { | ||
app.use( | ||
session({ | ||
store: process.env.NODE_ENV !== 'test' ? makeSessionStore() : null, | ||
secret: config.secret, | ||
resave: false, // required: force lightweight session keep alive (touch) | ||
saveUninitialized: false, // recommended: only save session when data exists | ||
unset: 'destroy', | ||
proxy: true, // Required for Heroku & Digital Ocean (regarding X-Forwarded-For) | ||
name: 'espaceMembreCookieName', | ||
cookie: { | ||
maxAge: 24 * 60 * 60 * 1000 * 7, | ||
httpOnly: true, | ||
secure: process.env.NODE_ENV === 'production' ? true : false, | ||
sameSite: 'lax', | ||
}, | ||
}) | ||
); // Only used for Flash not safe for others purposes | ||
}; | ||
|
||
export { setupSessionMiddleware }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import config from '@/config'; | ||
|
||
var whitelist = config.CORS_ORIGIN; | ||
|
||
const corsOptions = { | ||
origin: function (origin, callback) { | ||
if ( | ||
whitelist.indexOf(origin) !== -1 || | ||
process.env.NODE_ENV === 'test' || | ||
!origin | ||
) { | ||
callback(null, true); | ||
} else { | ||
callback(new Error('Not allowed by CORS')); | ||
} | ||
}, | ||
credentials: true, | ||
methods: 'POST, PUT, OPTIONS, DELETE, GET', | ||
allowedHeaders: | ||
'Origin, X-Requested-With, Content-Type, Accept, Authorization, X-HTTP-Method-Override, Set-Cookie, Cookie', | ||
}; | ||
|
||
export { corsOptions }; |