1.1.0 • Published 5 years ago
opskins-oauth v1.1.0
opskins-oauth
OPSkins-OAuth is a simple module, made to simplify OPSkins OAuth logins.
Installation
npm i opskins-oauth
Usage
Setup
const CustomStrategy = require('passport-custom');
const opAuth = require('opskins-oauth');
let OpskinsAuth = new opAuth.init({
name: 'testing', // Site name displayed to users on logon
returnURL: 'http://localhost:3037/auth/opskins/authenticate', // Your return route
apiKey: '2087fcb59f2be98c8a5bbfe245669d', // OPSkins API key
scopes: 'identity deposit withdraw', // Scopes you want to access
mobile: true // Removes login navbar if true
});
passport.use('custom', new CustomStrategy(function (req, done) {
OpskinsAuth.authenticate(req, (err, user) => {
if (err) {
done(err);
} else {
done(null, user);
}
});
}));
Routes
app.get('/auth/opskins', function (req, res) {
res.redirect(OpskinsAuth.getFetchUrl());
});
app.get('/auth/opskins/authenticate', passport.authenticate('custom', {
failureRedirect: '/'
}), function (req, res) {
res.redirect('/');
});
Access the user object:
app.get('/', function (req, res) {
console.log(req.user);
res.end('test');
});
Scopes
You can find a full range of available scopes here: https://docs.opskins.com/public/en.html#scopes. For a simple logon page, only use identity.
Other
Ignore this if you're only using this module for the purposes of logging a user on
The user object returned will contain an access_token
property, which can be used when calling API endpoints on behalf of the user:
let headers = {
'Authorization': `Bearer ${req.user.access_token}`
};
This access token is valid for 30 minutes. Endpoints will return an error like this after 30 minutes:
{
"error": "invalid_token",
"error_description": "Bearer token has expired"
}
Simply redirect the user to /auth/opskins
to get a new access token.