Authentication with laravel 5 is more handy rather laravel 4. everything you need to login authentication is already configured for you. you get all forms like register, login, password reset inbuilt in laravel 5. so for user login you no need to give more time to config and write code. after install a new laravel 5 you will get already authentication files on below location :-
1. database/migrations :- Migrations files for user and password reset tables
2. config/auth.php :- This is authentication configuration file, which contains options for authentication services.
3. app/User.php :- Laravel includes an App/User model in your app directory. This model used with the default Eloquent authentication driver.
4. app/Http/Controllers :- In Laravel there are two authentication related controllers. The AuthController used for new user registration and logging in, and the PasswordController contains the logic to help existing users reset their forgotten passwords.
5. resources/views/auth :- all login authentication, registration, reset pasword views.
6. app/Services/Registrar.php :- This file handle user registration. validator method of the Registrar contains the validation rules for new users of the application, while the create method of the Registrar is responsible for creating new User records in your database. The Registrar is called by the AuthController via the methods contained in the AuthenticatesAndRegistersUsers trait.
7. app/Http/routes.php :- contanis default routes
so these all files makes you easy user login, register, password reset process in laravel 5. so you already have all stuff to authentication with laravel 5. you are free to customize manually user login authentication and registration process. To customize user register process use “app/Services/Registrar.php” file and change validation and insertion process according your need. i think there is no need to change user login and password reset process. for changes views of all forms simply go to “resources/views/auth” and make look of login, register etc. forms as you wish. let’s see how to configure and connection for user login authentication process in laravel 5.
User login authentication process in laravel 5 :-
To configure user authentication process we will need below steps
1. Configure database connection
2. Create database tables using migrations
3. User register and check default User login process
4. Manual Authentication
Let’s explore all steps one by one and make all process easy going.
1. Configure database connection :- Go to “app/config/database.php” make a connection. configure your host, user, password to appropriate db driver below is mysql driver example.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'laravelu'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
2. Create database tables using migrations :- Now we need to create users and password reset tables to use default authentication process. we are creating database tables using migrations.
Migrations :- migrations provide a way to modify the database schema and stay up to date on the current schema state. basically migrations are a way there we can manipulate database using code. we don’t need to use phpmyadmin to manipulate database we can diretly handle databse manipulation using our code. To create a migration, we need to use commands on the Artisan CLI.
Let’s create a users table using migrations for this go to your application root folder(where you have located “app” directory) and run below command.
There are two way to create tables :-
1. Using Inbulit Migrations :-
Note :- if you look at database/migrations there will be already two migrations files
1. 2014_10_12_000000_create_users_table.php
2. 2014_10_12_100000_create_password_resets_table.php
so migrations also ready for create these two tables you need to just simply run migration command.
php artisan migrate
Example :-
root@127:/var/www/html/laravel# php artisan migrate
(i have "app" directory in my laravel directory so my root will be "/var/www/html/laravel/")
2. Create Tables manually with new migrations :- In this section we need to create tables manually using migrations and CLI.
create user table :-
php artisan make:migration create_users_table --create=users
create password reset table :-
php artisan make:migration create_password_resets_table --create=password_resets
Example :-
root@127:/var/www/html/laravel# php artisan make:migration create_users_table --create=users
(i have "app" directory in my laravel directory so my root will be "/var/www/html/laravel/")
Above command will creates new files in “database/migrations/date+create_users_table.php” and “database/migrations/date+create_password_reset_table.php” will look like below. it’s a core migration file which will create a table with an id field and the timestamps fields (created_at and updated_at).
date+create_users_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('users');
}
}
and date+create_password_reset_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('password_resets', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('password_resets');
}
}
Now we need to add some more fields to table using Schema Builder so new files with added code looks like :-
date+create_users_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('users');
}
}
and date+create_password_reset_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('password_resets', function(Blueprint $table)
{
$table->string('email')->index();
$table->string('token')->index();
$table->timestamp('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('password_resets');
}
}
Now the migration file will create a table with given field for this we need to run this file using command line again so for create a table simple run this command:-
php artisan migrate
Now we have all set with databse configuration and with databse tables. Now we need to register a user. so this process also set in fresh laravel 5.
3. User register and check default User login process :- For register a user simply run your site default url like:-
http://localhost/mylara5/auth/login
http://localhost/mylara5/auth/register
http://localhost/mylara5/home
Now register a new user and you you logged in also check all links working. Now we will discuss about some customization of authentication and some important functions.
4. Manual Authentication :- If you don’t want to use default authentication process, you will need to manage the authentication of your users using the Laravel authentication classes directly. let’s check out the attempt method:
<?php namespace App\Http\Controllers;
use Auth;
use Illuminate\Routing\Controller;
class AuthController extends Controller {
/**
* Handle an authentication attempt.
*
* @return Response
*/
public function authenticate()
{
if (Auth::attempt(['email' => $email, 'password' => $password]))
{
return redirect()->intended('dashboard');
}
}
}
The attempt method accepts an array of key / value pairs as its first argument. The password value will be hashed. The other values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the hashed password value passed to the method via the array. If the two hashed passwords match, a new authenticated session will be started for the user.
The attempt method will return true if authentication was successful. Otherwise, false will be returned.
Getting logged in user data :- after logged in user you can get users data like
Access user information :-
$email = Auth::user()->email;
$id = Auth::id();
Check if a user is authenticated:-
if (Auth::check()) {
// The user is logged in...
}