Guest User

Untitled

a guest
Apr 13th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. // auth.service.ts
  2.  
  3. import { Injectable } from '@angular/core';
  4. import { HttpClient, HttpHeaders } from '@angular/common/http';
  5. import { Observable } from 'rxjs/Observable';
  6.  
  7. import 'rxjs/add/operator/map';
  8. import 'rxjs/add/operator/mergeMap';
  9.  
  10. import { JwtHelperService } from '@auth0/angular-jwt';
  11.  
  12. interface IUser
  13. {
  14. token: string,
  15. token_expires: number,
  16. user_display_name: string,
  17. }
  18.  
  19. @Injectable()
  20. export class AuthService
  21. {
  22. private redirectUrl: string = '/';
  23. private loginUrl: string = 'auth/login-2';
  24. private currentUser: IUser = null;
  25. private nonce: string = '';
  26.  
  27. constructor(
  28. private http: HttpClient,
  29. private jwt: JwtHelperService,
  30. )
  31. {
  32. // set User Info if saved in local/session storage
  33. this.currentUser = <IUser>JSON.parse(sessionStorage.getItem('currentUser'));
  34. if (!this.currentUser) this.currentUser = <IUser>JSON.parse(sessionStorage.getItem('currentUser'));
  35. if (!this.currentUser) this.currentUser = <IUser>JSON.parse(localStorage.getItem('currentUser'));
  36. }
  37.  
  38. logIn(username: string, password: string, persist: boolean): Observable<boolean>
  39. {
  40. return this.http.post(
  41. API_BASE_DOMAIN + API_BASE_PATH + '/jwt-auth/v1/token',
  42. { username: username, password: password },
  43. {
  44. observe: 'response', // Full response instead of the body only
  45. withCredentials: true, // Send cookies
  46. }
  47. ).map(response => {
  48. this.nonce = response.headers.get('X-WP-Nonce');
  49. const body: any = response.body
  50.  
  51. // login successful if there's a jwt token in the response
  52. if (body.token)
  53. {
  54. // set current user data
  55. this.currentUser = <IUser>body;
  56.  
  57. // store username and jwt token in local storage to keep user logged in between page refreshes
  58. let storage = (persist) ? localStorage : sessionStorage;
  59. storage.setItem('currentUser', JSON.stringify(this.currentUser));
  60.  
  61. // return true to indicate successful login
  62. return true;
  63. }
  64. else
  65. {
  66. // return false to indicate failed login
  67. return false;
  68. }
  69. });
  70. }
  71.  
  72. getNonce(): string
  73. {
  74. console.log((this.nonce) ? this.nonce : '');
  75. return (this.nonce) ? this.nonce : '';
  76. }
  77.  
  78. getToken(): string
  79. {
  80. console.log((this.currentUser) ? this.currentUser.token : '');
  81. return (this.currentUser) ? this.currentUser.token : '';
  82. }
  83. }
  84.  
  85.  
  86.  
  87. // app.module.ts
  88.  
  89. import { NgModule } from '@angular/core';
  90. import { BrowserModule } from '@angular/platform-browser';
  91. import { HttpClientModule } from '@angular/common/http';
  92. import { JwtModule } from '@auth0/angular-jwt';
  93. import { RouterModule, Routes } from '@angular/router';
  94.  
  95. import { AppComponent } from './app.component';
  96.  
  97. import { AuthService } from './main/auth/services/auth.service';
  98. import { AuthGuardService } from './main/auth/services/auth-guard.service';
  99.  
  100. import { API_BASE_DOMAIN } from './main/auth/services/auth.service';
  101. import { HTTP_INTERCEPTORS } from '@angular/common/http';
  102. import { NonceHttpInterceptor } from './main/auth/auth.interceptor';
  103.  
  104. const appRoutes: Routes = [
  105. .
  106. .
  107. .
  108. ];
  109.  
  110.  
  111. @NgModule({
  112. declarations: [
  113. AppComponent
  114. ],
  115. imports : [
  116. BrowserModule,
  117. HttpClientModule,
  118. RouterModule.forRoot(appRoutes),
  119.  
  120. // Jwt Token Injection
  121. JwtModule.forRoot({
  122. config: {
  123. tokenGetter: (***AuthService?????***).getToken,
  124. whitelistedDomains: [ API_BASE_DOMAIN ]
  125. }
  126. })
  127. ],
  128. providers : [
  129. AuthService,
  130. AuthGuardService,
  131. {
  132. provide: HTTP_INTERCEPTORS,
  133. useClass: NonceHttpInterceptor,
  134. multi: true
  135. }
  136. ],
  137. bootstrap : [
  138. AppComponent
  139. ]
  140. })
  141.  
  142. export class AppModule
  143. {
  144. }
  145.  
  146. //login.component.ts
  147. import { AuthService } from '../services/auth.service';
  148.  
  149. @Component({
  150. .
  151. .
  152. .
  153. })
  154.  
  155. export class MyLoginComponent implements OnInit
  156. {
  157. constructor(
  158. private authService: AuthService,
  159. )
  160. {
  161. .
  162. .
  163. .
  164. }
  165.  
  166. onFormSubmit()
  167. {
  168. this.authService.logIn(uname, pwd, persist)
  169. .subscribe(
  170. .
  171. .
  172. .
  173. );
  174. }
  175. }
Add Comment
Please, Sign In to add comment