commit 95867923956fe11cdeb2f3e1b3582790089a97ca
parent 1ba87c63aed3b3f314edcfe81e33e1e3d117de75
Author: Marcel <MTRNord@users.noreply.github.com>
Date: Mon, 25 Jul 2016 14:46:05 +0200
Create server.js
Diffstat:
| A | server.js | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 43 insertions(+), 0 deletions(-)
diff --git a/server.js b/server.js
@@ -0,0 +1,43 @@
+var express = require('express');
+var jwt = require('jsonwebtoken');
+
+var app = express();
+
+app.get('/auth', function(req, res) {
+ // request received from Ionic Auth
+ var mySharedSecret = 'foxtrot';
+ var redirectUri = req.query.redirect_uri;
+ var state = req.query.state;
+
+ try {
+ var incomingToken = jwt.verify(req.query.token, mySharedSecret);
+ } catch (ex) { // lots of stuff can go wrong while decoding the jwt
+ console.error(ex.stack);
+ return res.status(401).send('jwt error');
+ }
+
+ // TODO: Authenticate your own real users here
+ var username = incomingToken.data.username;
+ var password = incomingToken.data.password;
+ var user_id;
+ if (username == 'dan' && password == '123') {
+ user_id = 'user-from-express';
+ }
+
+ // authentication failure
+ if (!user_id) {
+ return res.status(401).send('auth error');
+ }
+
+ // make the outgoing token, which is sent back to Ionic Auth
+ var outgoingToken = jwt.sign({"user_id": user_id}, mySharedSecret);
+ var url = redirectUri +
+ '&token=' + encodeURIComponent(outgoingToken) +
+ '&state=' + encodeURIComponent(state);
+
+ return res.redirect(url);
+});
+
+app.listen(8080, function() {
+ console.log('listening on port 8080');
+});