projectstreet-appauth

git clone git://archive.git.mtrnord.blog/MTRNord/projectstreet-appauth.git
Log | Files | Refs

server.js (1210B)


      1 var express = require('express');
      2 var jwt = require('jsonwebtoken');
      3 
      4 var app = express();
      5 
      6 app.get('/auth', function(req, res) {
      7   // request received from Ionic Auth
      8   var mySharedSecret = 'foxtrot';
      9   var redirectUri = req.query.redirect_uri;
     10   var state = req.query.state;
     11 
     12   try {
     13     var incomingToken = jwt.verify(req.query.token, mySharedSecret);
     14   } catch (ex) { // lots of stuff can go wrong while decoding the jwt
     15     console.error(ex.stack);
     16     return res.status(401).send('jwt error');
     17   }
     18 
     19   // TODO: Authenticate your own real users here
     20   var username = incomingToken.data.username;
     21   var password = incomingToken.data.password;
     22   var user_id;
     23   if (username == 'dan' && password == '123') {
     24     user_id = 'user-from-express';
     25   }
     26 
     27   // authentication failure
     28   if (!user_id) {
     29     return res.status(401).send('auth error');
     30   }
     31 
     32   // make the outgoing token, which is sent back to Ionic Auth
     33   var outgoingToken = jwt.sign({"user_id": user_id}, mySharedSecret);
     34   var url = redirectUri +
     35     '&token=' + encodeURIComponent(outgoingToken) +
     36     '&state=' + encodeURIComponent(state);
     37 
     38   return res.redirect(url);
     39 });
     40 
     41 app.listen(8080, function() {
     42   console.log('listening on port 8080');
     43 });