Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { HttpClient } from '@angular/common/http';
- import { map } from 'rxjs/operators';
- @Injectable()
- export class UserService {
- constructor(
- private _httpClient: HttpClient
- ) { }
- public auth(user) {
- return this._httpClient.post('users/login', user).pipe(map((resp: any) => resp.json()));
- }
- }
- import { Component, OnInit } from '@angular/core';
- import { Router } from '@angular/router';
- import { UserService } from '../../services/user.service';
- @Component({
- selector: 'app-side-menu',
- templateUrl: './side-menu.component.html',
- styleUrls: ['./side-menu.component.scss']
- })
- export class SideMenuComponent implements OnInit {
- public positionsList: Array<any> = [];
- public selectedOption: string;
- public feedbackMessage: string;
- public email: string;
- public password: string;
- public formNotValid: boolean;
- constructor(
- private _userService: UserService,
- private _router: Router
- ) {
- }
- ngOnInit() {
- }
- public sendLoginForm() {
- if(!this.email || !this.password){
- this.formNotValid = true;
- this.feedbackMessage = "Both fields are required to login!";
- return false;
- }
- const user = {
- email: this.email,
- password: this.password
- }
- this._userService.auth(user).subscribe(resp => {
- if(!resp.success){
- this.feedbackMessage = resp.message;
- return false;
- }
- });
- }
- public getSelectedOption(option) {
- this.selectedOption = "as " + option;
- }
- }
- this._httpClient.post(window.location.protocol + '//' + window.location.hostname + ':3000/' + 'users/login', user)
- "scripts": {
- .....
- "start": "ng serve --proxy-config proxy.config.json",
- .....
- }
- {
- "/api/*": {
- "target": "http://localhost:3000",
- "secure": false
- },
- "/__/*": {
- "target": "http://localhost:3100",
- "secure": false
- }
- }
- import { Injectable } from '@angular/core';
- import { HttpClient } from '@angular/common/http';
- import { map } from 'rxjs/operators';
- @Injectable()
- export class UserService {
- url='http://localhost:3000/'
- constructor(
- private _httpClient: HttpClient
- ) { }
- public auth(user) {
- return this._httpClient.post(this.url+'users/login', user).pipe(map((resp: any) => resp.json()));
- }
- }
- import { Injectable } from '@angular/core';
- import { HttpClient } from '@angular/common/http';
- import { map } from 'rxjs/operators';
- @Injectable()
- export class UserService {
- constructor(
- private _httpClient: HttpClient
- ) { }
- public auth(user) {
- return this._httpClient.post('http://localhost:3000/users/login', user).pipe(map((resp: any) => resp.json()));
- }
- }
- app.use((req , res , next) => {
- res.header("Access-Control-Allow-Origin" , "*");
- res.header(
- "Access-Control-Allow-Headers" ,
- "Origin, X-Requested-With, Content-Type, Accept, Authorization"
- );
- next();
- });
- export const environment = {
- production: false,
- API_REST_URL: 'https://yourDomainName', // can be http://localhost as well
- API_REST_PORT: 3000
- };
- import { environment } from '/path/to/environment';
- @Injectable()
- export class MyService{
- apiPort = environment['API_REST_PORT'];
- apiUrl = environment['API_REST_URL'];
- constructor(private http: HttpClient) {
- }
- apiCall(): Observable<any> {
- return this.http.get(`${this.apiUrl}:${this.apiPort}/endpoint/path`)
- .pipe(map(data => {
- return data;
- }));
- }
- }
- "scripts": {
- ...
- "start-dev": "ng serve --configuration=dev",
- "build:dev": "ng build --configuration=dev",
- ...
- },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement