Skip to content

Commit 65d12ac

Browse files
committed
users refactor
1 parent e325528 commit 65d12ac

File tree

7 files changed

+90
-118
lines changed

7 files changed

+90
-118
lines changed

src/controllers/auth.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ import {AuthServices, UserServices} from '../services';
44
export const AuthController = {
55

66
signin: async (req: Request, res: Response, next: NextFunction) => {
7-
const result = await AuthServices.signin(req.body.username, req.body.password, req.body.passwordConfirmation)
8-
.catch((err: Error) => {
9-
return next(err);
10-
});
11-
return res.json(result);
7+
try {
8+
return res.json(await AuthServices.signin(req.body.username, req.body.password, req.body.passwordConfirmation));
9+
} catch (err) {
10+
return next(err);
11+
}
1212
},
1313

1414
login: async (req: Request, res: Response, next: NextFunction) => {
15-
const result = await AuthServices.login(req.body.username, req.body.password)
16-
.catch((err: Error) => {
17-
return next(err);
18-
});
19-
return res.json(result);
15+
try {
16+
return res.json(await AuthServices.login(req.body.username, req.body.password));
17+
} catch (err) {
18+
return next(err);
19+
}
2020
},
2121

2222
checkToken: async (req: Request, res: Response, next: NextFunction) => {
23-
const result = await UserServices.isTokenValid(req.body.username, req.body.token)
24-
.catch((err: Error) => {
25-
return next(err);
26-
});
27-
return res.json(result);
23+
try {
24+
return res.json(await UserServices.isTokenValid(req.body.username, req.body.token));
25+
} catch (err) {
26+
return next(err);
27+
}
2828
}
2929
};

src/controllers/profile.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
11
import {Request, Response, NextFunction} from "express";
2-
import {UserServices} from '../services';
2+
import {AuthServices, UserServices} from '../services';
33

