This repository was archived by the owner on Feb 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathrequireVerifiedEmail.js
More file actions
79 lines (66 loc) · 2.29 KB
/
Copy pathrequireVerifiedEmail.js
File metadata and controls
79 lines (66 loc) · 2.29 KB
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
/**
* Middleware dependencies
*
* This middleware depends on the following to be run first:
*
* - req.login() (passport)
* - determineProvider
*/
/**
* Module dependencies
*/
var Role = require('../models/Role')
var mailer = require('../boot/mailer')
var settings = require('../boot/settings')
var url = require('url')
/**
* Require verified email middleware
*/
module.exports = function (options) {
options = options || {}
options = {
force: options.force || false,
view: options.view || 'requireVerifiedEmail',
locals: options.locals || {}
}
return function requireVerifiedEmail (req, res, next) {
Role.listByUsers(req.user, function (err, roles) {
if (err) { return next(err) }
var isAuthority = roles && roles.some(function (role) {
return role.name === 'authority'
})
if (req.user.emailVerified || isAuthority) {
next()
} else if (!req.provider.emailVerification.enable) {
next()
} else if (!options.force && !req.provider.emailVerification.require) {
next()
} else {
var resendURL = url.parse(settings.issuer)
resendURL.pathname = 'email/resend'
resendURL.query = {
email: req.user.email
}
if (req.connectParams) {
resendURL.query.redirect_uri = req.connectParams.redirect_uri
resendURL.query.client_id = req.connectParams.client_id
resendURL.query.response_type = req.connectParams.response_type
resendURL.query.scope = req.connectParams.scope
}
var existingUserMsg = 'E-mail verification is required to proceed'
var newUserMsg = 'Congratulations on creating your user account! ' +
"All that's left now is to verify your e-mail."
var isNewUser = req.flash('isNewUser').indexOf(true) !== -1
var locals = {
error: options.locals.error === undefined
? (!isNewUser ? existingUserMsg : undefined) : options.locals.error,
message: options.locals.message === undefined
? (isNewUser ? newUserMsg : undefined) : options.locals.message,
from: options.locals.from || mailer.from,
resendURL: options.locals.resendURL || url.format(resendURL)
}
res.render(options.view, locals)
}
})
}
}