consumerportal
Version:
mydna Custimised for you
148 lines (129 loc) • 5.32 kB
text/typescript
/// <reference path="../../includes.ts" />
declare function unescape(es: any): any;
module acctMgmtReSetPasswordCtrl {
import IModalService = app.IModalService;
import IUserService = services.IUserService;
interface IAcctMgmtResetPasswordController {
passwordStrengthMeter(): any;
resetPassword(): any;
login(): any;
}
interface IAcctMgmtResetPasswordScope extends angular.IScope {
myForm: angular.IFormController
}
class AcctMgmtResetPasswordController implements IAcctMgmtResetPasswordController {
static $inject = [
'passwordStrengthSrvc',
'apiSrvc',
'mydnaApis',
'errorHandlerSrvc',
'$scope',
'$routeParams',
'locationSrvc',
'authenSrvc',
'userService',
'appLocalStorage',
'modalService'
];
frm: any;
strengthClassName: string;
strengthUserText: string;
showErrors: any;
UrlParameters: any;
setPasswordError: boolean = false;
expiredError: boolean;
errorCode: any = '';
constructor(
private passwordStrengthSrvc: passwordStrengthSrvc.IPasswordStrengthService,
private apiSrvc: apiSrvc.IApiService,
private mydnaApis: ImyDNAApis,
private errorHandlerSrvc: errorHandlerSrvc.IErrorHandlerService,
private $scope: IAcctMgmtResetPasswordScope,
private $routeParams: angular.route.IRouteParamsService,
private locationSrvc: locationSrvc.ILocationService,
private authenSrvc : authenSrvc.IAuthenService,
private userService: IUserService,
private storage: app.IAppStorage,
private modalService: IModalService
){
let vm = this;
vm.showErrors = false;
vm.frm = {
Password: '',
ConfirmPassword: ''
};
let urlParameters = vm.locationSrvc.getParams();
if (urlParameters.token) { // this is the old method and should be removed
vm.frm.PasswordResetToken = unescape(urlParameters.token);
vm.frm.PasswordResetToken = vm.frm.PasswordResetToken.split(' ').join('+');
vm.frm.Username = urlParameters.username;
} else {
vm.frm.PasswordResetToken = $routeParams.token;
vm.frm.Username = this.$routeParams.user;
}
}
passwordStrengthMeter() {
let strength = this.passwordStrengthSrvc.checkStrength(this.frm.Password);
this.strengthClassName = strength.className;
this.strengthUserText = strength.userText;
}
resetPassword() {
this.setPasswordError = false;
this.showErrors = this.showErrors == true ? false : "";
if (this.$scope.myForm.$valid) {
this.$scope.$emit("disable-on-save-on");
this.apiSrvc.post_request(
this.mydnaApis.ResetPassword
, this.frm).then(data => {
if (data.data.Succeeded){
this.modalService.add('modal-notification', {
title: 'Success!',
content: 'Your password has been reset.'
}).then(() => {
this.login();
})
}
else {
this.showErrors = true;
if (data.data.Errors.indexOf('Invalid token.') > -1) {
this.expiredError = true;
}
else {
this.setPasswordError = true;
}
throw new Error(data);
}
//this.locationSrvc.goto("/acctmgmt/login", "");
this.$scope.$emit("disable-on-save-off");
}, err => {
this.errorHandlerSrvc.errorHandler(err, 'debug', '');
this.$scope.$emit("disable-on-save-off");
});
}
else {
this.showErrors = true;
}
}
login() {
this.userService.login({
grant_type: 'password',
client_id: OAuthClientId,
client_secret: OAuthSecret,
scope: OAuthScope,
Username: this.frm.Username,
Password: this.frm.Password,
RememberMe: false
}).then((data: any) => {
this.locationSrvc.goto('dashboard', '');
}, err => {
if (err.status == 400) {
this.errorCode = '400';
}
else {
this.errorHandlerSrvc.errorHandler(err, 'debug', '');
}
});
}
}
angular.module('app').controller('acctMgmtResetPasswordCtrl', AcctMgmtResetPasswordController);
}