egg-passport-dingtalk
dingtalk passport plugin for egg
Install
$ npm i egg-passport egg-passport-dingtalk --save
Usage
// {app_root}/config/plugin.jsexportspassport = enable: true package: 'egg-passport' exportspassportDingtalk = enable: true package: 'egg-passport-dingtalk';
Configuration
apply the app from https://open-dev.dingtalk.com/#/loginAndShareApp
// {app_root}/config/config.default.jsexportspassportDingtalk = key: 'your-key' secret: 'your-secret' // default is /callback/dingtalk callbackURL: '/callback/dingtalk' // default is /passport/dingtalk loginURL: '/passport/dingtalk' oauthPageConfig: title: 'your-dingtalk-oauth-page-title' logo: 'your-app-icon' slogan: 'your-app-slogan' // Your custom dingtalk oauth page, if this is set, the default oauthPageConfig will be invalid customLoginURL: '/auth' ;
see config/config.default.js for more detail.
// {app_root}/config/config.default.jsexportspassportDingtalk = ;
key | value | note |
---|---|---|
key | string | required |
secret | string | required |
callbackURL | string default is '/callback/dingtalk' | optional |
loginURL | string default is '/passport/dingtalk' | optional |
oauthPageConfig.title | string | optional |
oauthPageConfig.logo | string | optional |
oauthPageConfig.slogan | string | optional |
customLoginURL | string | optional If this is set, then you should render the oauth page by yourself |
Example
Login with dingtalk
// app/router.jsmodule { app; // authenticates routers apppassport; // this is a passport router helper, it's equal to the below codes // // const dingtalk = app.passport.authenticate('dingtalk'); // app.get('/passport/dingtalk', dingtalk); // app.get('/passport/dingtalk/callback', dingtalk);};
Notice: The dingtalk is different with github or twitter, it need to be rendered by yourself. Luckily, we provide a nice default page for you.
Authenticate Requests
Usually, you could write a middleware to handle if a request need to be login, example will like below:
// app/router.jsmodule { app;};
About how to write a middleware, please refer this
// app/middleware/need-login.jsmodule { return { const passportDingtalkConfig = appconfigpassportDingtalk; if !ctx return ctx; await ; }}
Then you visit the path '/', it will redirect you to the login page;
Integrated with DB verify or serializeUser
// app/router.jsmodule { app; // authenticates routers apppassport; apppassport;};
passport API supported
See https://github.com/eggjs-community/egg-passport#apis.
application
extent app.passport.mount(strategy, options)
: Mount the login and the login callback routers to use the givenstrategy
.app.passport.authenticate(strategy, options)
: Create a middleware that will authorize a third-party account using the givenstrategy
name, with optionaloptions
.app.passport.verify(handler)
: Verify authenticated userapp.passport.serializeUser(handler)
: Serialize user before store into sessionapp.passport.deserializeUser(handler)
: Deserialize user after restore from session
context
extend ctx.user
: get the current authenticated userctx.isAuthenticated()
: Test if request is authenticated* ctx.login(user[, options])
: Initiate a login session foruser
.ctx.logout()
: Terminate an existing login session
Questions & Suggestions
Please open an issue here.