Skip to main content

Laravel 5.1 - Step by step instructions for setting up default authentication

Create a Database in MySQL

1. Login to MySQL
2. Run the command to create a database for your application

CREATE DATABASE database_name;

3. You'll probably also need to create a user to access the database

CREATE USER 'username'@'localhost' IDENTIFIED BY 'some_password';

4. Then you'll need to grant this user access to the database you created

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost'

Configure Laravel Database Connection

The DB configuration file is located at /config/database.php. You'll need to edit the following highlighted sections in this file with the values needed to connect to the database you've created from above.


Configure Environment

If you installed Laravel through Composer, in the root of the Laravel install there is a hidden .env file which is automatically renamed from .env.example. If you didn't user Composer, you'll need to rename this file yourself. In this file, you'll need to edit the following lines:

DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=username
DB_PASSWORD=password

Setup Database Tables for Authentication

There are already migrations created for you in the following folders

/database/migrations/2014_10_12_000000_create_users_table.php
/database/migrations/2014_10_12_100000_create_password_resets_table.php

You'll need to goto the root of your application and run

php artisan migrate

You may get the following error message:


  [PDOException]
  SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (usin
  g password: YES)

This may be due to the Environment settings from .env being cached. You'll need to flush the environment by running the following at the Linux prompt

php artisan config:clear


Implement Authentication


By default Laravel ships with a lot of stuff that can be used to setup authentication quickly.

1. The config file for Laravel Authentication is located at /config/auth.php
2. The user model located at /app/User.php
3. The authentication controller under /app/Http/Controllers/Auth

What doesn't come out of the box is the routing configuration and the views to support registration and authentication.

Setup Routes

Locate the routing config file at /app/Http/routes.php and add the following lines at the end of the file.

// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');


// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');


Setup Views

Create /resources/view/auth/login.blade.php with the following code:

<form method="POST" action="/auth/login">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>


Create /resources/view/auth/register.blade.php with the following code:

<form method="POST" action="/auth/register">
    {!! csrf_field() !!}

    <div class="col-md-6">
        Name
        <input type="text" name="name" value="{{ old('name') }}">
    </div>

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password">
    </div>

    <div class="col-md-6">
        Confirm Password
        <input type="password" name="password_confirmation">
    </div>

    <div>
        <button type="submit">Register</button>
    </div>
</form>


You should now be able register a user at the relative path /auth/register and also login at /auth/login

Enjoy!

 




 

Comments