Zeogram Auth Library is Express-compatible authentication middleware for Node.js using Zeogram account.
To use Zeogram Auth library create an auth app account in Zeogram. Zeogram will provide a client id and secret code.
Install zeogram-auth-library with npm
npm install zeogram-auth-library
const express = require('express');
const app = express();
const zeoauth = require('zeogram-auth-library');
// Must be install
const cookieSession = require('cookie-session');
const cookieParser = require('cookie-parser');
//Add this middleware
app.use(cookieParser())
app.use(cookieSession({
maxAge: 24*60*60*1000,
keys: ['a-secret-key']
}));
// Initialize Auth App
app.use(
zeoauth.initialize(
'client-id',
'client-secret',
["name@profile", "avatar@profile", "email@profile", "birth@profile", "gender@profile"], //scopes
'/auth/zeogram/redirect', //redirect URL
)
);
// Add zeoauth.authenticate() middleware in which route will send auth request
app.get('/zeo/auth', zeoauth.authenticate());
// After Zeogram Auth this route will fire.
app.get('/auth/zeogram/redirect', zeoauth.callback('zeogram'), (req, res) => {
res.redirect('/')
});
// basic authorization middleware for website
function auth(req, res, next) {
if(req.session.user == undefined || req.session.user == null) {
res.send('Welcome to My website.. <a href="/zeo/auth">continue with Zeogram</a>')
} else {
res.user = req.session.user;
next()
}
}
// Route you need to authenticated.
app.get('/', zeoauth.authorizer(), auth, async(req, res) => {
res.send('Hello ' + res.user.givenName + '. Welcome to my Website <br/> <a href="/signout">Sign out</a>');
});
// Sign out route
app.get('/signout', zeoauth.logout, async(req, res) => {
res.redirect('/')
});
/**** another routes *****/
app.listen(3000);