UNPKG

@accounts/password

Version:

[![npm](https://img.shields.io/npm/v/@accounts/password)](https://www.npmjs.com/package/@accounts/password) [![npm downloads](https://img.shields.io/npm/dm/@accounts/password)](https://www.npmjs.com/package/@accounts/password) [![codecov](https://img.shie

118 lines 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resetPasswordForm = exports.resetPassword = exports.verifyEmail = exports.infosMiddleware = void 0; const tslib_1 = require("tslib"); const accounts_password_1 = tslib_1.__importDefault(require("../accounts-password")); const express_validator_1 = require("express-validator"); const request_ip_1 = require("request-ip"); function matchOrThrow(...args) { if (!(0, express_validator_1.validationResult)(args[0]).isEmpty()) { throw new Error('Validation error'); } return (0, express_validator_1.matchedData)(...args); } const getUserAgent = (req) => { let userAgent = req.headers['user-agent'] || ''; if (req.headers['x-ucbrowser-ua']) { // special case of UC Browser userAgent = req.headers['x-ucbrowser-ua']; } return userAgent; }; function getHtml(title, body) { return ` <!DOCTYPE html> <html lang="en"> <head> <title>${title}</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> ${body} </body> </html> `; } const infosMiddleware = (req, _res, next) => { req.infos = { userAgent: getUserAgent(req), ip: (0, request_ip_1.getClientIp)(req) ?? req.ip, }; next(); }; exports.infosMiddleware = infosMiddleware; const verifyEmail = (accountsPasswordOrInjector) => [ (0, express_validator_1.param)('token').isString().notEmpty(), async (req, res) => { try { const { token } = matchOrThrow(req); const accountsPassword = accountsPasswordOrInjector instanceof accounts_password_1.default ? accountsPasswordOrInjector : accountsPasswordOrInjector.get(accounts_password_1.default); await accountsPassword.verifyEmail(token); res.send(getHtml('Email successfully verified', ` <h3>The email address has been successfully verified.</h3> `)); } catch (err) { res.send( //codeql[js/xss-through-exception] getHtml('Email verification error', ` <h3>The email address couldn't be verified: ${err.message ?? 'unknown error'}</h3> `)); } }, ]; exports.verifyEmail = verifyEmail; const resetPassword = (accountsPasswordOrInjector) => [ (0, express_validator_1.body)('token').isString().notEmpty(), (0, express_validator_1.body)('newPassword').isString().notEmpty(), async (req, res) => { try { const { token, newPassword } = matchOrThrow(req); const accountsPassword = accountsPasswordOrInjector instanceof accounts_password_1.default ? accountsPasswordOrInjector : accountsPasswordOrInjector.get(accounts_password_1.default); await accountsPassword.resetPassword(token, newPassword, req.infos); res.send(getHtml('Password successfully changed', ` <h3>The password has been successfully changed.</h3> `)); } catch (err) { //codeql[js/xss-through-exception] res.send(getHtml('Password reset error', ` <h3>The password couldn't be changed: ${err.message ?? 'unknown error'}</h3> `)); } }, ]; exports.resetPassword = resetPassword; exports.resetPasswordForm = [ (0, express_validator_1.param)('token').isString().notEmpty().escape(), (req, res) => { try { const { token } = matchOrThrow(req); res.send(getHtml('Reset password', ` <div class="container"> <h1>Reset your password</h1> <form action="/resetPassword" method="POST"> <input type="hidden" name="token" value=${token} /> <div class="form-group"> <label for="newPassword">New password</label> <input type="text" class="form-control" id="newPassword" value="" placeholder="Enter your new password" name="newPassword"> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> `)); } catch (err) { //codeql[js/xss-through-exception] res.send(getHtml('Password reset error', ` <h3>The password couldn't be changed: ${err.message ?? 'unknown error'}</h3> `)); } }, ]; //# sourceMappingURL=express.js.map