22import config from '../config/environment' ;
33import jwt from 'jsonwebtoken' ;
44import expressJwt from 'express-jwt' ;
5- import compose from 'composable-middleware' ; < % if ( filters . mongooseModels ) { % >
6- import User from '../api/user/user.model' ; < % } % > < % if ( filters . sequelizeModels ) { % >
5+ import compose from 'composable-middleware' ; < % if ( filters . mongooseModels ) { % >
6+ import User from '../api/user/user.model' ; < % } % > < % if ( filters . sequelizeModels ) { % >
77import { User } from '../sqldb' ; < % } % >
88
99var validateJwt = expressJwt ( {
@@ -19,25 +19,25 @@ export function isAuthenticated() {
1919 // Validate jwt
2020 . use ( function ( req , res , next ) {
2121 // allow access_token to be passed through query parameter as well
22- if ( req . query && req . query . hasOwnProperty ( 'access_token' ) ) {
22+ if ( req . query && req . query . hasOwnProperty ( 'access_token' ) ) {
2323 req . headers . authorization = 'Bearer ' + req . query . access_token ;
2424 }
2525 // IE11 forgets to set Authorization header sometimes. Pull from cookie instead.
26- if ( req . query && typeof req . headers . authorization === 'undefined' ) {
26+ if ( req . query && typeof req . headers . authorization === 'undefined' ) {
2727 req . headers . authorization = 'Bearer ' + req . cookies . token ;
2828 }
2929 validateJwt ( req , res , next ) ;
3030 } )
3131 // Attach user to request
3232 . use ( function ( req , res , next ) {
33- < % if ( filters . mongooseModels ) { % > User . findById ( req . user . _id ) . exec ( ) < % }
34- if ( filters . sequelizeModels ) { % > User . find ( {
33+ < % if ( filters . mongooseModels ) { % > User . findById ( req . user . _id ) . exec ( ) < % }
34+ if ( filters . sequelizeModels ) { % > User . find ( {
3535 where : {
3636 _id : req . user . _id
3737 }
3838 } ) < % } % >
3939 . then ( user => {
40- if ( ! user ) {
40+ if ( ! user ) {
4141 return res . status ( 401 ) . end ( ) ;
4242 }
4343 req . user = user ;
@@ -52,14 +52,14 @@ export function isAuthenticated() {
5252 * Checks if the user role meets the minimum requirements of the route
5353 */
5454export function hasRole ( roleRequired ) {
55- if ( ! roleRequired ) {
55+ if ( ! roleRequired ) {
5656 throw new Error ( 'Required role needs to be set' ) ;
5757 }
5858
5959 return compose ( )
6060 . use ( isAuthenticated ( ) )
6161 . use ( function meetsRequirements ( req , res , next ) {
62- if ( config . userRoles . indexOf ( req . user . role ) >= config . userRoles . indexOf ( roleRequired ) ) {
62+ if ( config . userRoles . indexOf ( req . user . role ) >= config . userRoles . indexOf ( roleRequired ) ) {
6363 return next ( ) ;
6464 } else {
6565 return res . status ( 403 ) . send ( 'Forbidden' ) ;
@@ -80,7 +80,7 @@ export function signToken(id, role) {
8080 * Set token cookie directly for oAuth strategies
8181 */
8282export function setTokenCookie ( req , res ) {
83- if ( ! req . user ) {
83+ if ( ! req . user ) {
8484 return res . status ( 404 ) . send ( 'It looks like you aren\'t logged in, please try again.' ) ;
8585 }
8686 var token = signToken ( req . user . _id , req . user . role ) ;
0 commit comments