Guest User

Untitled

a guest
Dec 7th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. <ul class="list-group">
  2. <li *ngIf="isLoggedIn" (click)="onLogout()">Logout</li>
  3. </ul>
  4.  
  5. import { Component, OnInit } from '@angular/core';
  6. import { HttpService } from '../../http.service';
  7. import { Subscription } from 'rxjs';
  8.  
  9. @Component({
  10. selector: 'app-menu-items',
  11. templateUrl: './menu-items.component.html',
  12. styleUrls: ['./menu-items.component.css']
  13. })
  14. export class MenuItemsComponent implements OnInit {
  15. isLoggedIn: boolean = false;
  16. constructor(private httpService: HttpService) {
  17.  
  18. }
  19.  
  20. ngOnInit() {
  21.  
  22. }
  23. onSignout(){
  24. this.isLoggedIn = false;
  25. localStorage.removeItem('currentUser');
  26. }
  27. }
  28.  
  29. onSignin(){
  30.  
  31. const username = this.loginForm.get('username').value;
  32. const password = this.loginForm.get('password').value;
  33.  
  34. this.httpServie.sendDataLogin("grant_type=password&username=" + username + "&password=" + password)
  35. .subscribe(
  36. data => {
  37. this.token = JSON.parse(data["_body"]).access_token
  38. this.username = JSON.parse(data["_body"]).userName
  39. localStorage.setItem('currentUser', JSON.stringify({username: this.username, token: this.token}))
  40.  
  41. },
  42. err => {
  43. if (err.error instanceof Error) {
  44.  
  45. this.ServerErrorMsg = err.error.message;
  46. } else {
  47.  
  48. const returnArray = [];
  49. this.ServerErrorMsg = JSON.parse(err._body).error_description + "n";
  50. }
  51. }
  52. );
  53. }
Add Comment
Please, Sign In to add comment