44
export const ProfileController = {
55

66
getProfile: async (req: Request, res: Response, next: NextFunction) => {
7-
const user = await UserServices.getUser(req.body.username)
8-
.catch((err: Error) => {
9-
return next(err);
7+
try {
8+
return res.json({
9+
user: await UserServices.getUser(req.body.username),
10+
token: await UserServices.getToken(req.body.username)
1011
});
11-
const token = await UserServices.getToken(req.body.username)
12-
.catch((err: Error) => {
13-
return next(err);
14-
});
15-
return res.json({
16-
user: user,
17-
token: token
18-
});
12+
} catch (err) {
13+
return next(err);
14+
}
1915
},
2016

2117
updateProfile: async (req: Request, res: Response, next: NextFunction) => {
22-
const result = await UserServices.updateUser(req.body.token.username, req.body.data)
23-
.catch((err: Error) => {
24-
return next(err);
25-
});
26-
return res.json(result);
18+
try {
19+
return res.json(await UserServices.updateUser(req.body.token.username, req.body.data));
20+
} catch (err) {
21+
return next(err);
22+
}
2723
},
2824

2925
deleteProfile: async (req: Request, res: Response, next: NextFunction) => {
30-
await UserServices.deleteUser(req.body.token.username)
31-
.catch((err: Error) => {
32-
return next(err);
33-
});
34-
return res.redirect('/');
26+
try {
27+
await UserServices.deleteUser(req.body.token.username)
28+
return res.redirect('/');
29+
} catch (err) {
30+
return next(err);
31+
}
3532
}
3633
};

src/controllers/user.ts

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,45 @@
11
import {Request, Response, NextFunction} from "express";
2-
import {UserServices} from '../services';
2+
import {AuthServices, UserServices} from '../services';
33

44
export const UserController = {
55

6-
getUsers: (req: Request, res: Response, next: NextFunction) => {
7-
return UserServices.getUsers()
8-
.then(result => {
9-
return res.json(result)
10-
})
11-
.catch((err: Error) => {
12-
return next(err);
13-
});
6+
getUsers: async (req: Request, res: Response, next: NextFunction) => {
7+
try {
8+
return res.json(await UserServices.getUsers());
9+
} catch (err) {
10+
return next(err);
11+
}
1412
},
1513

16-
createUser: (req: Request, res: Response, next: NextFunction) => {
17-
return UserServices.addUser(req.body.data)
18-
.then(result => {
19-
return res.json(result)
20-
})
21-
.catch((err: Error) => {
22-
return next(err);
23-
});
14+
createUser: async (req: Request, res: Response, next: NextFunction) => {
15+
try {
16+
return res.json(await UserServices.addUser(req.body.data));
17+
} catch (err) {
18+
return next(err);
19+
}
2420
},
2521

26-
updateUser: (req: Request, res: Response, next: NextFunction) => {
27-
return UserServices.updateUser(req.body.user.username, req.body.data)
28-
.then(result => {
29-
return res.json(result)
30-
})
31-
.catch((err: Error) => {
32-
return next(err);
33-
});
22+
updateUser: async (req: Request, res: Response, next: NextFunction) => {
23+
try {
24+
return res.json(await UserServices.updateUser(req.body.user.username, req.body.data));
25+
} catch (err) {
26+
return next(err);
27+
}
3428
},
3529

36-
getUser: (req: Request, res: Response, next: NextFunction) => {
37-
return UserServices.getUser(req.body.user.username)
38-
.then(result => {
39-
return res.json(result)
40-
})
41-
.catch((err: Error) => {
42-
return next(err);
43-
});
30+
getUser: async (req: Request, res: Response, next: NextFunction) => {
31+
try {
32+
return res.json(await UserServices.getUser(req.body.user.username));
33+
} catch (err) {
34+
return next(err);
35+
}
4436
},
4537

46-
deleteUser: (req: Request, res: Response, next: NextFunction) => {
47-
return UserServices.deleteUser(req.body.user.username)
48-
.then(result => {
49-
return res.json(result)
50-
})
51-
.catch((err: Error) => {
52-
return next(err);
53-
});
38+
deleteUser: async (req: Request, res: Response, next: NextFunction) => {
39+
try {
40+
return res.json(await UserServices.deleteUser(req.body.user.username));
41+
} catch (err) {
42+
return next(err);
43+
}
5444
},
5545
};

src/models/Password.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ export class Password {
2424
return EncryptionServices.hash(toHash, this.iterations);
2525
}
2626

27-
public async comparePassword(password: string): Promise<boolean> {
27+
public comparePassword(password: string): boolean {
2828
return this.generateHash(password) === this.hash
2929
}
3030

31+
static difficultyPassword(password: string): boolean {
32+
return password.length >= 6;
33+
}
34+
3135
static confirmPassword(password: string, passwordConfirmation: string): boolean {
3236
return password === passwordConfirmation;
3337
}

src/services/auth.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,42 @@ export const AuthServices = {
99
signin: async (username: string, password: string, passwordConfirmation: string): Promise<Token> => {
1010
if (_.isNil(username) || _.isNil(password) || _.isNil(passwordConfirmation)) throw new Error('Missing data');
1111
if (!Password.confirmPassword(password, passwordConfirmation)) throw new Error('Password not matching');
12+
if (!Password.difficultyPassword(password)) throw new Error('Password must contains at least 6 characters');
1213

13-
const data = {
14+
const user = await UserServices.addUser({
1415
username: username,
1516
password: password
16-
};
17-
const user = await UserServices.addUser(data)
17+
})
1818
.catch((err: Error) => {
1919
throw err;
2020
});
21-
const token = new Token(data);
22-
token.userId = user.id;
21+
22+
const token = new Token({
23+
userId: user.id,
24+
username: username,
25+
password: password
26+
});
2327
return AuthServices.setToken(user, token);
2428
},
2529

2630
login: async (username: string, password: string): Promise<Token> => {
2731
if (_.isNil(username) || _.isNil(password)) throw new Error('Missing data');
2832

2933
const user: User = await UserServices.getUser(username);
30-
const psswd: Password = await UserServices.getPassword(username);
31-
const valid = psswd.comparePassword(password);
34+
const userPassword: Password = await UserServices.getPassword(username);
35+
const valid = userPassword.comparePassword(password);
36+
console.log(valid)
37+
if (valid === false) throw new Error(Translation[Config.language].INVALID_CREDENTIALS);
3238

33-
if (!valid) throw Translation[Config.language].INVALID_CREDENTIALS;
3439
const token = new Token({
40+
userId: user.id,
3541
username: username,
3642
password: password
3743
});
3844
return AuthServices.setToken(user, token);
3945
},
4046

4147
setToken: async (user: User, token: Token): Promise<Token> => {
42-
return DbClient.findOneAndUpdateOrInsert(Config.database.collections.tokens, {username: user.username}, token);
48+
return DbClient.findOneAndUpdateOrInsert(Config.database.collections.tokens, {userId: user.id}, token);
4349
}
4450
};

src/services/user.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {Translation} from "../translations";
77
export const UserServices = {
88

99
isTokenValid: async (username: string, token: string): Promise<boolean> => {
10-
const userToken: Token = await DbClient.findOne(Config.database.collections.tokens, {username: username});
10+
const userToken: Token = await UserServices.getToken(username);
1111
const now = moment.utc().format();
1212
if (userToken &&
1313
userToken.hash === token &&
@@ -49,18 +49,12 @@ export const UserServices = {
4949

5050
getToken: async (username: string): Promise<Token> => {
5151
const user: User = await UserServices.getUser(username);
52-
return DbClient.findOne(Config.database.collections.tokens, {userId: user.id})
53-
.then((data: any) => {
54-
return new Token(data);
55-
});
52+
return new Token(await DbClient.findOne(Config.database.collections.tokens, {userId: user.id}));
5653
},
5754

5855
getPassword: async (username: string): Promise<Password> => {
5956
const user: User = await UserServices.getUser(username);
60-
return DbClient.findOne(Config.database.collections.passwords, {userId: user.id})
61-
.then((data: any) => {
62-
return new Password(data);
63-
});
57+
return new Password(await DbClient.findOne(Config.database.collections.passwords, {userId: user.id}));
6458
},
6559

6660
deletePassword: async (username: string): Promise<void> => {

src/test/models/Password.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,13 @@ describe('Password', () => {
6868
const password = new Password({
6969
password: 'password'
7070
});
71-
let trueResult: any,
72-
falseResult: any;
73-
before((done) => {
74-
password.comparePassword('password')
75-
.then((res: any) => {
76-
trueResult = res;
77-
password.comparePassword('')
78-
.then((res: any) => {
79-
falseResult = res;
80-
done();
81-
})
82-
.catch((e: Error) => {
83-
console.log(e);
84-
});
85-
})
86-
.catch((e: Error) => {
87-
console.log(e);
88-
});
89-
});
9071
it('compare with right password should return a boolean equal to true', () => {
91-
expect(trueResult)
72+
expect(password.comparePassword('password'))
9273
.to.be.a('boolean')
9374
.to.be.equal(true);
9475
});
9576
it('compare with wrong password should return a boolean equal to false', () => {
96-
expect(falseResult)
77+
expect(password.comparePassword(''))
9778
.to.be.a('boolean')
9879
.to.be.equal(false);
9980
});

0 commit comments

Comments
 (0)