Search code examples
laravellaravel-5laravel-5.3laravel-migrationslaravel-authorization

Laravel 5.3 Passport migrations & modifying the default users table


I've just upgraded from laravel 5.2.45 to 5.3.15 and, like many others out there, have some questions about how to implement passport and the default tables that come with the command:

php artisan make:auth

Situation 1:

I have set up my configuration to have 2 separate database connections. One is named 'core' (default connection) and the other is named 'auth'.

Question 1:

When I perform all the steps properly and migrate, all the tables populate in 1 database. I would like to get passport migrations into the auth database away from everything else. How can I achieve this?

Situation 2:

(Assume all config files have been set up correctly)

I managed to do this in the previous version of laravel (5.2) where I changed the users table to acct_user and got everything to work perfectly. Currently when I make the same necessary changes, and attempt to register a user, the user does not save in the database.

I've pulled out the code located in Illuminate\Foundation\Auth\RegistersUsers and overrode the trait's method inside of the RegisterController class located in App\Http\Controllers\Auth.

Code:

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    //return redirect($this->redirectPath());
    //return $user;
}

The above code, from what I can gather, is directly responsible for handling registration requests and creating the user in the database. However, it keeps getting hung up inside of the event helper and keeps redirecting me and never creates the user. I've tried to comment out the return redirects throughout the class as well and apparently the redirect happens within the event helper itself. Bare in mind, for this I'm using Postman to make the request.

Question 2:

Am I doing something wrong here? How do I stop it from redirecting me and instead get it to simply return the user object?

Situation 3:

(Attempting to simulate a self-consuming application)

When I make a login or Register request through Postman with passport involved I'm expecting to get back a laravel_token cookie with the JWT embedded inside it.

Question 3:

Through Postman, how would I get this in the return?? or where would I look because currently I can't find it anywhere. Maybe I'm doing something wrong here too.

Any assistance is greatly appreciated.


Solution

  • I was running into this issue attempting to do something similar. Since the date of this question there has since been a few new revisions to the laravel/passport vendor code that address some internal issues that could be the result of what you're getting in some of your questions.

    However, to answer you questions directly:

    Question 1:

    When I perform all the steps properly and migrate, all the tables populate in 1 database. I would like to get passport migrations into the auth database away from everything else. How can I achieve this?

    Answer 1:

    This question was asked in a similar way on the laravel/passport github and the response was that it would be an additional feature they would have to add in. Currently, there is no clean/proper way to separate the passport migrations from everything else unless since they're embedded into the passport framework.

    Question 2:

    Am I doing something wrong here? How do I stop it from redirecting me and instead get it to simply return the user object?

    Answer 2:

    Check the methods within your App\Http\Controllers\Auth\ directory and in your App\Http\Middleware\RedirectIfAuthenticated.php file and comment out any redirect calls. This will allow you to then perform return $variable; in any spot that originally had a redirect-> call.

    Question 3:

    Through Postman, how would I get this in the return?? or where would I look because currently I can't find it anywhere. Maybe I'm doing something wrong here too.

    Answer 3:

    By going through Answer 2 you'll be able to assign your returned values from a DB query to some variable and then return that specific variable.

    Hope this helps.