UNPKG

3.4 kBJavaScriptView Raw
1'use strict'
2
3const AuthRequest = require('./auth-request')
4const debug = require('./../debug').accounts
5
6// class DeleteAccountRequest {
7class DeleteAccountRequest extends AuthRequest {
8 constructor (options) {
9 super(options)
10
11 this.username = options.username
12 }
13
14 /**
15 * Calls the appropriate form to display to the user.
16 * Serves as an error handler for this request workflow.
17 *
18 * @param error {Error}
19 */
20 error (error) {
21 error.statusCode = error.statusCode || 400
22
23 this.renderForm(error)
24 }
25
26 /**
27 * Returns a user account instance for the submitted username.
28 *
29 * @throws {Error} Rejects if user account does not exist for the username
30 *
31 * @returns {Promise<UserAccount>}
32 */
33 async loadUser () {
34 let username = this.username
35
36 return this.accountManager.accountExists(username)
37 .then(exists => {
38 if (!exists) {
39 throw new Error('Account not found for that username')
40 }
41
42 let userData = { username }
43
44 return this.accountManager.userAccountFrom(userData)
45 })
46 }
47
48 /**
49 * Renders the Delete form
50 */
51 renderForm (error) {
52 this.response.render('account/delete', {
53 error,
54 multiuser: this.accountManager.multiuser
55 })
56 }
57
58 /**
59 * Displays the 'your reset link has been sent' success message view
60 */
61 renderSuccess () {
62 this.response.render('account/delete-link-sent')
63 }
64
65 /**
66 * Loads the account recovery email for a given user and sends out a
67 * password request email.
68 *
69 * @param userAccount {UserAccount}
70 *
71 * @return {Promise}
72 */
73 async sendDeleteLink (userAccount) {
74 let accountManager = this.accountManager
75
76 const recoveryEmail = await accountManager.loadAccountRecoveryEmail(userAccount)
77 userAccount.email = recoveryEmail
78
79 debug('Preparing delete account email to:', recoveryEmail)
80
81 return accountManager.sendDeleteAccountEmail(userAccount)
82 }
83
84 /**
85 * Validates the request parameters, and throws an error if any
86 * validation fails.
87 *
88 * @throws {Error}
89 */
90 validate () {
91 if (this.accountManager.multiuser && !this.username) {
92 throw new Error('Username required')
93 }
94 }
95
96 static async post (req, res) {
97 const request = DeleteAccountRequest.fromParams(req, res)
98
99 debug(`User '${request.username}' requested to be sent a delete account email`)
100
101 return DeleteAccountRequest.handlePost(request)
102 }
103
104 /**
105 * Performs a 'send me a password reset email' request operation, after the
106 * user has entered an email into the reset form.
107 *
108 * @param request {DeleteAccountRequest}
109 *
110 * @return {Promise}
111 */
112 static async handlePost (request) {
113 try {
114 request.validate()
115 const userAccount = await request.loadUser()
116 await request.sendDeleteLink(userAccount)
117 return request.renderSuccess()
118 } catch (error) {
119 return request.error(error)
120 }
121 }
122
123 static get (req, res) {
124 const request = DeleteAccountRequest.fromParams(req, res)
125
126 request.renderForm()
127 }
128
129 static fromParams (req, res) {
130 let locals = req.app.locals
131 let accountManager = locals.accountManager
132 let username = this.parseParameter(req, 'username')
133
134 let options = {
135 accountManager,
136 response: res,
137 username
138 }
139
140 return new DeleteAccountRequest(options)
141 }
142}
143
144module.exports = DeleteAccountRequest