middleware/mw.auth.token.js
function getUserForToken(decodedToken) {
        var userId = decodedToken._id;
        var authToken = decodedToken.authToken;
        var cacheKey = userId + authToken;
        var conditions = {
            caller: userService.admin,
            where: { _id: userId, authToken: authToken, status: 'created' },
            findOne: true
        };
        var cachedUser;

        // if no user id or authToken, then no user
        if (!userId || !authToken) { return null; }

        // try to get user from cache, then DB
        return userCacheService.get({ key: cacheKey })
            .then(function (user) {
                cachedUser = user;
                return user ? user : userService.find(conditions);
            })
            .then(function (user) {

                // if user found in database, but not in cache, save in cache
                if (user && !cachedUser) {
                    userCacheService.set({ key: cacheKey, value: user });
                }

                // return the user
                return user;
            });
    }
function getUserForToken(decodedToken) {
        var userId = decodedToken._id;
        var authToken = decodedToken.authToken;
        var cacheKey = userId + authToken;
        var conditions = {
            caller: userService.admin,
            where: { _id: userId, authToken: authToken, status: 'created' },
            findOne: true
        };
        var cachedUser;

        // if no user id or authToken, then no user
        if (!userId || !authToken) { return null; }

        // try to get user from cache, then DB
        return userCacheService.get({ key: cacheKey })
            .then(function (user) {
                cachedUser = user;
                return user ? user : userService.find(conditions);
            })
            .then(function (user) {

                // if user found in database, but not in cache, save in cache
                if (user && !cachedUser) {
                    userCacheService.set({ key: cacheKey, value: user });
                }

                // return the user
                return user;
            });
    }
function getUserForToken ( decodedToken ) { var userId = decodedToken . _id ; var authToken = decodedToken . authToken ; var cacheKey = userId + authToken ; var conditions = { caller : userService . admin , where : { _id : userId , authToken : authToken , status : 'created' } , findOne : true } ; var cachedUser ; // if no user id or authToken, then no user if ( ! userId || ! authToken ) { return null ; } // try to get user from cache, then DB return userCacheService . get ( { key : cacheKey } ) . then ( function ( user ) { cachedUser = user ; return user ? user : userService . find ( conditions ) ; } ) . then ( function ( user ) { // if user found in database, but not in cache, save in cache if ( user && ! cachedUser ) { userCacheService . set ( { key : cacheKey , value : user } ) ; } // return the user return user ; } ) ; }
Get a user for a particular token
@param decodedToken
@returns {*}
Get a user for a particular token