Guest User

Untitled

a guest
Oct 28th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { FormsModule } from '@angular/forms';
  4. import { HttpModule } from '@angular/http';
  5. import { HashLocationStrategy, LocationStrategy } from '@angular/common';
  6.  
  7. import { AppComponent } from './app.component';
  8. import { LoginComponent } from './login/login.component';
  9. import { RegisterComponent } from './register/register.component';
  10.  
  11. /* Routing Module */
  12. import { AppRoutingModule } from './app-routing.module';
  13.  
  14. /* Authentication */
  15. import {AuthenticateService} from './authenticate.service';
  16. import {AuthGuard} from './auth.guard';
  17.  
  18.  
  19. import {DashboardModule} from './dashboard/dashboard.module';
  20.  
  21. @NgModule({
  22. declarations: [
  23. AppComponent,
  24. LoginComponent,
  25. RegisterComponent
  26. ],
  27. imports: [
  28. BrowserModule,
  29. FormsModule,
  30. HttpModule,
  31. DashboardModule,
  32. AppRoutingModule
  33. ],
  34. providers: [
  35. { provide: LocationStrategy, useClass: HashLocationStrategy},
  36. AuthenticateService,
  37. AuthGuard
  38. ],
  39. bootstrap: [AppComponent]
  40. })
  41. export class AppModule { }
  42.  
  43. import { Injectable } from '@angular/core';
  44. import {Response, Headers, Http, RequestOptionsArgs} from '@angular/http';
  45. import {Observable} from 'rxjs/Observable';
  46. import 'rxjs/add/operator/map';
  47. import 'rxjs/add/operator/catch';
  48.  
  49. @Injectable()
  50. export class AuthenticateService {
  51.  
  52. baseUrl = 'myurl/public';
  53. constructor(private http: Http) { }
  54. register(form) {
  55. return this.http.post(`${this.baseUrl}/register`, form)
  56. .map((response: Response) => {
  57. const user = response.json();
  58. if (user.status) {
  59. // store user details and jwt token in local storage to keep user logged in between page refreshes
  60. localStorage.setItem('loggedUser', JSON.stringify(user));
  61. }
  62. return user;
  63. })
  64. .catch((error: any) => Observable.throw(error.json().error || {message: 'Server Error'}));
  65. }
  66.  
  67. login(form): Observable<Response> {
  68. const headersObj = new Headers();
  69. headersObj.set('Content-Type', 'application/json');
  70.  
  71. const requestArg: RequestOptionsArgs = { headers: headersObj, method: 'POST' };
  72. // const telephone = form.telephone;
  73. // const password = form.password;
  74. return this.http.post(`${this.baseUrl}/login`, form, requestArg)
  75. .map((response: Response) => {
  76. const user = response.json();
  77. if (user.response.status) {
  78. localStorage.setItem('loggedUser', JSON.stringify(user.response));
  79. }
  80. return user.response;
  81. })
  82. .catch((error: any) => Observable.throw(error.json().error || {message: 'Server Error'}));
  83. }
  84.  
  85. logout() {
  86. // remove user from local storage to log user out
  87. localStorage.removeItem('loggedUser');
  88. }
  89.  
  90. }
  91.  
  92. import { Injectable } from '@angular/core';
  93. import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
  94. // import {AuthenticateService} from './authenticate.service';
  95.  
  96. @Injectable()
  97. export class AuthGuard implements CanActivate {
  98. constructor(private router: Router) {
  99. }
  100.  
  101. canActivate(route: ActivatedRouteSnapshot,
  102. state: RouterStateSnapshot): boolean {
  103. if ((!localStorage.getItem('loggedUser'))) {
  104. // not logged in so redirect to login page
  105. this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
  106. return false;
  107. } else {
  108. return true;
  109. }
  110. }
  111. }
  112.  
  113. import { NgModule } from '@angular/core';
  114. import {RouterModule, Routes} from '@angular/router';
  115. import {LoginComponent} from './login/login.component';
  116. import {RegisterComponent} from './register/register.component';
  117.  
  118.  
  119. export const routes: Routes = [
  120. {path: '', redirectTo: 'dashboard', pathMatch: 'full'},
  121. {path: 'login', component: LoginComponent},
  122. {path: 'register', component: RegisterComponent},
  123. ];
  124. @NgModule({
  125. imports: [
  126. RouterModule.forRoot(routes)
  127. ],
  128. exports: [
  129. RouterModule
  130. ]
  131. })
  132. export class AppRoutingModule { }
  133.  
  134. import { NgModule } from '@angular/core';
  135. import { CommonModule } from '@angular/common';
  136. import { RouterModule } from '@angular/router';
  137. import {FormsModule} from '@angular/forms';
  138.  
  139. import { DashboardRoutingModule } from './dashboard-routing.module';
  140.  
  141. import { DashboardComponent } from './dashboard.component';
  142. import { DashboardHomeComponent } from './dashboard-home/dashboard-home.component';
  143. import { UsersComponent } from './users/users.component';
  144. import { TransactionsComponent } from './transactions/transactions.component';
  145. import { UsersService } from './services/users.service';
  146.  
  147. @NgModule({
  148. imports: [
  149. CommonModule,
  150. FormsModule,
  151. RouterModule,
  152. DashboardRoutingModule
  153. ],
  154. exports: [ DashboardComponent ],
  155. declarations: [ DashboardComponent, DashboardHomeComponent, UsersComponent, TransactionsComponent],
  156. providers: [ UsersService ]
  157. })
  158. export class DashboardModule { }
  159.  
  160. import { NgModule } from '@angular/core';
  161. import { RouterModule, Routes } from '@angular/router';
  162.  
  163. import {DashboardComponent} from './dashboard.component';
  164. import {DashboardHomeComponent} from './dashboard-home/dashboard-home.component';
  165. import {UsersComponent} from './users/users.component';
  166. import {TransactionsComponent} from './transactions/transactions.component';
  167. import {AuthGuard} from '../auth.guard';
  168.  
  169.  
  170. export const dashboardRoutes: Routes = [
  171. {
  172. path: 'dashboard',
  173. component: DashboardComponent,
  174. canActivate: [ AuthGuard ],
  175. children: [
  176. {path: '', component: DashboardHomeComponent},
  177. {path: 'users', component: UsersComponent},
  178. {path: 'transactions', component: TransactionsComponent}
  179. ]}
  180. // {path: 'dashboard', component: DashboardComponent}
  181. ];
  182. @NgModule({
  183. imports: [
  184. RouterModule.forChild(dashboardRoutes)
  185. ],
  186. exports: [
  187. RouterModule
  188. ],
  189. declarations: []
  190. })
  191. export class DashboardRoutingModule { }
Add Comment
Please, Sign In to add comment