Osijek,
Croatia
+385 95 578 4732
MON - FRI, 4PM - 10PM
ivan@javorovic.com
REPLY IN 24 HOURS
Udemy Course
Ivan Javorović profile image

Ivan Javorović

Full Stack Developer

  • BORN : October 3, 1995
  • NICKNAME : Jawe
  • FAVORITE BAND : The Beatles

Add Auth to Laravel 5.6 (2018)

1,895 views

Routes, Laravel, Laravel 5.6, Auth, Middleware, User, Validator, Authentication

Since Laravel is so AWESOME we can add Auth really easily (literally 2 minutes).


This is all done through the console.


You can do this with an existing project and with a fresh one!


After you made a project you need to run this command (Win + R and enter cmd) inside of your project folder:

php artisan make:auth

All of the routes, controllers migrations and seeds are now set in your project.


Now lets run them!

php artisan migrate:refresh --seed


Keep in mind this will DROP all of your data in your currently selected DB and seed with the data you have set in your project.

In your routes folder you will have a new function added which will add all of the default Laravel routes for Login, Register and Password Reset.

Auth::routes();


USING AUTH MIDDLEWARE


If you want users to only have access to some pages after they are logged in. You can add a route group with the middleware auth to redirect to login or which ever page you speficied.


routes/web.php

Route::group(['middleware' => 'auth'], function(){ 
    Route::any('search/{data}', 'SearchController@search')->name('search');
});


AUTHENTICATED


If you wish to add a function after the current user has logged in successfully then you can add your own code here:


vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
//post successful login code
}

Here you can for example add code which will populate a logins table within you DB to follow user logins.


ADDING MORE REGISTRATION INFO


If you want to add more fields to your registration form you will need to edit your migration file, RegisterController (create and validator functions) as well as the User Model $fillable variable.


In this example i added the role_id column to the users table so you can see what I’m talking about.


database/migrations/create_users_table.php

Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});

app/Http/Controllers/Auth/RegisterController.php


validator

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'key' => 'required|string|max:255|unique:keys',
'password' => 'required|string|min:6|confirmed',
]);
}


create

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'role_id' => 2,
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}


app/User.php

protected $fillable = [
'role_id', 'name', 'email', 'password',
];

By: Ivan Javorović