kooyeed / egg-passport-qq

egg-passport plugin for qq-oauth-login

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

egg-passport-qq

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

Install

$ npm i egg-passport-qq --save

Usage

// {app_root}/config/plugin.js
exports.passportQQ = {
  enable: true,
  package: 'egg-passport-qq',
};

Configuration

// {app_root}/config/config.default.js
exports.passportQQ = {
    key: '',
    secret: '',
};

see config/config.default.js for more detail.

Example

  • Why and What: 该插件主要用于使用了 egg-passport 插件的情况下,第三方 qq oauth 协议登录,获取用户信息

  • How: 首先客户端 / web 端需要唤起 qq 应用(web 端是访问 https://graph.qq.com/oauth2.0/authorize )获取登录的 authorize_code 之后在服务端示例代码如下:

        // app/controller/oauth.js
        // 客户端拿到 code 之后,向服务端发起一个请求,服务端收到请求后,根据 code,构造 url,redirect 到那个url
        const url = `/path/you/set/for/qq/oauth?client_id=${QQClientID}&client_secret=${QQClientSecret}&grant_type=authorization_code&code=${yourCode}&redirect_uri=${参考qq开放平台文档注册url}`;
    
        // router.js
        app.get('/path/you/set/for/qq/oauth', app.passport.authenticate('loginByQQ', {
            successRedirect: '/path/for/qq/oauth/callback',
            failureRedirect: '/login',
        }));
        app.get('/path/for/qq/oauth/callback', app.controller.oauth.getDemoInformation);
      
        // app.js
        app.passport.verify(function* (ctx, user) {
            // 在这里可以拿到 user,user 构成看 egg-passport 文档或源码
            // 然后可以对 user 做一些持久化操作
            // 同时这个函数执行完以后,egg-passport 会自动帮我们设置 user 信息到 redis session(如果有)
        })
      
        // app/controller/oauth/getDemoInformation.js
        // 在这里可以获取 session 里面的信息

Questions & Suggestions

Please open an issue here.

License

MIT

About

egg-passport plugin for qq-oauth-login

License:MIT License


Languages

Language:JavaScript 100.0%