Pārlūkot izejas kodu

Created middleware to validate token

Nik Atlas 6 gadi atpakaļ
vecāks
revīzija
1d6eaf3e72
6 mainītis faili ar 82 papildinājumiem un 12 dzēšanām
  1. 10 1
      app.js
  2. 3 0
      database/db.js
  3. 35 9
      database/repositories/users.js
  4. 26 0
      middlewares/authorization.js
  5. 5 0
      middlewares/index.js
  6. 3 2
      routes/users.js

+ 10 - 1
app.js

@@ -8,6 +8,8 @@ var bodyParser = require('body-parser');
 var index = require('./routes/index');
 var users = require('./routes/users');
 
+const Middlewares = require('./middlewares');
+
 var app = express();
 
 // view engine setup
@@ -22,8 +24,15 @@ app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));
 
-app.use('/', index);
+// Setup param parsers 
+const tokenParser = Middlewares.Authorization.getTokenParser();
+app.use(tokenParser);
+
+// SETUP ROUTES 
 app.use('/users', users);
+app.use('/', index);
+
+
 
 // catch 404 and forward to error handler
 app.use(function(req, res, next) {

+ 3 - 0
database/db.js

@@ -56,6 +56,9 @@ class Database {
 	createSearchStream(keys, options){
 		return this.index.createSearchStream(keys, options);	
 	}
+	search(keys, options) {
+		return this.createSearchStream(keys, options);
+	}
 };
 let database = null; 
 function getSingleton() { 

+ 35 - 9
database/repositories/users.js

@@ -1,8 +1,8 @@
 const db = require("../db.js");
 var CryptoJS = require('crypto-js');
 
-const userRepository = {
-	login: function(params){
+class UserRepository {
+	login(params){
 		const {
 			username,
 			password
@@ -20,8 +20,8 @@ const userRepository = {
 			error.name = "BadCredentials";
 			throw error;
 		});
-	},
-	register: function(params) {
+	}
+	register(params) {
 		const {
 			username,
 			password,
@@ -52,10 +52,36 @@ const userRepository = {
     		throw err;
     	}).
     	then(() => newuser);
-	},
-	getByID: function(id){
+	}
+	getUser(token) {
+		return new Promise((res,rej) => {
+			db.search(['token', token]).
+			on('data', (data) => {
+				res(data);
+			}).
+			on('error', function (err) {
+				let error = new Error("DB error");
+				error.name = "InternalError";
+				rej(error);
+		  	}).
+		  	on('close', function () {
+		    	let error = new Error("Token invalid");
+				error.name = "TokenInvalid";
+				rej(error);
+		  	}).
+		  	on('end', function () {
+				let error = new Error("Token invalid");
+				error.name = "TokenInvalid";
+				rej(error);
+			});
+		});
+	}
+	getByID(id){
 		return db.get("user:" + id);
-	},
+	}
 }
-
-module.exports = userRepository;
+let userRepository = null;
+function getSingleton() { 
+	return userRepository = ( userRepository === null ? new UserRepository(): userRepository);
+}
+module.exports = getSingleton();

+ 26 - 0
middlewares/authorization.js

@@ -0,0 +1,26 @@
+const UserRepository = require("../database").Repositories.Users;
+
+class Authorization {
+	constructor() {
+
+	}
+	_parseToken(req, res, next) {
+		const token = req.query.token;
+		if(!token)return next();
+		console.log("Token parsing : " + token);
+		UserRepository.getUser(token).
+		then((user) => {
+			req.user = user;
+			next();
+		}).
+		catch((err) => {
+			next(err);
+		});
+	}
+	getTokenParser() {
+		return this._parseToken;
+	}
+
+}
+
+module.exports = new Authorization();

+ 5 - 0
middlewares/index.js

@@ -0,0 +1,5 @@
+const Authorization = require('./authorization');
+
+module.exports = {
+	Authorization
+}

+ 3 - 2
routes/users.js

@@ -5,8 +5,9 @@ let JSONError = require('../helpers').JSONError;
 let Users = require('../database').Repositories.Users;
 
 /* GET users listing. */
-router.get('/', function(req, res, next) {
-  res.send('respond with a resource');
+router.get('/me', function(req, res, next) {
+	console.log(req.user);
+  res.send(req.user);
 });