node-programmeren4-studentenhuis-master/controllers/authentication.controller.js
/**
     * Authenticate the incoming request by validating the JWT token. 
     * On success, we pass further processing to the next express handler.
     * 
     * https://www.sitepoint.com/using-json-web-tokens-node-js/
     * 
     * @param {*} req The incoming request, should contain valid JWT token in headers.
     * @param {*} res None. The request is passed to next for further processing.
     * @param {*} next ApiError when token is invalid, or req containing logged-in user.
     */
Authenticate the incoming request by validating the JWT token.
On success, we pass further processing to the next express handler.



@param {*} req The incoming request, should contain valid JWT token in headers.
@param {*} res None. The request is passed to next for further processing.
@param {*} next ApiError when token is invalid, or req containing logged-in user.
Authenticate the incoming request by validating the JWT token . On success we pass further processing to the next express handler . @param { * } req The incoming request should contain valid JWT token in headers . @param { * } res None . The request is passed to next for further processing . @param { * } next ApiError when token is invalid or req containing logged - in user .
validateToken(req, res, next) {
        logger.debug('validateToken called')
        const token = req.header('x-access-token') || ''
        auth.decodeToken(token, (err, payload) => {
            if (err) {
                logger.error('auth.decode - ' + err.message || err)
                const error = new ApiError(err.message || err, 401)
                next(error)
            } else {
                logger.trace('Authenticated! Payload = ')
                logger.trace(payload)
                req.user = {
                    id: payload.sub.id
                }
                logger.debug('req.user = ', req.user)
                next()
            }
        })
    }
validateToken ( req , res , next ) { logger . debug ( 'validateToken called' ) const token = req . header ( 'x-access-token' ) || '' auth . decodeToken ( token , ( err , payload ) => { if ( err ) { logger . error ( 'auth.decode - ' + err . message || err ) const error = new ApiError ( err . message || err , 401 ) next ( error ) } else { logger . trace ( 'Authenticated! Payload = ' ) logger . trace ( payload ) req . user = { id : payload . sub . id } logger . debug ( 'req.user = ' , req . user ) next ( ) } } ) }
Authenticate the incoming request by validating the JWT token.
Authenticate the incoming request by validating the JWT token .
/**
         * A token can be sent in the body of a request, via a query parameter (in the URL),
         * or as an HTTP header. We choose the header variant.
         */ // Invalid token /**
                 * The payload contains the values that were put in it via the sub-field.
                 * We could use those in our application to trace actions that a user performs, 
                 * such as monitor CRUD operations, by storing the user ID in a logging database.
                 * Example: User 12345 performed an update operation on item xyz on date dd-mm-yyyy.
                 * To do so, we attach the payload.sub (or only a part of that) to the request object.
                 * In this way, every next express handler has access to it - and could do 
                 * something smart with it.  
                 */