Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
- import { UserService } from '../user/user.service';
- import { Observable } from 'rxjs/Observable';
- @Injectable()
- export class AdminGuard implements CanActivate {
- constructor(private router: Router, private userService: UserService) {}
- canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
- if (this.userService.currentUser && JSON.stringify(this.userService.currentUser.authorities).search("ROLE_ADMIN") !== -1) {
- return true;
- } else {
- console.log("NOT AN ADMIN ROLE");
- this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
- return false;
- }
- }
- }
- import { Inject } from '@angular/core';
- import { Component, OnInit, OnDestroy } from '@angular/core';
- import { FormBuilder, FormGroup, Validators } from '@angular/forms';
- import { Router, ActivatedRoute } from '@angular/router';
- import { DisplayMessage } from '../shared/models/display-message';
- import { Subscription } from 'rxjs/Subscription';
- import { AuthService } from '../shared/';
- import { UserService } from '../user/user.service';
- import { Observable, Subject } from 'rxjs';
- import 'rxjs/add/observable/interval';
- @Component({
- selector: 'app-login',
- templateUrl: './login.component.html',
- styleUrls: ['./login.component.scss']
- })
- export class LoginComponent implements OnInit {
- title = 'Login';
- githubLink = 'https://github.com/zhengye1/Eatr';
- form: FormGroup;
- /**
- * Boolean used in telling the UI
- * that the form has been submitted
- * and waiting for response
- */
- submitted = false;
- /**
- * Used for displayed error message
- * from received form request or router
- */
- notification: DisplayMessage;
- returnUrl: string;
- private ngUnsubscribe: Subject<void> = new Subject<void>();
- constructor(private userService: UserService,
- private authService: AuthService,
- private router: Router,
- private route: ActivatedRoute,
- private formBuilder: FormBuilder) { }
- ngOnInit() {
- this.route.params
- .takeUntil(this.ngUnsubscribe)
- .subscribe((params: DisplayMessage) => {
- this.notification = params;
- });
- // get return url from route parameters or default to '/'
- this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
- this.form = this.formBuilder.group({
- username: ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(64)])],
- password: ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(32)])]
- });
- }
- ngOnDestroy() {
- this.ngUnsubscribe.next();
- this.ngUnsubscribe.complete();
- }
- onResetCredentials() {
- this.userService.resetCredentials()
- .takeUntil(this.ngUnsubscribe)
- .subscribe(res => {
- if (res.result === 'success') {
- alert('Password has been reset to 123 for all accounts');
- } else {
- alert('Server error');
- }
- });
- }
- repository() {
- window.location.href = this.githubLink;
- }
- onSubmit() {
- /**
- * Innocent until proven guilty
- */
- this.notification = undefined;
- this.submitted = true;
- this.authService.login(this.form.value)
- // show me the animation
- .delay(1000)
- .subscribe(data => {
- this.router.navigate([this.returnUrl]);
- //this.userService.getMyInfo().subscribe();
- },
- error => {
- this.submitted = false;
- this.notification = { msgType: 'error', msgBody: 'Incorrect username or password.' };
- });
- }
- }
- <div class="content" fxLayout="row" fxLayoutAlign="center">
- <md-card elevation="5" fxFlex>
- <md-card-subtitle>
- <h2>Eatr Application</h2>
- </md-card-subtitle>
- <md-card-title>
- <h2>{{title}}</h2>
- </md-card-title>
- <md-card-content>
- <p [class]="notification.msgType" *ngIf="notification">{{notification.msgBody}}</p>
- <form *ngIf="!submitted" [formGroup]="form" (ngSubmit)="onSubmit()" #loginForm="ngForm">
- <md-input-container>
- <input mdInput formControlName="username" required placeholder="user/admin">
- </md-input-container>
- <md-input-container>
- <input mdInput formControlName="password" required type="password" placeholder="123">
- </md-input-container>
- <button type="submit" [disabled]="!loginForm.form.valid" md-raised-button color="primary">Login</button>
- </form>
- <br>
- <div *ngIf="!submitted" >
- <button mdTooltip="Reset password if someone has changed it already!" (click)="onResetCredentials()" md-raised-button color="accent">Reset Credentials</button>
- </div>
- <md-spinner *ngIf="submitted" mode="indeterminate"></md-spinner>
- <br>
- <hr>
- <p><i>Created by <a href="https://github.com/zhengye1/">Vincent Zheng</a></i></p>
- <p><i>Click below to go to repository</i></p>
- <button (click)="repository()" md-raised-button color="accent">GitHub Repository</button>
- </md-card-content>
- </md-card>
- </div>
Add Comment
Please, Sign In to add comment