Guest User

Untitled

a guest
Oct 18th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
  3. import { UserService } from '../user/user.service';
  4. import { Observable } from 'rxjs/Observable';
  5.  
  6. @Injectable()
  7. export class AdminGuard implements CanActivate {
  8. constructor(private router: Router, private userService: UserService) {}
  9.  
  10. canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
  11. if (this.userService.currentUser && JSON.stringify(this.userService.currentUser.authorities).search("ROLE_ADMIN") !== -1) {
  12. return true;
  13. } else {
  14. console.log("NOT AN ADMIN ROLE");
  15. this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
  16. return false;
  17. }
  18. }
  19. }
  20.  
  21. import { Inject } from '@angular/core';
  22. import { Component, OnInit, OnDestroy } from '@angular/core';
  23. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  24. import { Router, ActivatedRoute } from '@angular/router';
  25. import { DisplayMessage } from '../shared/models/display-message';
  26. import { Subscription } from 'rxjs/Subscription';
  27. import { AuthService } from '../shared/';
  28. import { UserService } from '../user/user.service';
  29. import { Observable, Subject } from 'rxjs';
  30. import 'rxjs/add/observable/interval';
  31.  
  32. @Component({
  33. selector: 'app-login',
  34. templateUrl: './login.component.html',
  35. styleUrls: ['./login.component.scss']
  36. })
  37. export class LoginComponent implements OnInit {
  38. title = 'Login';
  39. githubLink = 'https://github.com/zhengye1/Eatr';
  40.  
  41. form: FormGroup;
  42.  
  43. /**
  44. * Boolean used in telling the UI
  45. * that the form has been submitted
  46. * and waiting for response
  47. */
  48. submitted = false;
  49.  
  50. /**
  51. * Used for displayed error message
  52. * from received form request or router
  53. */
  54. notification: DisplayMessage;
  55.  
  56. returnUrl: string;
  57.  
  58. private ngUnsubscribe: Subject<void> = new Subject<void>();
  59.  
  60. constructor(private userService: UserService,
  61. private authService: AuthService,
  62. private router: Router,
  63. private route: ActivatedRoute,
  64. private formBuilder: FormBuilder) { }
  65.  
  66. ngOnInit() {
  67. this.route.params
  68. .takeUntil(this.ngUnsubscribe)
  69. .subscribe((params: DisplayMessage) => {
  70. this.notification = params;
  71. });
  72. // get return url from route parameters or default to '/'
  73. this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
  74.  
  75. this.form = this.formBuilder.group({
  76. username: ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(64)])],
  77. password: ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(32)])]
  78. });
  79. }
  80. ngOnDestroy() {
  81. this.ngUnsubscribe.next();
  82. this.ngUnsubscribe.complete();
  83. }
  84.  
  85. onResetCredentials() {
  86. this.userService.resetCredentials()
  87. .takeUntil(this.ngUnsubscribe)
  88. .subscribe(res => {
  89. if (res.result === 'success') {
  90. alert('Password has been reset to 123 for all accounts');
  91. } else {
  92. alert('Server error');
  93. }
  94. });
  95. }
  96.  
  97. repository() {
  98. window.location.href = this.githubLink;
  99. }
  100.  
  101. onSubmit() {
  102. /**
  103. * Innocent until proven guilty
  104. */
  105. this.notification = undefined;
  106. this.submitted = true;
  107.  
  108. this.authService.login(this.form.value)
  109. // show me the animation
  110. .delay(1000)
  111. .subscribe(data => {
  112. this.router.navigate([this.returnUrl]);
  113. //this.userService.getMyInfo().subscribe();
  114. },
  115. error => {
  116. this.submitted = false;
  117. this.notification = { msgType: 'error', msgBody: 'Incorrect username or password.' };
  118. });
  119.  
  120. }
  121. }
  122.  
  123. <div class="content" fxLayout="row" fxLayoutAlign="center">
  124.  
  125. <md-card elevation="5" fxFlex>
  126.  
  127. <md-card-subtitle>
  128. <h2>Eatr Application</h2>
  129. </md-card-subtitle>
  130.  
  131. <md-card-title>
  132. <h2>{{title}}</h2>
  133. </md-card-title>
  134.  
  135. <md-card-content>
  136.  
  137. <p [class]="notification.msgType" *ngIf="notification">{{notification.msgBody}}</p>
  138.  
  139. <form *ngIf="!submitted" [formGroup]="form" (ngSubmit)="onSubmit()" #loginForm="ngForm">
  140. <md-input-container>
  141. <input mdInput formControlName="username" required placeholder="user/admin">
  142. </md-input-container>
  143. <md-input-container>
  144. <input mdInput formControlName="password" required type="password" placeholder="123">
  145. </md-input-container>
  146. <button type="submit" [disabled]="!loginForm.form.valid" md-raised-button color="primary">Login</button>
  147. </form>
  148. <br>
  149. <div *ngIf="!submitted" >
  150. <button mdTooltip="Reset password if someone has changed it already!" (click)="onResetCredentials()" md-raised-button color="accent">Reset Credentials</button>
  151. </div>
  152.  
  153. <md-spinner *ngIf="submitted" mode="indeterminate"></md-spinner>
  154. <br>
  155. <hr>
  156.  
  157. <p><i>Created by <a href="https://github.com/zhengye1/">Vincent Zheng</a></i></p>
  158. <p><i>Click below to go to repository</i></p>
  159. <button (click)="repository()" md-raised-button color="accent">GitHub Repository</button>
  160.  
  161. </md-card-content>
  162.  
  163. </md-card>
  164.  
  165. </div>
Add Comment
Please, Sign In to add comment