npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ne-koa-auth

v1.2.13

Published

登录验证包

Downloads

7

Readme

koa-auth handbook

Koa用法

use jwt

const koaAuth = require('ne-koa-auth');
const {jwt,utils} = koaAuth;

use jwt sign

jwt.sign(payload[,exp]);
    --payload: require,加密的主体
        --_id: require,用户唯一id
        --ip:  require,用户ip地址,例如:ip可以使用 utils.getIp(ctx)得到
        ... 其他任意参数
    --exp: unrequire,失效时间 默认为30m,你可以传入字符串或者数字来规定失效时间

use jwt verify

jwt.verify(token);
    --token: require,签名字符串

use jwt middleware

app.use(jwt.middleware(config[,callback]));
    --config: require,koa config
          --match:xx, unrequire,匹配xx则需要验签,可以为String,Array,RegExp,默认'/'
          --except:xx, unrequire,不匹配xx则需要验签,可以为String,Array,RegExp,如果 "match"和"except"同时存在,则只生效match
    --callback: unrequire 在解签成功后,会触发回调函数,如果回调函数返回值为false,则会抛出401错误
如果签名成功,则会在ctx增加user属性--> ctx.user

Egg用法

在 middleware 文件中增加auth.js(文件名可以任意命名)文件

    'use strict';
    const { jwt } = require('ne-koa-auth');
    module.exports = jwt.middleware;

如果想添加回调函数,则需要小小的改变
    module.exports = options=>{
      return jwt.middleware(options,callback);
    }

在 config.default.js 文件中增加配置

这里(auth)名称要和middleware中的(auth.js)文件名一样,当然你也可以使用任何命名,只要保持一致即可

config={
    middleware:[其余中间件,'auth'],
    auth:{
        match:xx, unrequire,匹配xx则需要验签,可以为String,Array,RegExp,默认'/'
        except:xx, unrequire,不匹配xx则需要验签,可以为String,Array,RegExp,如果 "match"和"except"同时存在,则只生效match
    },
    ip:false  // 不校验ip
}

在controller/serverAdmin.js中的serverAdmin.js中的postLogin方法内
修改签名方式
const { jwt, utils } = require('ne-koa-auth');
....
const token = jwt.sign({
  _id: admin._id,
  ip: utils.getIp(ctx,{xRealIp:false}),
}[,exp]);//在签名的时候,如果想延长签名失效时间,可以传入exp参数