Authentication with another laravel table 5.4

3

I have a project in Laravel 5.4 and I want to use the authentication system that it brings, but with the information that is found in another table that has already been created through the migrations.

The truth is that I have surfed all over the internet and I have seen the documentation, but on both sides I have not managed to get answers that allow me to do what I need.

To the maximum that I have arrived it is to realize the authentication but without redirection, that is to say, the data are sent and then I arrive at the same view of login.

I leave the code of what I wear.

LoginController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller {

use AuthenticatesUsers;


public function username() {
    return 'usuario';
}

public function __construct() {

}

public function showLoginForm() {
    return view('auth.login');
}

public function login(Request $request) {
    // Validate the form data
    $this->validate($request, [
        'password' => 'required|min:2'
    ]);
    // Attempt to log the user in
    if (Auth::guard()->attempt(['usuario' => $request->usuario, 'password' => $request->password], $request->remember)) {
        // if successful, then redirect to their intended location
        return redirect()->intended(route('admin.dashboard'));
    }
    // if unsuccessful, then redirect back to the login with the form data
    return redirect()->back()->withInput($request->only('usuario', 'remember'));
}

}

The rest is practically by default. It is worth mentioning that it generates the authentication structure with:

php artisan make:auth

Answer Shaz

Thanks for answering. I have done what you indicate, I add the code of how I have both files

app / access (model)     

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Acceso extends Authenticatable
{
use Notifiable;
protected $guard = 'users';
protected $table = 'acceso';
protected $fillable = ['id', 'rol_id', 'personal_id', 'usuario', 'contraseña', 'activo'];
protected $primaryKey = 'id';

public function rol(){
    return $this->belongsTo('App\Rol');
}

public function personal(){
    return $this->belongsTo('App\Personal');
}
}

config / auth.php

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Acceso::class,
    ],
    
asked by Israel jara 07.06.2017 в 23:38
source

3 answers

1

Normally you configure the table in the model that is specified in your config / auth.php file:

...
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => app\Models\User::class,
    ],
    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    //
],
...

In the model (in this case in app / Models / User.php) you define the table you want to use:

...
class User extends Authenticatable
{

    protected $table = 'users';

    // ...
}

More information in the documentation: link

    
answered by 07.06.2017 в 23:46
0

Add the table key in your config / auth.php file within the array providers = > users.

Add the properties and configurations you need to the model responsible for carrying out the authentication.

Remember to modify the App \ Http \ Controllers \ Auth \ RegisterController driver, more specifically the validator and create methods.

Everything must work perfectly.

Greetings.

    
answered by 12.06.2017 в 16:40
0

I know exactly why this happens, go to home controlller there must be a constructor with a middleware-> ('auth') change it to auth.basic, one of the reasons why it does not work is because inside that auth class there are directives for the methods of guard, I guess that what you did was to modify the class that came by default and put your code if you see this code that brings by default a function called guard which you deleted and therefore the auth class does not find the guard

    
answered by 30.08.2017 в 21:00