Alongside the token the value of admin state is also sent.

This commit is contained in:
2018-03-07 01:35:40 +01:00
parent 858f3f5d57
commit ba96e27c43
3 changed files with 15 additions and 7 deletions

View File

@@ -9,7 +9,7 @@ class UserRepository {
create: 'insert into user (user_name) values (?)',
change: 'update user set password = ? where user_name = ?',
retrieveHash: 'select * from user where user_name = ?',
getAdminByUser: 'select admin from user where user_name = ?'
getAdminStateByUser: 'select admin from user where user_name = ?'
};
}
@@ -51,8 +51,8 @@ class UserRepository {
return this.database.run(this.queries.change, [password, user.username]);
}
isAdmin(user) {
return this.database.get(this.queries.getAdminByUser, user.username).then((row) => {
checkAdmin(user) {
return this.database.get(this.queries.getAdminStateByUser, user.username).then((row) => {
return row.admin;
})
}

View File

@@ -1,10 +1,12 @@
const User = require('src/user/user');
const Token = require('src/user/token');
const UserSecurity = require('src/user/userSecurity');
const UserRepository = require('src/user/userRepository');
const configuration = require('src/config/configuration').getInstance();
const secret = configuration.get('authentication', 'secret');
const userSecurity = new UserSecurity();
const userRepository = new UserRepository();
/**
* Controller: Log in a user provided correct credentials.
@@ -17,9 +19,11 @@ function loginController(req, res) {
const password = req.body.password;
userSecurity.login(user, password)
.then(() => {
.then(() => userRepository.checkAdmin(user))
.then((checkAdmin) => {
const token = new Token(user).toString(secret);
res.send({ success: true, token });
const admin_state = checkAdmin == 1 ? true : false;
res.send({ success: true, token, admin: admin_state });
})
.catch((error) => {
res.status(401).send({ success: false, error: error.message });

View File

@@ -1,10 +1,12 @@
const User = require('src/user/user');
const Token = require('src/user/token');
const UserSecurity = require('src/user/userSecurity');
const UserRepository = require('src/user/userRepository');
const configuration = require('src/config/configuration').getInstance();
const secret = configuration.get('authentication', 'secret');
const userSecurity = new UserSecurity();
const userRepository = new UserRepository();
/**
* Controller: Register a new user
@@ -17,9 +19,11 @@ function registerController(req, res) {
const password = req.body.password;
userSecurity.createNewUser(user, password)
.then(() => {
.then(() => userRepository.checkAdmin(user))
.then((checkAdmin) => {
const token = new Token(user).toString(secret);
res.send({ success: true, message: 'Welcome to Seasoned!', token});
const admin_state = checkAdmin == 1 ? true : false;
res.send({ success: true, message: 'Welcome to Seasoned!', token, admin: admin_state });
})
.catch((error) => {
res.status(401).send({ success: false, error: error.message });