T
T
tester_toster2017-08-03 21:50:47
API
tester_toster, 2017-08-03 21:50:47

Laravel - how to authorize a user from under api in the web?

To work with applications via api, I installed Laravel passport
/config/auth.php:

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

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

I made a receipt of a token by password and email, attached a standard trait so that after authorization in the web it was possible to work with api requests.
There was a problem: there is App\Http\Controllers\Api\v1RegistrationController,
It has a guard - passport.
It contains the code:
$user = User::create([
             'name'          => $request->has('name') ? $request->name : '',
            'first_name'    => $request->has('first_name') ?? $request->first_name,
            'last_name'     => $request->has('last_name') ?? $request->first_name,
            'email'         => $request->email,
            'password'      => bcrypt($request->password),
        ]);
        Auth::login($user);

It is necessary that after the user is created from under the api, he is authorized in the web, since the web application is still working with the api, but this does not happen - Auth::guest() shows false, but after reloading the page, the authorization disappears.
It is necessary that the user from the browser registering via api can be authorized after the redirect to the main one.
How to implement this?
Tried: But after reloading the page, authorization disappears, although if you try to do this from under web controllers, everything works
Auth::guard('web')->login($user);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2017-08-03
@Sanasol

And why topsy-turvy from the "usual" scenario?
Usually they register, log in, and then you can issue tokens and anything to work with api.
In the API, why should it be logged in, it is not designed for this at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question