Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2.  
  3. import { UserService } from './user';
  4. import { Router } from '@angular/router';
  5.  
  6. @Component({
  7. selector: 'app-root',
  8. templateUrl: './app.component.html',
  9. styleUrls: ['./app.component.css']
  10. })
  11. export class AppComponent implements OnInit{
  12. title = 'app works!';
  13.  
  14. loginstatus: boolean = false;
  15.  
  16. constructor(private _userService: UserService, private _router: Router){}
  17.  
  18. onLogout(){
  19. this._userService.logout();
  20. this._router.navigate(['/login']);
  21. }
  22.  
  23. ngOnInit(){
  24. this.onLogout();
  25. }
  26. }
  27.  
  28. <ul class="navigation">
  29. <li *ngIf="!loginstatus"><a routerLink="/login" routerLinkActive="active">Login</a></li>
  30. <li *ngIf="loginstatus"><a (click)="onLogout()">Logout</a></li>
  31. </ul>
  32.  
  33. <router-outlet></router-outlet>
  34.  
  35. import { Component, OnInit } from '@angular/core';
  36. import { FormGroup, FormBuilder, Validators} from '@angular/forms';
  37.  
  38. import { UserService } from '../user.service';
  39.  
  40. @Component({
  41. selector: 'login',
  42. templateUrl: './login.component.html',
  43. styleUrls: ['./login.component.css']
  44. })
  45.  
  46. export class LoginComponent implements OnInit{
  47. user = {
  48. username: '',
  49. password: ''
  50. }
  51.  
  52. loginForm: FormGroup;
  53.  
  54. constructor(private _userService: UserService, private _formBuilder: FormBuilder){}
  55.  
  56. ngOnInit(){
  57. this.loginForm = this._formBuilder.group({
  58. username: ['', Validators.required],
  59. password: ['', Validators.required]
  60. });
  61. }
  62.  
  63. onLogin(){
  64. this._userService.postUserLogin(this.user)
  65. .subscribe(token => {
  66. console.log(localStorage.getItem('currentUser'));
  67. })
  68. err => console.log(err);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement