esbenp / larapi

An API-friendly fork of Laravel. Authentication, error handling, resource filtering, sorting, pagination and much more included
http://esbenp.github.io/2016/04/11/modern-rest-api-laravel-part-0/
MIT License
406 stars 114 forks source link

Get user in LoginProxy #39

Open sreinoso opened 6 years ago

sreinoso commented 6 years ago

Hi, I'm interested on get the user to return to angular2 in LoginProxy@proxy, but I did not find which class or object returns this.

I need something like this:

[...]
public function proxy($grantType, array $data = [])
    {
        $data = array_merge($data, [
            'client_id'     => env('PASSWORD_CLIENT_ID'),
            'client_secret' => env('PASSWORD_CLIENT_SECRET'),
            'grant_type'    => $grantType
        ]);

        $response = $this->apiConsumer->post('/oauth/token', $data);
        // dd($response, $data, self::REFRESH_TOKEN);
        if (!$response->isSuccessful()) {
            throw new InvalidCredentialsException();
        }

        $data = json_decode($response->getContent());

        // Create a refresh token cookie
        $this->cookie->queue(
            self::REFRESH_TOKEN,
            $data->refresh_token,
            864000, // 10 days
            null,
            null,
            false,
            true // HttpOnly
        );

        return [
            'user' => /* User with this token or refresh token */,
            'access_token' => $data->access_token,
            'expires_in' => $data->expires_in
        ];
    }

[...]

I thought something like $this->auth->user() but it seems it's not defined.

What am I doing wrong?

Thanks!

esien525 commented 6 years ago

hi @sreinoso , did you able to solve this?

esien525 commented 6 years ago

I found the solution, just simply use $request->user('api'); in any Controller function.

sreinoso commented 6 years ago

No, I did not found the solution, I will try it!

fernandocoronatomf commented 5 years ago

@esien525 Is it possible to use multiple tables against laravel passport? I have 2 models that can be logged in, and none of them are users... is it possible or do you recommend me another approach?

esien525 commented 5 years ago

@fernandocoronatomf, I forget how i do it already. Haha

But i think it is possible as it works like laravel guard.

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],

        'exampleapi' => [
            'driver' => 'passport',
            'provider' => 'someusers',
        ],
    ],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => Api\Users\Models\User::class,
        ],

         'someusers' => [
            'driver' => 'eloquent',
            'model' => Api\Users\Models\SomeUser::class,
         ],
    ],

the code is not tested, but i hope it helps

fernandocoronatomf commented 5 years ago

Thanks for answering but perhaps I did not make myself clear. To use another Model is fine, the problem is when the models aren't linked to a user table.

i had to extend some passport classes to make it work and then switch the guard in a middleware + overwrite some other methods from passport classes.