Advertisement
Guest User

Untitled

a guest
Oct 26th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { map } from 'rxjs/operators';
  4.  
  5. @Injectable()
  6. export class UserService {
  7.  
  8. constructor(
  9. private _httpClient: HttpClient
  10. ) { }
  11.  
  12.  
  13. public auth(user) {
  14. return this._httpClient.post('users/login', user).pipe(map((resp: any) => resp.json()));
  15. }
  16.  
  17. }
  18.  
  19. import { Component, OnInit } from '@angular/core';
  20. import { Router } from '@angular/router';
  21. import { UserService } from '../../services/user.service';
  22.  
  23.  
  24.  
  25. @Component({
  26. selector: 'app-side-menu',
  27. templateUrl: './side-menu.component.html',
  28. styleUrls: ['./side-menu.component.scss']
  29. })
  30. export class SideMenuComponent implements OnInit {
  31.  
  32. public positionsList: Array<any> = [];
  33. public selectedOption: string;
  34. public feedbackMessage: string;
  35.  
  36. public email: string;
  37. public password: string;
  38.  
  39. public formNotValid: boolean;
  40.  
  41. constructor(
  42. private _userService: UserService,
  43. private _router: Router
  44. ) {
  45.  
  46. }
  47.  
  48. ngOnInit() {
  49. }
  50.  
  51. public sendLoginForm() {
  52. if(!this.email || !this.password){
  53. this.formNotValid = true;
  54. this.feedbackMessage = "Both fields are required to login!";
  55. return false;
  56. }
  57.  
  58. const user = {
  59. email: this.email,
  60. password: this.password
  61. }
  62.  
  63. this._userService.auth(user).subscribe(resp => {
  64. if(!resp.success){
  65. this.feedbackMessage = resp.message;
  66. return false;
  67. }
  68. });
  69. }
  70.  
  71. public getSelectedOption(option) {
  72. this.selectedOption = "as " + option;
  73. }
  74.  
  75.  
  76. }
  77.  
  78. this._httpClient.post(window.location.protocol + '//' + window.location.hostname + ':3000/' + 'users/login', user)
  79.  
  80. "scripts": {
  81. .....
  82. "start": "ng serve --proxy-config proxy.config.json",
  83. .....
  84. }
  85.  
  86. {
  87. "/api/*": {
  88. "target": "http://localhost:3000",
  89. "secure": false
  90. },
  91. "/__/*": {
  92. "target": "http://localhost:3100",
  93. "secure": false
  94. }
  95. }
  96.  
  97. import { Injectable } from '@angular/core';
  98. import { HttpClient } from '@angular/common/http';
  99. import { map } from 'rxjs/operators';
  100.  
  101. @Injectable()
  102. export class UserService {
  103. url='http://localhost:3000/'
  104. constructor(
  105. private _httpClient: HttpClient
  106. ) { }
  107.  
  108.  
  109. public auth(user) {
  110. return this._httpClient.post(this.url+'users/login', user).pipe(map((resp: any) => resp.json()));
  111. }
  112.  
  113. }
  114.  
  115. import { Injectable } from '@angular/core';
  116. import { HttpClient } from '@angular/common/http';
  117. import { map } from 'rxjs/operators';
  118.  
  119. @Injectable()
  120. export class UserService {
  121.  
  122. constructor(
  123. private _httpClient: HttpClient
  124. ) { }
  125.  
  126.  
  127. public auth(user) {
  128. return this._httpClient.post('http://localhost:3000/users/login', user).pipe(map((resp: any) => resp.json()));
  129. }
  130.  
  131. }
  132.  
  133. app.use((req , res , next) => {
  134.  
  135. res.header("Access-Control-Allow-Origin" , "*");
  136. res.header(
  137. "Access-Control-Allow-Headers" ,
  138. "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  139. );
  140. next();
  141.  
  142. });
  143.  
  144. export const environment = {
  145. production: false,
  146. API_REST_URL: 'https://yourDomainName', // can be http://localhost as well
  147. API_REST_PORT: 3000
  148. };
  149.  
  150. import { environment } from '/path/to/environment';
  151.  
  152. @Injectable()
  153. export class MyService{
  154. apiPort = environment['API_REST_PORT'];
  155. apiUrl = environment['API_REST_URL'];
  156.  
  157. constructor(private http: HttpClient) {
  158. }
  159.  
  160. apiCall(): Observable<any> {
  161. return this.http.get(`${this.apiUrl}:${this.apiPort}/endpoint/path`)
  162. .pipe(map(data => {
  163. return data;
  164. }));
  165. }
  166. }
  167.  
  168. "scripts": {
  169. ...
  170. "start-dev": "ng serve --configuration=dev",
  171. "build:dev": "ng build --configuration=dev",
  172. ...
  173. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement