Laravel 5 custom authentication

Laravel, Laravel 5, 2015-06-17 00:03:22 UTC

Laravel by default has its own authentication system. Just use it and you can start creating your application. But, sometimes this is not fit your need. Here I want to show you how to create custom authentication.

For make this simple, I start from basic Laravel 5 authentication system, edit it and fit into your need;

1. Migrate

The Laravel 5 default package included a users table migration and password reset table migration in "database/migration" folder. So, you need to execute migrate command to create these tables in your database.

Fit the credentials table into your need. Mine like below;

Schema::create('penggunas', function(Blueprint $table)
    $table->string('password', 60);

2. Create a user

I don't use seed for this. For simple, I use a trick like below;

Open up "routes.php", create a route like below and execute.

    $check = DB::table('penggunas')
                    'name' => 'Code Scr',
                    'email_pengguna' => '',
                    'keyword' => Hash::make('1234'),
                    'role' => 'super admin',
                    'verified' => 1
    if($check) {
        echo "success";
    else {
        echo "fail";

At least you need 2 users for testing.
Please create another user with verified = 0.

3. Configuring auth

Open "config/auth.php" file, edited line "table => 'users'" to your credential table;

return [
    'driver' => 'eloquent',
    'model' => 'App\User',
    'table' => 'penggunas',
    'password' => [
        'email' => 'emails.password',
        'table' => 'password_resets',
        'expire' => 60,

4. Model

Open up "app/User.php".

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    protected $table = 'penggunas'; 

    protected $fillable = ['name', 'email_pengguna', 'keyword'];
    protected $hidden = ['keyword', 'remember_token'];

5. Routes

Add another route

Route::get('/', 'WelcomeController@index');
Route::get('home', 'HomeController@index'); 
Route::get('custom_auth/login', 'CustomAuth@getLogin'); 
Route::post('custom_auth/login', 'CustomAuth@postLogin');

6. CustomAuth Controller

Create "CustomAuth" file in "app/Http/Controllers" folder and add these lines;

namespace App\Http\Controllers;

use Auth;
use Request;

class CustomAuth extends Controller {

    public function getLogin() {
        return view('custom_auth.login'); //or just use the default login page

    public function postLogin() {
        $email = Request::input('email_pengguna');
        $password = Request::input('keyword');
        if (Auth::attempt(['email_pengguna' => $email, 'keyword' => $password, 'verified' => 1]))
            //echo "success";
            return redirect('home');
        else {
            return "fail";

7. Login view

Create "resources/view/custom_auth/login.blade.php" file;

<form method="POST" action="/custom_auth/login">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="email" name="email_pengguna" value="{{ old('email_pengguna') }}">
    <input type="password" name="keyword">
    <button type="submit">Login</button>

8. Open "http://your_app/custom_auth/login", type an email and a password. If it match, the application redirect you to "http://your_app/home".