Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. import {Component} from '@angular/core';
  2. import {CORE_DIRECTIVES} from '@angular/common';
  3. import {Router} from '@angular/router';
  4. import {AuthService} from '../../auth.service';
  5. import {RoleService} from '../../auth/role.service';
  6.  
  7. @Component({
  8. selector: 'settings',
  9. directives: [ CORE_DIRECTIVES ],
  10. template: `
  11. <div class="row">
  12. <div class="col-xs-12 text-xs-center">
  13. Username: <strong>{{user()?.attributes?.username}}</strong><br/>
  14. Member Since: <strong>{{user()?.createdAt | date}}</strong><br/>
  15. Role: <strong>{{roleService.role?.attributes?.displayName}}</strong>
  16. <hr/>
  17. </div>
  18. </div>
  19. <div class="row">
  20. <div class="col-xs-12 col-md-6">
  21. <div class="card">
  22. <div class="card-header">
  23. <h5 style="margin-bottom:0px;">Change Password</h5>
  24. <small>Use this form to change your password to a new one.</small>
  25. </div>
  26. <div class="card-block">
  27. <form (ngSubmit)="changePassword()">
  28. <fieldset class="form-group">
  29. <label>New Password</label>
  30. <input type="password" class="form-control" autocomplete="off" name="password"
  31. placeholder="Enter password" [(ngModel)]="password">
  32. </fieldset>
  33. <fieldset class="form-group">
  34. <label>Confirm New Password</label>
  35. <input type="password" class="form-control" autocomplete="off" name="passwordConfirm"
  36. placeholder="Repeat password" [(ngModel)]="passwordConfirm">
  37. </fieldset>
  38. <div *ngIf="passwordChangeMessage.length > 0 && !passwordChangeInProgress">
  39. <div class="alert alert-danger" role="alert">
  40. <i class="fa fa-exclamation-triangle"></i> {{passwordChangeMessage}}
  41. </div>
  42. </div>
  43. <small>
  44. When the password is changed you will be signed out and will have to sign in again with your new password.
  45. </small>
  46. <hr/>
  47. <div class="text-xs-center">
  48. <button type="submit" class="btn btn-primary"><i class="fa fa-floppy-o"></i> Change Password</button>
  49. </div>
  50. </form>
  51. </div>
  52. </div>
  53. </div>
  54. <div class="col-md-6">
  55.  
  56. </div>
  57. </div>
  58. <div class="row">
  59. <div class="col-xs-12 col-md-6">
  60. <div class="card">
  61. <div class="card-header">
  62. <h5 style="margin-bottom:0px;">Log Out</h5>
  63. <small>Sign out of your account.</small>
  64. </div>
  65. <div class="card-block text-xs-center">
  66. <button class="btn btn-danger" (click)="logout()"><i class="fa fa-sign-out"></i> Log Out</button>
  67. </div>
  68. </div>
  69. </div>
  70. </div>`
  71. })
  72.  
  73. export class GeneralSettingsComponent {
  74. private password: string = '';
  75. private passwordConfirm: string = '';
  76. private passwordChangeInProgress = false;
  77. private passwordChangeMessage = '';
  78.  
  79. constructor(private router: Router, private authService: AuthService, private roleService: RoleService) { }
  80.  
  81. user() {
  82. return this.authService.user();
  83. }
  84.  
  85. changePassword() {
  86. if (!this.password || !this.passwordConfirm) {
  87. this.passwordChangeMessage = 'Please enter a password and confirm it.';
  88. return;
  89. }
  90.  
  91. if (this.passwordConfirm !== this.password) {
  92. this.passwordChangeMessage = 'New passwords do not match.';
  93. return;
  94. }
  95.  
  96. if (this.password.length < 5) {
  97. this.passwordChangeMessage = 'Password must be longer than 4 characters.';
  98. return;
  99. }
  100.  
  101. this.passwordChangeInProgress = true;
  102.  
  103. this.authService.changePassword(this.password).then(success => {
  104. this.authService.logout();
  105. this.router.navigateByUrl('../login');
  106. }, error => {
  107. this.passwordChangeInProgress = false;
  108. this.passwordChangeMessage = 'Password Update Failed! ' + error.message;
  109. alert(JSON.stringify(error));
  110. });
  111. }
  112.  
  113. logout() {
  114. this.authService.logout();
  115. this.router.navigateByUrl('/login');
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement