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_fe/koa-auth

v1.2.16

Published

登录验证包

Downloads

3

Readme

简介

koa2中间件,主要作用是校验签名.内部集成了jwt的签名,解签及Koa2和egg中间件

安装&配置

npm i @ne_fe/koa-auth -S

使用

引用koa-auth

const { jwt,utils } = require('@ne_fe/koa-auth');

use jwt.sign

jwt.sign(payload[,exp]);

|参数|下级参数|类型|是否必传|说明| |--|--|--|--|--| |payload||Object|true|加密的主体| ||_id|String|true|用户唯一id| ||ip|String|true|用户ip地址,可以使用 utils.getIp(ctx)得到| ||*|Any|false|自定义参数| |exp||String|Number|false|失效时间 默认为30m|

详细的时间参数请见jsonwebtoken

use jwt.verify

jwt.verify(token);

|参数|类型|是否必传|说明| |--|--|--|--| |token|String|true|签名后的字符串|

use jwt middleware

Koa中中间件用法

app.use(jwt.middleware(config[,callback]));
// 如果签名成功,则会在ctx增加user属性--> ctx.user

|参数|下级参数|类型|是否必传|说明| |--|--|--|--|--| |config||Object|true|中间件的配置信息| ||match|String|Array|RegExp|false|url黑名单,只有匹配该名单的url才需要校验,默认'/',即所有url都需要验签| ||except|String|Array|RegExp|false|url白名单,匹配该名单的url不需要校验,如果 "match"和"except"同时存在,则只生效match| ||ip|Boolean|false|校验时,是否需要匹配IP,默认true| |callback||Function|false|在解签成功后,会触发回调函数,如果回调函数返回值为false,则会抛出401错误;callback中可以放置自定义校验规则|

Egg用法

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

const { jwt } = require('@ne_fe/koa-auth');
module.exports = jwt.middleware;

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

在 config.default.js 文件中

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

config={
    middleware:['auth'...],
    // 这里的配置和koa中间件配置一致
    auth:{
        match:'',
        except:'',
        ip:true,
    },
}

将老代码改为使用koa-auth

在controller/serverAdmin.js文件中

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