Permission verification

  • Register a permission verification plugin
my-nodestack-app
├── src
│   ├── routes
│   │   │── auth.js
│   ├── routes
│   │   │── auth      
│   │   │   │── index.js 
├── ndsk.config.js
└── package.json
const Boom = require('@hapi/boom')
exports.plugin  = {
    name: 'auth',
    register: async (server) =>{
        server.auth.scheme('test', ()=>{
            return {
              api: {},
              authenticate: async (request, h) =>{
                if(request.query.username === 'jack'){
                    return h.authenticated({ credentials: {
                        id:1,   
                        user: 'jack'
                    }});
                }else{
                    throw Boom.unauthorized(null, 'Custom');
                }
              }
            }
          });
        server.auth.strategy('auth', 'test');
    }
}
export const options = {
    auth:'auth',
}
export default (request,h)=>{ 
    console.log(request.auth.credentials)
    return `welcome ${request.query.username}`  
} 

Next step

Rate limit