Simple OAuth2 StackExchange
This library is a wrapper around Simple OAuth2 Library
Specially made for Authorization Code Flow with StackExchange.
Requirements
Latest Node 8 LTS or newer versions.
Getting started
npm install --save simple-oauth2 simple-oauth2-stack-exchange
or
yarn add simple-oauth2 simple-oauth2-stack-exchange
Usage
const simpleOAuth2StackExchange = require('simple-oauth2-stack-exchange');
const stackExchange = simpleOAuth2StackExchange.create(options);
stackExchange
object exposes 3 keys:
- authorize: Middleware to request user's authorization.
- getToken: Middleware for callback processing and exchange the authorization token for an
access_token
- oauth2: The underlying simple-oauth2 instance.
Options
Required options
Option | Description |
---|
clientId | Your App Id. |
clientSecret | Your App Secret Id. |
callbackURL | Callback configured when you created the app. |
Other options
Example
Original boilerplate
const oauth2 = require('simple-oauth2').create({
client: {
id: process.env.STACK_EXCHANGE_APP_ID,
secret: process.env.STACK_EXCHANGE_APP_SECRET
},
auth: {
authorizeHost: 'https://stackoverflow.com'
authorizePath: '/oauth',
tokenHost: 'https://stackoverflow.com',
tokenPath: '/oauth/access_token'
}
});
router.get('/auth/stack-exchange', (req, res) => {
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: 'http://localhost:3000/auth/stack-exchange/callback',
});
res.redirect(authorizationUri);
});
router.get('/auth/stack-exchange/callback', async(req, res) => {
const code = req.query.code;
const options = {
code,
redirect_uri: 'http://localhost:3000/auth/stack-exchange/callback'
};
try {
const result = await oauth2.authorizationCode.getToken(options);
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');
}
});
With SimpleOAuth2StackExchange
const simpleOAuth2StackExchange = require('simple-oauth2-stack-exchange');
const stackExchange = simpleOAuth2StackExchange.create({
clientId: process.env.STACK_EXCHANGE_APP_ID,
clientSecret: process.env.STACK_EXCHANGE_APP_SECRET,
callbackURL: 'http://localhost:3000/auth/stack-exchange/callback'
});
router.get('/auth/stack-exchange', stackExchange.authorize);
router.get('/auth/stack-exchange/callback', stackExchange.accessToken, (req, res) => {
return res.status(200).json(req.token);
});