2

I want when i click angular front end button redirect to Laravel Backend View.I am new to angular.This is what i try to do.Please help me.

frontend.component.html

<button type="button" (click)="userDetailsEdit()">Edit Details</button>

frontend.component.ts

   userDetailsEdit() {
    alert('Its Working');
    this.authService.goToBreweryBarBackend();
}

auth.service.ts

 goToBreweryBarBackend() {
              const accessToken = JSON.parse(localStorage.getItem('access_token'));
              headers.append('Content-Type', 'application/json');
              this.http.get(this.url.LaravelURL + 'api/goToBreweryBarBackend', { headers: new Headers({ 'Authorization': 'Bearer ' + accessToken }) });

api.php

Route::get('/goToBreweryBarBackend', ['uses' => 'Homecontroller@index']);

HomeController.php

public function index()
{
    return view('home');
}
RuwanthaSameera
  • 113
  • 1
  • 12
  • Is there any error in console? – Yash Rami Mar 19 '19 at 07:08
  • no.there are no errors – RuwanthaSameera Mar 19 '19 at 07:25
  • What do you mean by "redirect"? In your code, you are trying to return a rendered HTML to the API request. Is this what you are trying to do? Or do you want a complete redirection to the Laravel side of the application? – Harun Yilmaz Mar 19 '19 at 08:12
  • @HarunYılmaz i already developed backend using Laravel 5.5. i want to go From angular (Frontend) to Laravel backend. – RuwanthaSameera Mar 19 '19 at 08:41
  • I mean, do you want only to get data (JSON, XML etc) from backend? Or do you want the users to redirect completely from Angular to Laravel? – Harun Yilmaz Mar 19 '19 at 08:45
  • @HarunYılmaz i can get data from backend.But i dont know how to redirect completely from angular to laravel. – RuwanthaSameera Mar 19 '19 at 08:48
  • 1
    I think this will help: https://stackoverflow.com/questions/34338440/how-to-redirect-to-an-external-url-in-angular2 – Harun Yilmaz Mar 19 '19 at 08:49
  • @HarunYılmaz ill try using this example.thank you. – RuwanthaSameera Mar 19 '19 at 08:57
  • @HarunYılmaz its work. but there is a problem.I developed user login in frontend. If frontend login i logged in one user and i try to goto backend, laravel doesnt know this user already logged in frontend or not.what should i do? – RuwanthaSameera Mar 19 '19 at 09:03
  • 1
    Laravel uses "session" guard as default for web. Maybe you can implement a middleware that gets the access token from URL and manually authenticate the user. So when you redirect your user, you can pass access token with the first redirection. – Harun Yilmaz Mar 19 '19 at 09:11
  • @HarunYılmaz Ill try as u said. Thank you so much your guidence. – RuwanthaSameera Mar 19 '19 at 09:20

1 Answers1

1
userDetailsEdit() {
    window.location.href = 'http://localhost:8000/intro'; 
}
RuwanthaSameera
  • 113
  • 1
  • 12