|
@@ -1,8 +1,8 @@
|
|
const db = require("../db.js");
|
|
const db = require("../db.js");
|
|
var CryptoJS = require('crypto-js');
|
|
var CryptoJS = require('crypto-js');
|
|
|
|
|
|
-const userRepository = {
|
|
|
|
- login: function(params){
|
|
|
|
|
|
+class UserRepository {
|
|
|
|
+ login(params){
|
|
const {
|
|
const {
|
|
username,
|
|
username,
|
|
password
|
|
password
|
|
@@ -20,8 +20,8 @@ const userRepository = {
|
|
error.name = "BadCredentials";
|
|
error.name = "BadCredentials";
|
|
throw error;
|
|
throw error;
|
|
});
|
|
});
|
|
- },
|
|
|
|
- register: function(params) {
|
|
|
|
|
|
+ }
|
|
|
|
+ register(params) {
|
|
const {
|
|
const {
|
|
username,
|
|
username,
|
|
password,
|
|
password,
|
|
@@ -52,10 +52,36 @@ const userRepository = {
|
|
throw err;
|
|
throw err;
|
|
}).
|
|
}).
|
|
then(() => newuser);
|
|
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);
|
|
return db.get("user:" + id);
|
|
- },
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
-module.exports = userRepository;
|
|
|
|
|
|
+let userRepository = null;
|
|
|
|
+function getSingleton() {
|
|
|
|
+ return userRepository = ( userRepository === null ? new UserRepository(): userRepository);
|
|
|
|
+}
|
|
|
|
+module.exports = getSingleton();
|