-
Notifications
You must be signed in to change notification settings - Fork 4
/
users.ts
177 lines (145 loc) · 4.34 KB
/
users.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import * as Hapi from "@hapi/hapi";
import Boom from "@hapi/boom";
import uuid from "uuid/v4";
import * as bcrypt from "bcrypt";
import { UserModel as User, IUser } from "../models/User";
import { createToken } from "../util/token";
import { IRequest, IRegisterRequest, ILoginRequest } from "./types";
export interface ICreateResponse {
userID: string;
id_token: string;
username: string;
}
export const createUser = async (
request: IRegisterRequest,
h: Hapi.ResponseToolkit
) => {
const { email, username, password } = request.payload;
const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(password, salt);
const id = uuid();
try {
let user: IUser = await User.create(
Object.assign({}, { email, username, id, password: hash, admin: false })
);
let token = createToken(user);
return h
.response({
userID: user.id,
id_token: token,
username: user.username
})
.code(201);
} catch (error) {
return Boom.badImplementation(error);
}
};
export interface IAuthUser {
token: string;
}
export const authenticateUser = (
request: Hapi.Request,
h: Hapi.ResponseToolkit
): Hapi.ResponseObject => {
return h.response({ token: createToken(request.pre.user) }).code(201);
};
export const getDetails = async (
request: IRequest,
h: Hapi.ResponseToolkit
): Promise<IUser> => {
const { user } = request.auth.credentials;
const foundUser = await User.findById(user, ["username", "email"]).lean();
return foundUser;
};
interface IUpdatePassword extends IRequest {
payload: {
oldPassword: string;
newPassword: string;
};
}
export const updatePassword = async (request: IUpdatePassword) => {
const { user } = request.auth.credentials;
const { oldPassword, newPassword } = request.payload;
const salt = await bcrypt.genSalt(10);
const newPasswordHash = await bcrypt.hash(newPassword, salt);
try {
const updated = await User.findByIdAndUpdate(
{
_id: user
},
{ $set: { password: newPasswordHash } },
{ new: true, select: "username email" }
);
return updated;
} catch (err) {
return Boom.internal("Internal MongoDB error", err);
}
};
export const verifyValidPassword = async (request: IUpdatePassword) => {
const { user } = request.auth.credentials;
const { oldPassword, newPassword } = request.payload;
const salt = await bcrypt.genSalt(10);
const newPasswordHash = await bcrypt.hash(newPassword, salt);
// Validates the the old password was actually the user's password
const isPasswordValid = async (p: string): Promise<boolean> => {
const { password } = await User.findById(user, "password").lean();
const result = await bcrypt.compare(p, password);
return result;
};
const valid = await isPasswordValid(oldPassword);
if (!valid) {
return Boom.badRequest("That wasn't your password");
}
return request.payload;
};
export const verifyUniqueUser = async (request: IRegisterRequest) => {
const { username, email } = request.payload;
const user: IUser = await User.findOne({
$or: [{ email }, { username }]
});
if (user) {
if (user.username === username) {
return Boom.badRequest("Username taken");
}
if (user.email === email) {
return Boom.badRequest("Email taken");
}
}
return request.payload;
};
interface IUpdateEmailNameRequest extends IRequest {
payload: {
username: string;
email: string;
};
}
export const updateNameEmail = async (
request: IUpdateEmailNameRequest
): Promise<IUser | Boom<any>> => {
const { user } = request.auth.credentials;
const { username, email } = request.payload;
try {
const updated = await User.findByIdAndUpdate(
{
_id: user
},
{ $set: { username, email } },
{ new: true, select: "username email" }
);
return updated;
} catch (err) {
return Boom.internal("Internal MongoDB error", err);
}
};
export const verifyCredentials = async (request: ILoginRequest) => {
const { password, user: identifier } = request.payload;
const user = await User.findOne({
$or: [{ email: identifier }, { username: identifier }]
});
if (user) {
const isValid = await bcrypt.compare(password, user.password);
return isValid ? user : Boom.badRequest("Incorrect password!");
} else {
return Boom.badRequest("Incorrect username or email!");
}
};