Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var router = express.Router();
- const uuidv4 = require('uuid/v4');
- var http = require('http');
- router.post('/login', function (req, res, next) {
- req.checkBody('username', 'Username is mandatory').notEmpty();
- req.checkBody('password', 'Password is mandatory').notEmpty();
- var response;
- var username = req.body.username;
- var password = req.body.password;
- var credentials = configuration.app.credentials;
- if (credentials.username === username && credentials.password === password) {
- function getJSON(options,cb){
- http.request(options,function(res){
- var body ='';
- res.on('data', function(chunk){
- body+=chunk;
- });
- res.on('end',function(){
- var result = JSON.parse(body.tokenId);
- cb(null, result);
- })
- res.on('error', cb);
- })
- .on('error',cb)
- .end();
- }
- var options = {
- host: 'kapua.comtrade.com',
- port: '8081',
- path: '/v1/authentication/user',
- method: 'GET'
- };
- getJSON(options, function(err,result){
- if (err){
- return console.log('Error - Getting token', err);
- }else {
- console.log('Kapua token:', result);
- }
- });
- // var token = uuidv4(); STARI TOKEN.
- var token = result; //Kapua token.
- response = {success: true, message: 'User successfully logged in', tokenId: token};
- tokens.push(token);
- // req.session.authUser = {username: username};
- } else {
- response = {success: false, message: 'Wrong credentials'};
- }
- res.json(response);
- });
- router.post('/logout', function(req, res, next) {
- req.checkBody('token', 'Token is mandatory').notEmpty();
- var token = req.body.token;
- for(var i = tokens.length - 1; i >= 0; i--) {
- if(tokens[i] === token) {
- tokens.splice(i, 1);
- // req.session.destroy();
- res.json({success: true, message: 'Logout successful'});
- }
- }
- res.json({success: false, message: 'Logout error'});
- });
- router.post('/check-login-status', function(req, res, next) {
- res.json({
- status: typeof req.session.authUser !== 'undefined' &&
- typeof req.session.authUser.username !== 'undefined'
- });
- });
- module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement