0

I am working in a Angular 4 project. In this I need to get the IP address of a client when the session is started.

Before that how can I achieve the session concept in Angular 4? I have referred lot of pages but nothing has helped me so far. Please help me to solve these all. I am not asking write code to anyone at least I want the clear idea about this.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
Nikson
  • 900
  • 2
  • 21
  • 51

1 Answers1

-1

You can Integrate https://ipstack.com/, by creating a service when a user logs in.

Please follow:

Create Service for Get IP Address – - my-user-serv.service.ts

import { Injectable } from '@angular/core';

import { HttpClient, HttpErrorResponse  } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';

import 'rxjs/add/observable/merge';

import 'rxjs/add/operator/map';


@Injectable()

export class MyUserServService {

  constructor(private http: HttpClient) { }


  getIpAddress() {

      return this.http

            .get('http://ipstack.com')

            .map(response => response || {})

            .catch(this.handleError);
  }

  private handleError(error: HttpErrorResponse):

      Observable<any> {

        console.error('observable error: ', error);

        return Observable.throw(error);
    }
}


Used the Service in your user components 
–-user.component.ts

import { Component, OnInit } from '@angular/core';

import {MyUserServService} from '../my-user-serv.service';

@Component({

  selector: 'app-user',

  templateUrl: './user.component.html',

  styleUrls: ['./user.component.css']

})

export class UserComponent implements OnInit {


  constructor(private userService: MyUserServService) { }


  ngOnInit(): void {

    console.log("ip");

    this.userService.getIpAddress().subscribe(data => {

      console.log(data);

    });
  }
}

The Result Looks like –
{
"ip":"122.177.88.235",
"country_code":"IN",
"country_name":"India",
"region_code":"UP",
"region_name":"Uttar Pradesh",
"city":"Ghaziabad",
"zip_code":"201001",
"time_zone":"Asia/Kolkata",
"latitude":28.6667,
"longitude":77.4333,
"metro_code":0
}
Danish Arora
  • 330
  • 1
  • 6
  • Yes.. It is quite obvious that you should not be able to access, private/intranet IP. Only Public IP can be accessed. – Danish Arora Sep 08 '18 at 17:18
  • Thanks Danish.. Finally we took route of Java.. Java is able to give us IP address.. – Ziggler Sep 10 '18 at 22:15
  • @Ziggler Can you tell me how you solved this problem using Java from Angular? – M Fuat Mar 20 '19 at 09:45
  • Currently I am out of this project and company. Our Java developer provided us a service which I make a call and it gives me the IPAddress of the client who made the call. I use this IPAddress for all my subsequent calls. I am not A java developer and donot know how he coded it in Java. – Ziggler Mar 20 '19 at 15:58