Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephan-v's avatar

Laravel basic authentication

I've followed the lessons on laravel 5 fundamentals and during the authentication one Jeffrey talks about deleting the included auth and write the auth from scratch by yourself.

I've tried it but when I did I almost end up with what is included in the first place with a default laravel install because of the fact that I am using an abstract class which I need to abide by.

Can anybody give me an extremely basic example of authentication with laravel 5?

0 likes
1 reply
bobbybouwmann's avatar

So it's really easy to do. First you need to create the users table

php artisan migrate:make create_users_table --create=users

Then update the migration file to this:

class CreateUsersTable extends Migration {
    
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
            {
            $table->increments('id');
                $table->string('name');
                $table->string('username');
                $table->string('email');
                $table->string('password');
                $table->string('remember_token')->nullable();
                $table->timestamps();
            });
    }

    public function down()
    {
            Schema::drop('users');
    }
}

Run migrate to get the table in the database (This only works if you did setup the database in the config/database.php file)

php artisan migrate

Now we need create some routes to handle the actions for the user. Open up routes.php and add these routes

Route::get('home', function () {
    return view('welcome');
});

Route::get('register', 'AccountController@register');
Route::post('register', 'AccountController@register');

Route::get('login', 'AccountController@login');
Route::post('login', 'AccountController@login');

Route::get('logout', 'AccountController@logout);

Now we have our routes defined we need a controller to do this, so once again php artisan is your friend

php artisan make:controller AccountController

Add these methods to the account controller

public function getRegister()
{
    return view('auth.register');
}

public function postRegister()
{
    $this-validate(Input::all(), [
        'name' => 'required',
        'email' => 'required|email',
        'password' => 'required|min:6'
    ]); // If $this->validate fails Laravel auto redirects you back to the previous route, so no worries here!

    $user = User::create(Input::all());

    Auth::login($user);

    return redirect('/');
}

public function getLogin()
{
    return view('auth.login');
}

public function postLogin() 
{
    $this-validate(Input::all(), [
        'email' => 'required|email',
        'password' => 'required|min:6'
    ]);

    Auth::attempt([
        'email' => Input::get('email'),
        'password' => Input::get('password')
    ]);

    return redirect('/'); // Redirect back to the home page
}

public function logout()
{
    Auth::loggout();
    return redirect('login');
}

You can now use the Auth::user() to get information from the logged in user. I leave the views for you, but that can't be hard because you can use the views which ships with Laravel, just update the url where the form posts to.

Hope this helps you out :D

2 likes

Please or to participate in this conversation.