Needling Perl Mongers

    @aytacworld/express-module-auth-bearer

    1.0.0 • Public • Published

    @aytacworld/express-module-auth-bearer

    This module is ment to be used with @aytacworld/express.

    It will add bearer functions to check user authentication and authorization.

    Needed modules

    You need to create a verifyBearer module.

    const verifyBearer = (app, config, modules) => {
      return {
        name: 'verifyBearer',
        verify: (token) => {
          return tokenBelongsToUser(token);
        }
      }
    }

    Installation

    using npm

    npm i @aytacworld/express-module-auth-bearer

    using yarn

    yarn add @aytacworld/express-module-auth-bearer

    Usage

    app.js

    const Express = require('@aytacworld/express');
    const ExpressAuthBearer = require('@aytacworld/express-module-auth-bearer');
    const myRoutes = require('./routes');
    const myAppDb = require('./db');
    
    const app = new Express({});
    
    const verifyBearer = (app, config, modules) => ({
        name: 'verifyBearer',
        verify: async (token) => {
          const user = await modules.myAppDb.users.findByAccessToken(token);
          return user;
        }
      });
    
    server.module(myAppDb, ExpressAuthBearer);
    server.route('/', myRoutes);
    
    server.listen();

    routes.js

    const path = require('path');
    const router = require('express').Router();
    
    // login module is added.
    const Router = (config, { auth }) => {
      router.get('/api/products',
        auth.bearer(),
        async (req, res) => {
          res.json({ hello: 'world' });
        });
    
      return router;
    };
    
    module.exports = Router;

    MIT License

    Copyright (c) 2019 Adem Aytaç

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Keywords

    none

    Install

    npm i @aytacworld/express-module-auth-bearer

    DownloadsWeekly Downloads

    2

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    3.5 kB

    Total Files

    3

    Last publish

    Collaborators

    • aytacworld