Giter Club home page Giter Club logo

current-user's Introduction

hapi-current-user

This plugin supprots only Hapi and assumes you are using hapi-auth-jwt2 for authentication

current-user plugin adds property currentUser (can be modified) to request object. Now, every handler will have current user

{
  method: 'GET',
  path: '/api/v1/user',
  handler: (request, handler) => {
    return request.currentUser;
  }
}

Usage

  1. Define Plugin options

    import { User } from "/path/to/models";
    
    module.exports = {
      model: User,
      authColumn: "email"
    };
  2. Register plugin in server.js

    import currentUser from "hapi-current-user";
    import currentUserOptions from "hapi-current-user";
    
    await server.register({
      plugin: currentUser,
      options: currentUserOptions
    });
  3. In handler you have

    // file: routes.js
    [
      {
        method: "GET",
        path: "/api/v1/user",
        handler: (request, handler) => {
          return request.currentUser;
          /** Response
           * {
           *   id: 1,
           *   name: 'John Doe',
           *   email: '[email protected]',
           *   ...
           * }
           */
        }
      }
    ];

Example

import currentUser from "hapi-current-user";
import loginuser from "/path/to/loginUser";
import { User } from "/path/to/models";

server.route([
  {
    method: "POST",
    path: "/api/v1/authenticate",
    handler: (request, handler) => {
      // hapi-auth-jwt2 is implemented
      // in loginUser
      return loginUser(request);
    },
    options: {
      auth: false
    }
  },
  {
    method: "GET",
    path: "/api/v1/user",
    handler: (request, handler) => {
      return request.currentUser;
      /** Response
       * {
       *   id: 1,
       *   name: 'John Doe',
       *   email: '[email protected]',
       *   ...
       * }
       */
    }
  }
]);
.
.
.
// register currentUser after hapi-auth-jwt2

await server.register({
  plugin: currentUser,
  options: {
    model: User,
    authColumn: "email"
  }
});
.
.
.

Contributing

Feel free to contribute

LICENSE

package licensed under MIT License

Social

Twitter Follow

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.