forked from paralect/koa-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.controller.js
122 lines (98 loc) · 3.21 KB
/
account.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const userService = require('resources/user/user.service');
const authService = require('auth.service');
const emailService = require('email.service');
const securityUtil = require('security.util');
const config = require('config');
const createUserAccount = async (userData) => {
const salt = await securityUtil.generateSalt();
const [hash, signupToken] = await Promise.all([
securityUtil.getHash(userData.password, salt),
securityUtil.generateSecureToken(),
]);
const user = await userService.create({
firstName: userData.firstName,
lastName: userData.lastName,
passwordHash: hash.toString(),
passwordSalt: salt.toString(),
email: userData.email,
isEmailVerified: false,
signupToken,
});
await emailService.sendSignupWelcome({ email: user.email, signupToken });
return user;
};
/**
* Create user, company, default app, send signup confirmation email and
* create auth token for user to login
*/
exports.signup = async (ctx) => {
const userData = ctx.validatedRequest.value;
const user = await createUserAccount(userData);
const response = {};
if (config.isDev) {
response._signupToken = user.signupToken;
}
ctx.body = response;
};
/**
* Verify user's email when user click a link from email
* sets `emailVerified` to true if token is valid
*/
exports.verifyEmail = async (ctx, next) => {
const data = ctx.validatedRequest.value;
const user = await userService.markEmailAsVerified(data.userId);
const token = authService.createAuthToken({
userId: user._id,
});
const loginUrl = `${config.webUrl}?token=${token}`;
ctx.redirect(`${loginUrl}&emailVerification=true`);
};
/**
* Sign in user
* Loads user by email and compare password hashes
*/
exports.signin = async (ctx, next) => {
const signinData = ctx.validatedRequest.value;
const token = authService.createAuthToken({ userId: signinData.userId });
ctx.body = {
token,
};
};
/**
* Send forgot password email with a unique link to set new password
* If user is found by email - sends forgot password email and update
* `forgotPasswordToken` field. If user not found, returns validator's error
*/
exports.forgotPassword = async (ctx, next) => {
const data = ctx.validatedRequest.value;
const user = await userService.findOne({ email: data.email });
let { resetPasswordToken } = user;
const { firstName } = user;
if (!resetPasswordToken) {
resetPasswordToken = await securityUtil.generateSecureToken();
await userService.updateResetPasswordToken(user._id, resetPasswordToken);
}
await emailService.sendForgotPassword({
email: user.email,
resetPasswordToken,
firstName,
});
ctx.body = {};
};
/**
* Updates user password, used in combination with forgotPassword
*/
exports.resetPassword = async (ctx, next) => {
const { userId, password } = ctx.validatedRequest.value;
await userService.updatePassword(userId, password);
await userService.updateResetPasswordToken(userId, '');
ctx.body = {};
};
exports.resendVerification = async (ctx, next) => {
const { email } = ctx.request.body;
const user = await userService.findOne({ email });
if (user) {
await emailService.sendSignupWelcome({ email, signupToken: user.signupToken });
}
ctx.body = {};
};