Advertisement
Guest User

Untitled

a guest
Jan 10th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. async function superTokensMiddleware(
  2. req: express.Request,
  3. res: express.Response,
  4. next: express.NextFunction
  5. ): Promise<void> {
  6. try {
  7. const cookies = parseCookies(req);
  8. if (cookies!.sAccessToken && cookies!.sIdRefreshToken) {
  9. req.session = await SuperTokens.getSession(req, res, false);
  10. return next();
  11. }
  12. req.session = await SuperTokens.createNewSession(res, 0);
  13. next();
  14. } catch (err) {
  15. if (SuperTokens.Error.isErrorFromAuth(err)) {
  16. if (err.errType === SuperTokens.Error.GENERAL_ERROR) {
  17. next(err);
  18. } else if (err.errType === SuperTokens.Error.UNAUTHORISED) {
  19. req.session = await SuperTokens.createNewSession(res, 0);
  20. next();
  21. } else {
  22. res.status(440).send("Please call the refresh token endpoint");
  23. }
  24. }
  25.  
  26. if (err.errType === 2000) {
  27. return res.redirect("/");
  28. }
  29. next(err);
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement