I have to implement login functionality in Laravel 5.2. I have successfully done so using the official Laravel documentation except that I do not know how to authenticate the user using different database table column names, namely st_username
and st_password
.
I have searched the Internet for clues but to no avail. I don't know which class I need to use (like, use Illuminate.......) for Auth. If any one knows the answer, please let me know.
Here is my code:
Login View
@extends('layouts.app')
@section('content')
<div class="contact-bg2">
<div class="container">
<div class="booking">
<h3>Login</h3>
<div class="col-md-4 booking-form" style="margin: 0 33%;">
<form method="post" action="{{ url('/login') }}">
{!! csrf_field() !!}
<h5>USERNAME</h5>
<input type="text" name="username" value="abcuser">
<h5>PASSWORD</h5>
<input type="password" name="password" value="abcpass">
<input type="submit" value="Login">
<input type="reset" value="Reset">
</form>
</div>
</div>
</div>
</div>
<div></div>
@endsection
AuthController
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
$this->username = 'st_username';
$this->password = 'st_password';
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
Route File
Route::get('/', function () {
return view('index');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController@index');
});
config/auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
];
I searched a lot how to customize Laravel 5.2 authorisation form and this is what is working for me 100%. Here is from bottom to top solution.
This solution is originally from here: https://laracasts.com/discuss/channels/laravel/replacing-the-laravel-authentication-with-a-custom-authentication
but i had to make couple changes to make it work.
My web app is for the DJs so my custom column names are with 'dj_', for example name is dj_name
config/auth.php
in config/app.php add your custom provider to the list ...
Create CustomAuthProvider.php inside folder app\Providers
Create CustomUserProvider.php also inside folder app\Providers
in App/Http/Controllers/Auth/AuthController.php change all 'name' to 'dj_name' and add your custom fields if you have them...you can also change 'email' to your email column name
In Illuminate\Foundation\Auth\User.php add
In App/User.php change 'name' to 'dj_name' and add your custom fields. For changing 'password' column to your custom column name add
Now backend is all done, so you only have to change layouts login.blade.php, register.blade.php, app.blade.php...here you only have to change 'name' to 'dj_name', email, or your custom fields... !!! password field NEEDS to stay named password !!!
Also, to make unique email validation change AuthController.php
Also, if you want to make custom created_at an updated_at fields change global variables in Illuminate\Database\Eloquent\Model.php
A custom Auth::attempt() doesn't work for me by changing the model in auth.php like this:
If I try to authenticate a Person via custom column names in Auth::attempt:
I get the same error:
But I can authenticate a Person like this:
But that's not the way it should be, I guess.
In the mentioned File (EloquentUserProvider.php), I see 'password' is hardcoded.
So there is actually no way to use a custom column name for passwords the easy way?
This is easy simply use the desired column names in Auth::attempt()/method like so:
Updated: If you also wish to change default authentication table which is
users
by default or change the model name or pathApp\User
by default, you can find these settings inconfig\auth.php