Created
October 24, 2016 17:42
-
-
Save chl03ks/6fc5c6e6e5d93fccfaf88c6b8b266a64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, OnDestroy } from '@angular/core'; | |
import { ActivatedRoute, Router, Params } from '@angular/router'; | |
import { ToastsManager } from 'ng2-toastr/ng2-toastr'; | |
import { CustomerApi } from '../../shared/sdk/services/custom/Customer'; | |
import { Customer } from '../../shared/sdk/models/Customer'; | |
import { LoopBackAuth } from '../../shared/sdk/services/core/auth.service'; | |
import * as zxcvbn from 'zxcvbn'; | |
@Component({ | |
moduleId : module.id, | |
selector : 'fl-reset-password', | |
templateUrl: 'reset-password.component.html', | |
styleUrls : ['reset-password.component.css'] | |
}) | |
export class ResetPasswordComponent implements OnInit, OnDestroy { | |
public strength: Object; | |
public passwordStrength: any = {}; | |
private password: string = ''; | |
private passwordConfimation: string = ''; | |
constructor(private _toastr: ToastsManager, | |
private customer: CustomerApi, | |
private authService: LoopBackAuth, | |
private route: ActivatedRoute, | |
private router: Router, | |
) { | |
this.strength = { | |
0: 'Worst', | |
1: 'Bad', | |
2: 'Weak', | |
3: 'Good', | |
4: 'Strong' | |
}; | |
} | |
ngOnInit() { | |
this.route.params.forEach((params: Params) => { | |
let accestoken = params['accestoken']; | |
let id = params['id']; | |
this.authService.setUser(accestoken, id, {}); | |
this.authService.save(); | |
}); | |
} | |
public newPassword() { | |
if (this.password !== '') { | |
console.log('im here'); | |
if (this.password === this.passwordConfimation) { | |
console.log('im here'); | |
let currentUserId = this.authService.getCurrentUserId(); | |
this.customer | |
.updateAttributes(currentUserId, { password: this.password }) | |
.subscribe( | |
(res: Customer) => { | |
if (res) { | |
console.log('hi'); | |
this.router.navigate(['login']); | |
this._toastr.success('Your password has been reset successfully','Yeah'); | |
} | |
}, | |
(err: any) => { | |
this._toastr.error('Something went wrong try again later', 'Ups!'); | |
} | |
); | |
} else if (this.password.length < 6) { | |
this._toastr.error('Your password can not be less than 6 characters', 'Hmmm!'); | |
} else { | |
this._toastr.error('The passwords you entered didn’t match. Please try again!', 'Hmmm!'); | |
} | |
} else { | |
this._toastr.error('Your password can not be empty', 'Hmmm!'); | |
} | |
} | |
public passwordStrengthCheck() { | |
if (this.password !== '') { | |
let result = zxcvbn(this.password); | |
this.passwordStrength.text = 'Password strength'; | |
this.passwordStrength.result = this.strength[result.score]; | |
this.passwordStrength.score = result.score; | |
this.passwordStrength.warning = result.feedback.warning; | |
this.passwordStrength.feedback = result.feedback.suggestion; | |
} else { | |
this.passwordStrength = {}; | |
} | |
} | |
ngOnDestroy() { | |
this.authService.clearStorage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment