Guest User

Untitled

a guest
Apr 16th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. Status Code: 500 Internal Server Error and
  2. x-access-token: myToken
  3.  
  4. import { BrowserModule } from '@angular/platform-browser';
  5. import { NgModule } from '@angular/core';
  6. import { HttpClientModule } from '@angular/common/http';
  7. import { RouterModule, Routes } from '@angular/router';
  8. import { FormsModule } from '@angular/forms';
  9. import { ReactiveFormsModule } from '@angular/forms'; // <-- #1 import module
  10.  
  11. import { AppComponent } from './app.component';
  12. import { NavbarComponent } from './components/navbar/navbar.component';
  13. import { HomeComponent } from './components/home/home.component';
  14. import { DashbaordComponent } from './components/dashbaord/dashbaord.component';
  15. import { LoginComponent } from './components/login/login.component';
  16. import { RegisterComponent } from './components/register/register.component';
  17. import { ProfileComponent } from './components/profile/profile.component';
  18.  
  19.  
  20. import { ConfigService } from './services/config.service';
  21. import { AuthComponent } from './components/auth/auth.component';
  22.  
  23.  
  24. const appRoutes: Routes = [
  25. { path: 'home', component: HomeComponent },
  26. { path: 'register', component: RegisterComponent },
  27. { path: 'login', component: LoginComponent },
  28. { path: 'profile', component: ProfileComponent },
  29. { path: 'auth', component: AuthComponent },
  30. {
  31. path: 'dashboard',
  32. component: DashbaordComponent,
  33. data: { title: 'dashboard' }
  34. },
  35. { path: '',
  36. redirectTo: '/home',
  37. pathMatch: 'full'
  38. },
  39. { path: '**', component: HomeComponent }
  40. ];
  41.  
  42. @NgModule({
  43. declarations: [
  44. AppComponent,
  45. NavbarComponent,
  46. HomeComponent,
  47. DashbaordComponent,
  48. LoginComponent,
  49. RegisterComponent,
  50. ProfileComponent,
  51. AuthComponent
  52. ],
  53. imports: [
  54. BrowserModule,
  55. // import HttpClientModule after BrowserModule.
  56. HttpClientModule,
  57. FormsModule,
  58. ReactiveFormsModule,
  59. RouterModule.forRoot(
  60. appRoutes,
  61. { enableTracing: true } // <-- debugging purposes only
  62. )
  63. // other imports here
  64. ],
  65. providers: [ConfigService],
  66. bootstrap: [AppComponent]
  67. })
  68. export class AppModule { }
  69.  
  70. import { Component, OnInit } from '@angular/core';
  71. import { ConfigService } from '../../services/config.service';
  72. import { Config } from '../../interfaces/config';
  73. import { Router } from '@angular/router';
  74.  
  75. @Component({
  76. selector: 'app-login',
  77. templateUrl: './login.component.html',
  78. styleUrls: ['./login.component.css']
  79. })
  80. export class LoginComponent implements OnInit {
  81. username: string;
  82. password: string;
  83. constructor(private configService: ConfigService,private router: Router) { }
  84. ngOnInit() {
  85. }
  86. config : Config;
  87. configs : any;
  88. myToken : any;
  89. logedConfig() {
  90. const newConfiged = {
  91. username: this.username,
  92. password: this.password,
  93. myToken : this.myToken
  94. };
  95. this.configService.logConfig(newConfiged)
  96. .subscribe(data => {
  97. if(data){
  98. this.configs = data;
  99. localStorage.setItem('token', data.myToken)
  100. this.router.navigate(['/dashboard']);
  101. } else{
  102. this.router.navigate(['/login']);
  103. }
  104. } );)
  105. }
  106. }
  107.  
  108. import { Injectable } from '@angular/core';
  109. import { HttpClient } from '@angular/common/http';
  110. import { Observable } from 'rxjs/Observable';
  111. import { Config } from '../interfaces/config';
  112. import { HttpHeaders } from '@angular/common/http';
  113.  
  114. const httpOptions = {
  115. headers: new HttpHeaders({
  116. 'Content-Type': 'application/json',
  117. 'x-access-token': 'myToken'
  118. })
  119. };
  120. @Injectable()
  121. export class ConfigService {
  122. myToken : any;
  123.  
  124. constructor(private http: HttpClient) { }
  125. configUrl = 'http://localhost:3000/api/users';
  126. postUrl = 'http://localhost:3000/api/register';
  127. deleteUrl = 'http://localhost:3000/api/user';
  128. loginUrl = 'http://localhost:3000/api/login';
  129. authUrl = 'http://localhost:3000/api/auth/profile';
  130. getConfig() {
  131. // now returns an Observable of Config
  132. return this.http.get<Config>(this.configUrl);
  133. }
  134. /** POST: add a new hero to the database */
  135. addConfig (config: Config): Observable<Config> {
  136. return this.http.post<Config>(this.postUrl, config, httpOptions)
  137. }
  138. deleteConfig (): Observable<{}> {
  139. // const url = `${this.deleteUrl}/${id}`; // DELETE api/heroes/42
  140. return this.http.delete(this.deleteUrl, httpOptions)
  141. }
  142. logConfig (config: Config): Observable<Config> {
  143. return this.http.post<Config>(this.loginUrl, config, httpOptions)
  144. }
  145. authConfig() {
  146. localStorage.getItem('token');
  147. return this.http.get<Config>(this.authUrl, httpOptions);
  148. }
  149. }
  150.  
  151. import { Component, OnInit } from '@angular/core';
  152. import { ConfigService } from '../../services/config.service';
  153. import { Config } from '../../interfaces/config';
  154. import { Router } from '@angular/router';
  155. @Component({
  156. selector: 'app-auth',
  157. templateUrl: './auth.component.html',
  158. styleUrls: ['./auth.component.css']
  159. })
  160. export class AuthComponent implements OnInit {
  161. myToken :any;
  162. constructor(private configService: ConfigService,private router: Router) { }
  163. config: Config;
  164. configs : any;
  165. ngOnInit() {
  166. this.doConfig()
  167. }
  168. doConfig() {
  169. this.configService.authConfig()
  170. // clone the data object, using its known Config shape
  171. .subscribe(data => this.configs = data );
  172. }
  173. }
  174.  
  175. <div class="jumbotron jumbotron-fluid">
  176. <div class="container text-center">
  177. <h1 class="display-4">Profile</h1>
  178. <button type="button" class="btn btn-danger">Do nothing</button>
  179. <p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
  180. <ul class="list-group" *ngFor="let config of configs">
  181. <li class="list-group-item">{{config.username}}
  182. </li>
  183.  
  184. </ul>
  185. </div>
  186. </div>
Add Comment
Please, Sign In to add comment