-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.js
33 lines (27 loc) · 1.01 KB
/
utils.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
import { ForbiddenError, AuthenticationError } from 'apollo-server-express';
// Users utilities
const utilities = {};
utilities.checkLoggedInAndVerified = (user) => {
// User should be logged in at this stage
if (!user) {
throw new AuthenticationError('User is not logged in!');
}
// Make sure email is verified (in case of password service)
// TODO: use current loggedIn service instead
const isPasswordService = Object.keys(user.services).indexOf('password') !== -1;
const isEmailVerified = isPasswordService && user.emails[0].verified === true;
if (isPasswordService && !isEmailVerified) {
throw new ForbiddenError('Email is not verified!');
}
};
// TODO: pass email to verify as an argument
utilities.checkLoggedInAndNotVerified = (user) => {
// User should be logged in at this stage
if (!user) {
throw new AuthenticationError('User is not logged in!');
}
if (user.emails[0].verified === true) {
throw new ForbiddenError('Email already verified!');
}
};
export default utilities;