1.0.0 • Published 3 years ago

teddy-crm-login v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

优品财富统一Web和H5登陆控件

how to use

登录

  1. app.use()即可使用中间件
  2. auth方法提供了两个入参,分别是登录态失效处理方法和是否需要查询用户权限
  3. 使用了auth中间键的路由即可受登录的权限控制,且可以再res.locals中拿到用户信息(res.locals.user)和用户权限(res.locals.module,可定制)。
  4. 不使用auth中间键的路不受登录控制。
  5. auth中间件会处理用户登录失败,并跳转到opt.failed,使用者需要自行实现登录opt.failed方法。
  6. auth中间件不会给客户端回包,用户需要注意
  7. 登录的根域名需要和登录成功回调的域名一致。(目前仅支持upchina.com、whup.com、upchinaproduct.com
  8. 当前版本仅支持H5登录和Web登录,目前不要在优品股票通的APP和PC端中使用,请不要在优品终端容器中使用
  9. 对于首页登录失败的情况,中间件可以直接跳转到登录页面,登录成功后悔自动跳转回来。如果是ajax请求,则不会跳转到登录页面,ajax后续需要自己处理登录失败情况。
  10. 详细内容参照如下Demo
    const crm_login = require('@up/crm-login')
//use crm_login as middleware
const app = require('express')

const opt = {
    failed: funciton (req, res) {
        // only the web server fontpage can excute this function
        // ajax request will not excute this function, you should process login failed yourself
        // go to login web page or go to the frontpage of  current websit

        // suggested uri: http://clogin.test.whup.com/?unique_id=${unique_id}&callback=${callback}&ch=${ch}
        // e.g. http://clogin.test.upchina.com/?unique_id=$unionid&callback=https%3A%2F%2Fwww.upchina.com%2F&ch=${ch}
    },
    need_module: false,         // 登录后是否携带用户权限,默认是不携带,可不传递参数
}

app.use(crm_login.auth(opt), function (req, res, next) {

    //TODO:: do something or send

})

// get user info and user module
app.use('/test', function (req, res, next) {
    console.log(res.locals.user)

    // only when need_module is true, you can get res.locals.module
    console.log(res.locals.module)
})
### 登出

> 1. 模块提供了登出接口auth_out中间件
> 2. **用户需要自行处理登出后页面的去向和回包内容**
> 3. **详情请参照如下demo
app.use('/logout', crm_login.auth_out, function (req, res, next) {
    //TODO:: go to login web page or go to the frontpage of current websit
    res.redirect('http://www.upchina.com/');
});