代码拉取完成,页面将自动刷新
'use strict';
require('dotenv').config()
const createApplication = require('./');
const simpleOauthModule = require('simple-oauth2');
createApplication(({
app,
callbackUrl
}) => {
const oauth2 = simpleOauthModule.create({
client: {
id: process.env.CLIENT_ID,
secret: process.env.CLIENT_SECRET,
},
auth: {
tokenHost: process.env.OAUTH_AUTHORITY,
tokenPath: process.env.OAUTH_TOKEN_ENDPOINT,
authorizePath: process.env.OAUTH_AUTHORIZE_ENDPOINT,
},
options: {
authorizationMethod: 'body',
}
});
// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: callbackUrl,
scope: process.env.OAUTH_SCOPES,
});
// Initial page redirecting to Github
app.get('/auth', (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});
// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
const code = req.query.code;
const options = {
code,
redirect_uri: callbackUrl,
};
try {
const result = await oauth2.authorizationCode.getToken(options);
console.log('The resulting token: ', result);
const token = oauth2.accessToken.create(result);
return res.status(200).json(token)
} catch (error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});
app.get('/', (req, res) => {
res.send('Hello<br><a href="/auth">Log in with Microsoft</a>');
});
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。