realm-object-server
Version:
182 lines • 8.1 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const bodyParser = require("body-parser");
const express = require("express");
const path = require("path");
const decorators_1 = require("../../decorators");
const Server_1 = require("../../Server");
const errors = require("../../errors");
const VIEWS_PATH = `${__dirname}/views`;
let PasswordAuthUIService = class PasswordAuthUIService {
start(server) {
return __awaiter(this, void 0, void 0, function* () {
this.authClient = server.authClient;
this.logger = server.logger.withContext({ service: "password-auth-ui" });
});
}
getConfirmEmail(res, token, redirectUrl) {
return __awaiter(this, void 0, void 0, function* () {
if (token) {
try {
yield this.authClient.completeEmailConfirmation(token);
if (redirectUrl) {
res.redirect(302, `${redirectUrl}?success=true`);
}
else {
res.render(`${VIEWS_PATH}/confirm-email-success`);
}
}
catch (err) {
const message = this.parseErrorMessage("Failed to complete email confirmation", err);
this.logger.detail(message);
if (redirectUrl) {
res.redirect(302, `${redirectUrl}?success=false&message=${message}`);
}
else {
res.status(400).render(`${VIEWS_PATH}/confirm-email-failure`, { message });
}
}
}
else {
throw new errors.realm.MissingParameters("token");
}
});
}
getResetPassword(res, token, redirectUrl) {
return __awaiter(this, void 0, void 0, function* () {
res.render(`${VIEWS_PATH}/reset-password`, {
token,
redirectUrl,
});
});
}
postResetPassword(res, redirectUrl, email, token, password, passwordRepeated) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (token) {
if (password === passwordRepeated) {
yield this.authClient.completePasswordReset(token, password);
if (redirectUrl) {
res.redirect(302, `${redirectUrl}?success=true`);
}
else {
res.render(`${VIEWS_PATH}/reset-password-success`);
}
}
else {
throw new Error("The two passwords must match");
}
}
else if (email) {
try {
yield this.authClient.requestPasswordReset(email);
}
catch (err) {
this.logger.detail(`Error requesting password reset: ${JSON.stringify(err)}`);
}
finally {
if (redirectUrl) {
res.redirect(302, redirectUrl);
}
else {
res.render(`${VIEWS_PATH}/reset-password-requested`);
}
}
}
else {
throw new errors.realm.MissingParameters("token", "email");
}
}
catch (err) {
this.logger.detail(`Error resetting password: ${JSON.stringify(err)}`);
const message = this.parseErrorMessage("Failed to complete password reset", err);
if (redirectUrl) {
res.redirect(302, `${redirectUrl}?success=false&message=${message}`);
}
else {
res.status(400).render(`${VIEWS_PATH}/reset-password`, {
error: message, token,
});
}
}
});
}
parseErrorMessage(intent, err) {
if (err.detail) {
return `${intent}: ${err.detail}`;
}
else if (err.title) {
return `${intent}: ${err.title}`;
}
else if (err.message) {
return `${intent}: ${err.message}`;
}
else {
return intent;
}
}
};
__decorate([
decorators_1.Start(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Server_1.Server]),
__metadata("design:returntype", Promise)
], PasswordAuthUIService.prototype, "start", null);
__decorate([
decorators_1.Get("/confirm-email"),
__param(0, decorators_1.Response()),
__param(1, decorators_1.Query("token")),
__param(2, decorators_1.Query("redirectUrl")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, String, String]),
__metadata("design:returntype", Promise)
], PasswordAuthUIService.prototype, "getConfirmEmail", null);
__decorate([
decorators_1.Get("/reset-password"),
__param(0, decorators_1.Response()),
__param(1, decorators_1.Query("token")),
__param(2, decorators_1.Query("redirectUrl")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, String, String]),
__metadata("design:returntype", Promise)
], PasswordAuthUIService.prototype, "getResetPassword", null);
__decorate([
decorators_1.MiddlewaresBefore(bodyParser.urlencoded({ extended: true })),
decorators_1.Post("/reset-password"),
__param(0, decorators_1.Response()),
__param(1, decorators_1.Body("redirect-url")),
__param(2, decorators_1.Body("email")),
__param(3, decorators_1.Body("token")),
__param(4, decorators_1.Body("password")),
__param(5, decorators_1.Body("password-repeated")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, String, String, String, String, String]),
__metadata("design:returntype", Promise)
], PasswordAuthUIService.prototype, "postResetPassword", null);
PasswordAuthUIService = __decorate([
decorators_1.ServeStatic("/", path.resolve(__dirname, "static")),
decorators_1.BaseRoute("/", { allowAnonymous: true }),
decorators_1.ServiceName("password-auth-ui")
], PasswordAuthUIService);
exports.PasswordAuthUIService = PasswordAuthUIService;
//# sourceMappingURL=PasswordAuthUIService.js.map