egg-userservice

2.0.0 • Public • Published

egg-userservice

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Userservice plugin for Egg.

This plugin provides a convention of how your application accesses current user data.

You can define the way to retrieve current user data according to the exact user strategy your application is using.

User data may be stored in:

  • cookie
  • database
  • some kind of service
  • cache system(eg. redis)
  • etc..

Whatever kind of strategy you are using, just configurate it with this plugin, and keep the way of accessing user data unchanged, for a better understanding across the entire whole egg community.


Install

$ npm i egg-userservice

Usage

  • ctx.user: current user data
  • ctx.userId: the user id of current user
  • app.config.userservice.service.getUser(ctx):
  • app.config.userservice.service.getUserId(ctx):

Configuration

Add your userservice configurations to config/config.default.js

exports.userservice = {
  service: {
    async getUser(ctx) {
      // Retrieve your user data from cookie, redis, db, whatever
      // For common web applications using cookie, you may get session id with ctx.cookies
    },
 
    getUserId(ctx) {
      // The way to get userId
      // eg. return ctx.user.userId
    }
  }
}

For complicated applications

The way your application retrieving user data can be complicated, it may be very weird if configurating it in a config file.

A standalone plugin of your own can be a better solution. In this kind of situation, the way of accessing data with ctx.user and ctx.userId should be left unchanged.

License

MIT

Package Sidebar

Install

npm i egg-userservice

Weekly Downloads

0

Version

2.0.0

License

MIT

Last publish

Collaborators

  • atian25
  • dead_horse
  • shaoshuai0102
  • fengmk2
  • popomore
  • eggjs-admin