Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. import {Injectable, EventEmitter} from "@angular/core";
  2. import {Http, Headers, Response, RequestOptions} from "@angular/http";
  3. import {Observable} from "rxjs/Observable";
  4. import {AuthHttp} from "./auth.http";
  5. import {User} from "./user";
  6.  
  7. @Injectable()
  8.  
  9. export class AuthService {
  10. authKey = "auth";
  11.  
  12. constructor(private http: AuthHttp) {
  13. }
  14.  
  15. // Persist auth into localStorage or removes it if a NULL argument is given
  16. setAuth(auth: any): boolean {
  17. if (auth) {
  18. localStorage.setItem(this.authKey, JSON.stringify(auth));
  19. }
  20. else {
  21. localStorage.removeItem(this.authKey);
  22. }
  23. return true;
  24. }
  25.  
  26. // Retrieves the auth JSON object (or NULL if none)
  27. getAuth(): any {
  28. var i = localStorage.getItem(this.authKey);
  29. if (i) {
  30. return JSON.parse(i);
  31. }
  32. else {
  33. return null;
  34. }
  35. }
  36.  
  37. // Returns TRUE if the user is logged in, FALSE otherwise.
  38.  
  39. isLoggedIn(): boolean {
  40. return localStorage.getItem(this.authKey) != null;
  41. }
  42.  
  43. **get() {
  44. return this.http.get("api/Accounts")
  45. .map(response => <User>response.json());
  46. }**
  47.  
  48. }
  49.  
  50. /// <summary>
  51. /// GET: api/accounts
  52. /// </summary>
  53. /// <returns>A Json-serialized object representing the current account.</returns>
  54. [HttpGet()]
  55. public IActionResult Get()
  56. {
  57. var id = GetCurrentUserId();
  58. //var id = User.FindFirst(ClaimTypes.NameIdentifier).Value;
  59. var user = DbContext.Users.Where(i => i.Id == id).FirstOrDefault();
  60. if (user != null) return new JsonResult(new UserViewModel()
  61. {
  62. Type = user.Type,
  63. UserName = user.UserName,
  64. Email = user.Email,
  65. DisplayName = user.DisplayName,
  66. PhoneNumber = user.PhoneNumber,
  67. DisplayPhoneNumber = user.DisplayPhoneNumber,
  68. Description = user.Description
  69.  
  70. }, DefaultJsonSettings);
  71. else return NotFound(new { error = String.Format("User ID {0} has not been found", id) });
  72. }
  73.  
  74. export class User {
  75. constructor(
  76. public Type: number,
  77. public UserName: string,
  78. public DisplayName: string,
  79. public PhoneNumber: string,
  80. public DisplayPhoneNumber: boolean,
  81. public Description: string,
  82. public Password: string,
  83. public PasswordNew: string,
  84. public Email: string
  85. ) {
  86. }
  87. }
  88.  
  89. import {Component, OnInit} from "@angular/core";
  90. import {Router, ActivatedRoute} from "@angular/router";
  91. import {AuthService} from "./auth.service";
  92. import {AuthHttp} from "./auth.http";
  93. import {User} from "./user";
  94.  
  95. @Component({
  96. selector: "home",
  97. templateUrl: "templates/home.component.html",
  98. //template: `<h1>Home Component</h1>`
  99. })
  100.  
  101. export class HomeComponent {
  102.  
  103. isRegister: boolean;
  104. currentUser: User;
  105. constructor(
  106. public router: Router,
  107. public authService: AuthService,
  108. private activatedRoute: ActivatedRoute
  109. ) {
  110.  
  111. //this.isRegister = this.authService.isLoggedIn();
  112. //console.log(this.isRegister);
  113. }
  114. ngOnInit() {
  115. if (this.authService.isLoggedIn()) {
  116. this.authService.get().subscribe(
  117. User => this.currentUser = User
  118. );
  119. console.log(this.isRegister);
  120. console.log(this.currentUser.UserName);
  121. }
  122. }
  123. }
  124.  
  125. <div *ngIf="currentUser"><h1>{{currentUser.UserName}}</h1></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement