Koa middleware and API SDK integrating Wechat, QQ, Baidu and Weibo
koa2 middleware, developers can obtain users' basic information (including user number, nickname, avatar) through this middleware
npm install l-passport -S
Introduce l-passport and configure it
const passport = require ( 'l-passport' ) ;
// 微信登录:设置appId与app secret
passport . initialize ( {
provider : 'wechat'
appId : 'your_app_id' ,
appSecret : 'your_app_secret'
} ) ;
router . get ( '/login/wechat' , passport . authorization ( 'wechat' ) , async ( ctx ) => {
ctx . body = ctx . state . passport ;
} ) ;
If you need to configure multiple platforms (such as web, ios, android), it is recommended to refer to the following code
const passport = require ( 'l-passport' ) ;
passport . initialize ( {
provider : 'wechat' ,
clients : [
{ platform : 'web' , appId : 'your_app_id' , appSecret : 'your_app_secret' } ,
{ platform : 'ios' , appId : 'your_app_id' , appSecret : 'your_app_secret' } ,
{ platform : 'android' , appId : 'your_app_id' , appSecret : 'your_app_secret' } ,
]
} ) ;
router . get ( '/login/wechat_web' , passport . authorization ( 'wechat' , { platform : 'web' } ) , async ( ctx ) => {
ctx . body = ctx . state . passport ;
} ) ;
router . get ( '/login/wechat_ios' , passport . authorization ( 'wechat' , { platform : 'ios' } ) , async ( ctx ) => {
ctx . body = ctx . state . passport ;
} ) ;
router . get ( '/login/wechat_android' , passport . authorization ( 'wechat' , { platform : 'android' } ) , async ( ctx ) => {
ctx . body = ctx . state . passport ;
} ) ;
If you need to configure multiple service providers and multiple platforms, it is recommended to refer to the following code
const passport = require ( 'l-passport' ) ;
passport . initialize ( [
{
provider : 'wechat' ,
clients : [
{ platform : 'web' , appId : 'your_app_id' , appSecret : 'your_app_secret' } ,
{ platform : 'ios' , appId : 'your_app_id' , appSecret : 'your_app_secret' } ,
{ platform : 'android' , appId : 'your_app_id' , appSecret : 'your_app_secret' } ,
]
} ,
{
provider : 'baidu' ,
clients : [
{ platform : 'web' , appId : 'your_app_id' , appSecret : 'your_app_secret' , redirect : 'your_baidu_redirect' } ,
{ platform : 'ios' , appId : 'your_app_id' , appSecret : 'your_app_secret' , redirect : 'your_baidu_redirect' } ,
{ platform : 'android' , appId : 'your_app_id' , appSecret : 'your_app_secret' , redirect : 'your_baidu_redirect' } ,
]
}
] ) ;
router . get ( '/login/wechat_web' , passport . authorization ( 'wechat' , { platform : 'web' } ) , async ( ctx ) => {
ctx . body = ctx . state . passport ;
} ) ;
router . get ( '/login/baidu_ios' , passport . authorization ( 'baidu' , { platform : 'ios' } ) , async ( ctx ) => {
ctx . body = ctx . state . passport ;
} ) ;
provider
: - service provider (required)appId
: - Application ID (required)appSecret
: - Application secret (required)platform
: - service platform (optional)redirect
: - application callback address (optional)scope
: - the scope of permission applied for (optional)state
: - The current state of the application, you can specify any value, the service provider will return this value unchanged (optional)Note: Different service providers handle callback addresses differently during authentication. For example, WeChat does not check the callback function, Weibo and QQ only need to check the domain name of the callback function, and Baidu needs to check including Query The entire callback address including parameters
l-passport supports two ways to set the callback function
1.Configuration settings
const passport = require ( 'l-passport' ) ;
passport . initialize ( {
provider : 'baidu'
appId : 'your_app_id' ,
appSecret : 'your_app_secret' ,
redirect : 'your_app_redirect' ,
state : 'your_app_state' ,
scope : 'your_app_scope'
} ) ;
router . get ( '/login/baidu' , passport . authorization ( 'baidu' ) , async ( ctx ) => {
ctx . body = ctx . state . passport ;
} ) ;
2. Dynamic settings
Put redirect, state, and scope in the Query parameters of the route, such as /login/baidu?redirect=your_redirect&state=your_state&scope=your_scope
l-passport has been integrated as follows:
- 1.qq: QQ login
- 2.baidu: Baidu login
- 3.weibo: Weibo login
- 4.wechat: WeChat login
If developers feel that the currently integrated login strategy cannot meet their needs, they can expand it themselves. Its basic form is as follows:
class YourStragety {
// 服务提供商提供的授权地址
getAuthorizeUrl ( redirect , state , scope ) { }
// 用户通过授权后的认证过程
authorize ( code ) { }
}
passport . use ( 'your_stagety_name' , YourStragety ) ;
After the authentication is completed, the user information will be mounted in ctx.state.passport. Its basic format is as follows:
{
"provider" : "服务提供商" ,
"uid" : "用户编号" ,
"nickname" : "用户昵称" ,
"avatar" : "用户头像" ,
"body" : {}
}