Create Custom Login in Laravel 5

by Sam  


This tutorial will help you create a custom login using Laravel 5. Although laravel has provided an authentication automatically, we’ll just try to create our own since we created a custom registration.

Important : You need to create custom registration for you to have login credentials. If not, please check our previous Registration Tutorials.


.. Continuation of Laravel Registration

Let’s get started


1. Add Login, Logout and Dashboard in our Routes

After the user has successfully logged in, we’ll redirect the page to dashboard page, that’s why we need to add dashboard in our route also.

Go to routes>>web.php and add the the following:


Route::get('/login', 'AuthController@loginForm')->name('login');
Route::post('/login', 'AuthController@login');

Route::get('/logout', 'AuthController@logout');

Route::get('/dashboard', 'HomeController@dashboard')->middleware('auth');

2. Add Controller methods

On our registration tutorials, we added AuthController already. Now, what we need to do is just to simply add the following into our AuthController class:


/* @GET
*/
public function loginForm()
{
return view('custom_auth.login');
}

/* @POST
*/
public function login(Request $request){
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if (\Auth::attempt([
'email' => $request->email,
'password' => $request->password])
){
return redirect('/dashboard');
}
return redirect('/login')->with('error', 'Invalid Email address or Password');
}
/* GET
*/
public function logout(Request $request)
{
if(\Auth::check())
{
\Auth::logout();
$request->session()->invalidate();
}
return redirect('/login');
}

The code above will handle Login and Logout request from the user. After the user has successfully logged in, it will be redirected to the dashboard.

3. Create Login in Views

Go to resources>>views>>custom_auth and create login.blade.php file. Then, copy the code below.


@extends('layouts.customapp')
@section('title', 'Login Form')
@section('content')
<div class="container">
@if ( session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
<div class="col-md-4 col-md-offset-4">
<h2>Login Here</h2><hr>
<form class="form-horizontal" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }}
<label for="exampleInputEmail1">Email address</label>
<input class="form-control" value="{{ old('email') }}" required autofocus name="email" placeholder="Email Address">
<br>
<label for="exampleInputPassword1">Password</label>
<input class="form-control" id="exampleInputPassword1" type="password" placeholder="Password" name="password">
<br><br>
<button type="submit" class="btn btn-primary btn-block">
Login
</button>
</form>
</div>
</div>
@endsection

4. Inside HomeController

In our web.php, we pointed the HomeController as our Dashboard controller. Now, Go to app>>Http>>Controllers>>HomeController.php and copy the code below:


public function dashboard()
{
return view('dashboard');
}

5. Create Dashboard

Finally, we’ll add a dashboard blade inside our views. Now, go to resourcess>>views , create dashboard.blade.php file and copy the code below.


@extends('layouts.customapp')
@section('content')
<div class="container">
<h1>Welcome to Dashboard</h1>
<p><a href="{{ url('/logout') }}">Logout</a></p>
</div>
@endsection

And, that’s all.

Now go open your browser and visit http://localhost:8000/login. If you haven’t entered data, you need to be registered first: http://localhost:8000/register

I hope you’ll be able to register and login now.



Leave a Reply

Your email address will not be published. Required fields are marked *


  1. Thank you for sharing with us, I believe this website truly stands out :D.

    1. Sam says:

      Thank you very much

  2. Hiya very cool website!! Guy .. Beautiful .. Superb .. I will bookmark your website and take the feeds additionally?I’m satisfied to search out a lot of useful information here in the submit, we need develop more techniques on this regard, thanks for sharing.

    1. Sam says:

      Thank you so much Marilou. Keep visiting, we’re posting more tutorials.

  3. Appreciate this post. Let me try it out.

  4. I think this is among the most significant information for me. And i’m glad reading your article. But should remark on few general things, The site style is perfect, the articles is really nice : D. Good job, cheers

  5. Moritz says:

    Auth::attempt([

    ’email’ => $request->email,

    ‘password’ => $request->password])

    )

    always returns false

    How do I also set a custom user table eg staff?

Need of a Website?

Maybe you're planning to build a landing page for your website. You might also have a plan to create a web application. I can offer you with an affordable but with good quality services. Don't hesitate to submit your specifications by clicking the button below.

Get Quote