Getting Started With Laravel: Authentication

S.O
staybusyng
Published in
3 min readMar 20, 2018

After installing Laravel one of the first things to do is setup your database connection, and do other configurations. In this post, we’ll be do all configurations, database setup and create our register, login, password reset methods. If you don’t have Laravel installed, read this: https://medium.com/staybusyng/getting-started-with-laravel-69d288229de8

1. CREATE DATABASE

Login to your database, and create a new database. If you are working with phpmyadmin or other similar tools, creating database should be easy.

  1. If you are working with terminal, do: create database [database name]

2. Update your .env file, in your terminal, do cp .env.example .env

This should create a configuration file for you.

3. You can open your app with your favourite IDE, I use VSCode:

Open app on IDE

You can see my .env file has some default values, i’m going to update with my local details:

Update my database details

Now your laravel app is connected with database.

2. SetUp Authentication

Setting up authentication (login, register, reset password) in Laravel is super easy, and can be done with a single command:

php artisan make:auth

Authentication scaffolding generated

Cool right?

3. Run Migrations

With Laravel you don’t have to create your tables directly on your server, according to the official Laravel website:

Laravel makes interacting with databases extremely simple across a variety of database backends using either raw SQL, the fluent query builder, and the Eloquent ORM. Currently, Laravel supports four databases:

MySQL

PostgreSQL

SQLite

SQL Server

The interaction is done with migrations, seeders and factories For now, we’ll only need migration. A typical migration file defines your table name, table columns, table column types etc for example, lets have a look at the user migration generated for us. Check staybusyLaravel/database/migrations That’s where you’ll find migrations.

user migration file

First part, defines the class name ( CreateUsersTables ) and then extends the Migration class.

The first function, up Give details of the table, column name and types.

The second function down defines what should happen should you decide to reverse a migration action, in this case, the created table “Users” should be dropped, which essentially means, the table should be deleted.

To run migration, do: php artisan migrate

PS: If you get this error: “SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))”

Error during first migration

No fuss, just go to app/providers/ open, AppServiceProvider.php in the boot() function add this: Schema::defaultStringLength(191), now import the class at the top, with: use Illuminate\Support\Facades\Schema

Fix migration error

Go into your database, drop it, create it again. Then try running migration again. Migration should run successfully now.

Migration done

4. Test Test

Serve your app, php artisan serve

You should now have, Login and Register Link at the top right corner

Login and Register Links

Give it a try, and see if it works.

Done for now.

--

--