Skip to content

Instantly share code, notes, and snippets.

@machielg
Created June 26, 2017 11:21
Show Gist options
  • Save machielg/5591349545756aad33c1b71b1813a58e to your computer and use it in GitHub Desktop.
Save machielg/5591349545756aad33c1b71b1813a58e to your computer and use it in GitHub Desktop.
var Keycloak = require('keycloak-connect');
var hogan = require('hogan-express');
var express = require('express');
var session = require('express-session');
var app = express();
app.use(express.static('public'))
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
// Register '.mustache' extension with The Mustache Express
app.set('view engine', 'html');
app.set('views', require('path').join(__dirname, '/view'));
app.engine('html', hogan);
// A normal un-protected public URL.
app.get('/', function (req, res) {
res.render('index');
});
// Create a session-store to be used by both the express-session
// middleware and the keycloak middleware.
var memoryStore = new session.MemoryStore();
app.use(session({
secret: 'mySecret',
resave: false,
saveUninitialized: true,
store: memoryStore
}));
// Provide the session store to the Keycloak so that sessions
// can be invalidated from the Keycloak console callback.
//
// Additional configuration is read from keycloak.json file
// installed from the Keycloak web console.
var keycloak = new Keycloak({
store: memoryStore
});
// Install the Keycloak middleware.
//
// Specifies that the user-accessible application URL to
// logout should be mounted at /logout
//
// Specifies that Keycloak console callbacks should target the
// root URL. Various permutations, such as /k_logout will ultimately
// be appended to the admin URL.
app.use(keycloak.middleware({
logout: '/logout',
admin: '/'
}));
app.get('/login', keycloak.protect(), function (req, res) {
res.render('index', {
result: JSON.stringify(JSON.parse(req.session['keycloak-token']), null, 4),
event: '1. Authentication\n2. Login'
});
});
app.post('/save-gtaa-person', keycloak.protect(), function (req, res) {
res.send('Got a POST request')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment