Trouble understanding how to create a redirect request for a Laravel Api


#1

Hello Everyone,

I am trying to make a redirect request to my backend application’s /oauth/authorize, so I can get a token, and save it… I am having trouble understand how to do this in Angular2, and any suggestions would be much appreciated.

Here is what I have so far in my user.service.ts:

login(user: User) {
    let headers = new Headers();
    headers.append("Content-Type", "application/json");
    headers.append("Access-Control-Allow-Origin", "http://orca.dev");
    headers.append("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

    return this.http.post("http://orca.dev/oauth/token",
      JSON.stringify({
        username: user.email,
        password: user.password,
        grant_type: "password",
        client_secret: "some-secret",
        client_id: 6,
        code: 1
      }),
      { headers: headers }
    )
    .map(response => response.json())
    .do(data => {
      console.log("Data Access Token" + data.Result.access_token);
      Config.token = data.Result.access_token;
    })
    .catch(this.handleErrors);
  }
  
  handleErrors(error: Response) {
    console.log("Error Getting Data Access Token");
    console.dir(error.json());
    return Observable.throw(error);
  }
}

This is how I think it is supposed to work but from a php perspective:
I need to make an http redirect request from NativeScript like this one here in php:

   $query = http_build_query([
        'client_id' => 6,
        'redirect_uri' => 'http://consumer.dev/callback',
        'response_type' => 'code',
        'scope' => 'conference'
    ]);
    return redirect('http://orca.dev/oauth/authorize?' . $query);

After approving on server the user is forwarded back to the callback where we make a post request to get the access token like below in php:

$http = new GuzzleHttp\Client;
    $response = $http->post('http://orca.dev/oauth/token', [
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => 6, // from admin panel above
            'client_secret' => 'some-secret', 
            'redirect_uri' => 'http://consumer.dev/callback',
            'code' => $request->code // Get code from the callback
        ]
    ]);

$access_token = json_decode((string) $response->getBody(), true)['access_token'];
return $access_token;

About:
Backend Api => Laravel 5.4 with Passport https://laravel.com/docs/5.4/passport
Nativescript Angular2
tns version 2.5.2