lib/localUserMiddleware.js
function isLoggedInMiddleware() {
		return function isLoggedIn(req, res, next) {
			const AclUser = mongoose.model("TransomAclUser");

			if ((req.headers && req.headers.authorization) || (req.body && req.body.access_token) || (req.query && req.query.access_token)) {
				// Verify the authorization token or fail.
				passport.authenticate('bearer', {
					session: false
				}, function (err, user, info) {
					if (err) {
						return next(err);
					}
					if (!user || (info && info.indexOf("invalid_token") >= 0)) {
						// Something didn't work out.  Token Expired / Denied.
						debug("NOT LoggedIn - Invalid");
						return next(new restifyErrors.InvalidCredentialsError("Incorrect or expired credentials"));
					} else {
						// Properly authenticated!
						return next();
					}
				})(req, res, next);
			} else {
				// debug("No Authorization header or query access_token. Trying Anonymous.");
				if (localuser.anonymous !== false) {
					// Attempt anonymous login, if possible;
					AclUser.findOne({
						'username': 'anonymous',
						'active': true
					}, function (err, user) {
						if (err) {
							return next(err);
						}
						if (user) {
							req.locals = req.locals || {};
							req.locals.user = user;
							return next();
						}
						// Oh no, not authenticated
						debug("No bearer token provided or query access_token. No Anonymous user available.");
						return next(new restifyErrors.InvalidCredentialsError("No bearer token provided or query access_token. No Anonymous user available."));
					});
				} else {
					debug("No bearer token provided or query access_token.");
					return next(new restifyErrors.InvalidCredentialsError("No bearer token provided or query access_token."));
				}
			}
		}
	}
function isLoggedInMiddleware() {
		return function isLoggedIn(req, res, next) {
			const AclUser = mongoose.model("TransomAclUser");

			if ((req.headers && req.headers.authorization) || (req.body && req.body.access_token) || (req.query && req.query.access_token)) {
				// Verify the authorization token or fail.
				passport.authenticate('bearer', {
					session: false
				}, function (err, user, info) {
					if (err) {
						return next(err);
					}
					if (!user || (info && info.indexOf("invalid_token") >= 0)) {
						// Something didn't work out.  Token Expired / Denied.
						debug("NOT LoggedIn - Invalid");
						return next(new restifyErrors.InvalidCredentialsError("Incorrect or expired credentials"));
					} else {
						// Properly authenticated!
						return next();
					}
				})(req, res, next);
			} else {
				// debug("No Authorization header or query access_token. Trying Anonymous.");
				if (localuser.anonymous !== false) {
					// Attempt anonymous login, if possible;
					AclUser.findOne({
						'username': 'anonymous',
						'active': true
					}, function (err, user) {
						if (err) {
							return next(err);
						}
						if (user) {
							req.locals = req.locals || {};
							req.locals.user = user;
							return next();
						}
						// Oh no, not authenticated
						debug("No bearer token provided or query access_token. No Anonymous user available.");
						return next(new restifyErrors.InvalidCredentialsError("No bearer token provided or query access_token. No Anonymous user available."));
					});
				} else {
					debug("No bearer token provided or query access_token.");
					return next(new restifyErrors.InvalidCredentialsError("No bearer token provided or query access_token."));
				}
			}
		}
	}
function isLoggedInMiddleware ( ) { return function isLoggedIn ( req , res , next ) { const AclUser = mongoose . model ( "TransomAclUser" ) ; if ( ( req . headers && req . headers . authorization ) || ( req . body && req . body . access_token ) || ( req . query && req . query . access_token ) ) { // Verify the authorization token or fail. passport . authenticate ( 'bearer' , { session : false } , function ( err , user , info ) { if ( err ) { return next ( err ) ; } if ( ! user || ( info && info . indexOf ( "invalid_token" ) >= 0 ) ) { // Something didn't work out.  Token Expired / Denied. debug ( "NOT LoggedIn - Invalid" ) ; return next ( new restifyErrors . InvalidCredentialsError ( "Incorrect or expired credentials" ) ) ; } else { // Properly authenticated! return next ( ) ; } } ) ( req , res , next ) ; } else { // debug("No Authorization header or query access_token. Trying Anonymous."); if ( localuser . anonymous !== false ) { // Attempt anonymous login, if possible; AclUser . findOne ( { 'username' : 'anonymous' , 'active' : true } , function ( err , user ) { if ( err ) { return next ( err ) ; } if ( user ) { req . locals = req . locals || { } ; req . locals . user = user ; return next ( ) ; } // Oh no, not authenticated debug ( "No bearer token provided or query access_token. No Anonymous user available." ) ; return next ( new restifyErrors . InvalidCredentialsError ( "No bearer token provided or query access_token. No Anonymous user available." ) ) ; } ) ; } else { debug ( "No bearer token provided or query access_token." ) ; return next ( new restifyErrors . InvalidCredentialsError ( "No bearer token provided or query access_token." ) ) ; } } } }
Create middleware that requires a valid Bearer token.
Create middleware that requires a valid Bearer token .