|
@@ -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();
|