Skip to content

Custom login and registration with auth laravel 5.7

I’m trying to create the first project with Laravel 5.7.

In this project I have three different areas with three different login (ex. Login for users, login for admin, login for technicians).

Now, I would like to customize the first login (url, html, input, class, db table, db column ecc…) but I don’t know how to do.

The URL of first login is website.com/public/users

My Table is:

CREATE TABLE `gf_users` (
  `gf_id` int(11) NOT NULL,
  `gf_name` text NOT NULL,
  `gf_email` varchar(200) NOT NULL,
  `gf_password` varchar(300) NOT NULL,
) 

I have searched a lot and with the information that I have found, I have tried to do this:

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' => AppGFUsers::class
        ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

web.php

Route::get('users', 'AuthLoginController@showLoginFormUsers')->name('login');
Route::post('users', 'AuthLoginController@login');

LoginController.php

public function showLoginFormUsers(){
   return view('users.login');
}

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

GFUsers.php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;

class GFUsers extends Authenticatable
{
    use Notifiable;

    protected $table = 'gf_users';
    protected $primaryKey = 'gf_id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'gf_email', 'gf_password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'gf_password'
    ];


    public function getAuthPassword(){
        return $this->gf_password;
    }
}

login.blade.php

<form method="POST" action="{{ asset('/users') }}">
@csrf
  <div class="form-group">
    <input type="email" class="form-control" name="gf_email" id="gf_email" placeholder="Email" required>
  </div>
  <div class="form-group">
    <input type="password" class="form-control" name="gf_password" id="gf_password" placeholder="Password" required>
  </div>
  <button type="submit" class="btn btn-primary">LOGIN</button>
</form>

If I compile the form and click on login button, this refresh the page but I don’t understand if it works or not.

1. Is the procedure correct?
2. Are there some errors?
3. Have I forget something?
4. What can I do for registration?

Advertisement

Answer

After long research I have find my solution. I hope that this can help someone else.
I have customized: id, username, password and remember token in the table and the files User.php, LoginController.php.

==Table==:

CREATE TABLE `gf_users` (
  `gf_id` int(11) NOT NULL,
  `gf_name` text NOT NULL,
  `gf_email` varchar(200) NOT NULL,
  `gf_password` varchar(300) NOT NULL,
  `gf_remember_token` varchar(300) NOT NULL
) 

web.php:

// Authentication Routes
Route::get('users', 'AuthNewLoginController@showLoginFormUsers')->name('login');
Route::post('users', 'AuthNewLoginController@checklogin');
Route::post('logout', 'AuthNewLoginController@logout')->name('logout');
// Registration Routes
Route::get('users/registration', 'AuthRegisterController@showRegistrationFormUsers')->name('register');
Route::post('users/registration', 'AuthRegisterController@register');

NewLoginController.php:

<?php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateSupportFacadesAuth;
use IlluminateHttpRequest;

class LoginController extends Controller
{

    use AuthenticatesUsers;
    protected $redirectTo = '/users/details';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    
    
    public function showLoginFormUsers(){
        return view('users.login');
    }
    
    public function username(){
        return 'gf_email';
    }
    
    function checklogin(Request $request){
        
        $this->validate($request, [
            'input-email' => 'required', 
            'input-password' => 'required',
        ]);
        
        $user_data = array(
            'gf_email'  => $request->get('input-email'),
            'password' => $request->get('input-password')
        );
        
        if(!Auth::attempt($user_data)){
            return redirect('users');
        }
        
        if ( Auth::check() ) {
            return redirect('users/details');
        }
    }
    
    function logout(){
        Auth::logout();
        return redirect('users');
    }

}

NewUser.php:

<?php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    protected $table = 'gf_users';
    protected $primaryKey = 'gf_id';
    public $timestamps = false; 
    
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'gf_email', 'gf_password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'gf_password', 'gf_remember_token',
    ];
    
    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword(){
        return $this->gf_password;
    }
    
    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->gf_remember_token;
    }
    
    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->gf_remember_token = $value;
    }
    
    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'gf_remember_token';
    }
    
    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->gf_email;
    }
}

login.blade.php

<form method="POST" action="{{ asset('/users') }}">
@csrf
  <div class="form-group">
    <input type="email" class="form-control" name="input-email" id="input-email" placeholder="Email" required>
  </div>
  <div class="form-group">
    <input type="password" class="form-control" name="input-password" id="input-password" placeholder="Password" required>
  </div>
  <button type="submit" class="btn btn-primary">LOGIN</button>
</form>

config/auth.php:

<?php

return [


    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],


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

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

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppNewUser::class
        ],

        //'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

After this changes I did the command php artisan config:cache and php artisan event:clear.

User contributions licensed under: CC BY-SA
10 People found this is helpful