Custom Authentication (SignIn / SignUp) in Laravel
Hello everyone, welcome to justlaravel.com. As routing and pagination are very simple in laravel, authentication is not much different, it is very simple too, everything is configured out of the box.
Working Demo Project on Github
By default, laravel comes with 2 authentication controllers: Auth Controller and Password Controller.
Auth Controller, handles new registrations and authentication, Password Controller, manages forgotten passwords.
Default routes for authentication are,
1 2 3 4 5 6 7 8 | // 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'); |
So when we go to localhost:8000/auth/ login, login form appears, of course, we have placed our view files in resources directory, in previous versions of laravel, it shipped with these view files too.
We are not going to use these, as we completely customize our authentication.We ‘ll use modal for SignIn/SignUp as modals are very popular these days and very fancy too.
Firstly,let’ss create a modal, we are using w3css here,
In resources/views location, create a new file named customAuthentication.blade.php, as we said we use w3css modal along with w3css tabs for login and register.
1 2 | <li class="w3-right"><a class="w3-green" href="#" id="auth" onclick="document.getElementById('authentication').style.display='block'">SignIn/SignUp</a></li> |
above line of code calls the modal when SignIn/SignUp is clicked,
and the code for modal is,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | <div id="authentication" class="w3-modal"> <span onclick="document.getElementById('authentication').style.display='none'" class="w3-closebtn w3-grey w3-hover-red w3-container w3-padding-16 w3-display-topright">X</span> <div class="w3-modal-content w3-card-8 w3-animate-zoom" style="max-width: 600px"> <div class="col-md-6 w3-card-8 w3-teal" onclick="openForm('Login')"> <h3>Sign In</h3> </div> <div class="col-md-6 w3-card-8 w3-teal" onclick="openForm('Register')"> <h3>Sign Up</h3> </div> <div style="margin-top: 25px !important;"> <div id="Login" class="w3-container form"> <div class="w3-container "> <div class="w3-section"> <br> <br> <form action="/login" method="POST"> {{ csrf_field() }} <input type="hidden" name="redirurl" value="{{ $_SERVER['REQUEST_URI'] }}"> <label><b>Username</b></label> <input name="username" class="w3-input w3-border w3-margin-bottom" type="text" placeholder="Enter Username" required> <label><b>Password</b></label> <input class="w3-input w3-border w3-margin-bottom" name="password" type="password" placeholder="Enter Password" required> <input type="submit" class="w3-btn w3-btn-block w3-green" value="Login"> <input class="w3-check w3-margin-top" type="checkbox" checked="checked"> Remember me </form> </div> </div> <div class="w3-container w3-border-top w3-padding-16 "> <button onclick="document.getElementById('authentication').style.display='none'" type="button" class="w3-btn w3-red">Cancel</button> <span class="w3-right w3-padding w3-hide-small">Forgot <a href="#">password?</a></span> </div> </div> </div> <div id="Register" class="w3-container form "> <div class="w3-container"> <div class="w3-section"> <form action="/register" method="POST" id="regForm"> {{ csrf_field() }} <input type="hidden" name="redirurl" value="{{ $_SERVER['REQUEST_URI'] }}"> <label><b>Email</b></label> <input class="w3-input w3-border w3-margin-bottom" type="text" name="email" placeholder="Enter Email" value="{{ old('email') }}" required> <label><b>Username</b></label> <input class="w3-input w3-border w3-margin-bottom" type="text" name="username" placeholder="Enter username" required value="{{ old('username') }}"> <label><b>Password</b></label> <input class="w3-input w3-border w3-margin-bottom" type="password" name="password" required placeholder="Enter Password"> <label><b>Confirm Password</b></label> <input class="w3-input w3-border w3-margin-bottom" required type="password" name="password_confirmation" placeholder="Enter Password"> <button type="submit" class="w3-btn w3-btn-block w3-green">SignUp</button> </form> </div> </div> <div class="w3-container w3-border-top w3-padding-16 "> <button onclick="document.getElementById('authentication').style.display='none'" type="button" class="w3-btn w3-red">Cancel</button> </div> </div> </div> </div> <script> openForm("Login"); function openForm(formName) { var x = document.getElementsByClassName("form"); for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } document.getElementById(formName).style.display = "block"; } </script> |
Now, when we fill the signup form, it is sent to register controller, there we store the user details in the users table, here we use the default table users came along with laravel installation.
1 2 3 4 5 6 7 8 9 | public function register(Request $request) { $user = new User (); $user->name = $request->get ( 'username' ); $user->email = $request->get ( 'email' ); $user->password = Hash::make ( $request->get ( 'password' ) ); $user->remember_token = $request->get ( '_token' ); $user->save (); return redirect ( '/' ); } |
After registration, we will be able to login,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public function login(Request $request) { if (Auth::attempt ( array ( 'name' => $request->get ( 'username' ), 'password' => $request->get ( 'password' ) ) )) { session ( [ 'name' => $request->get ( 'username' ) ] ); return Redirect::back (); } else { Session::flash ( 'message', "Invalid Credentials , Please try again." ); return Redirect::back (); } } |
Logout functionality is done flushing session, and calling Auth::logout function,
1 2 3 4 5 | public function logout() { Session::flush (); Auth::logout (); return Redirect::back (); } |
and routes file will look like,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php /* * |-------------------------------------------------------------------------- * | Application Routes * |-------------------------------------------------------------------------- * | * | Here is where you can register all of the routes for an application. * | It's a breeze. Simply tell Laravel the URIs it should respond to * | and give it the controller to call when that URI is requested. * | */ Route::get ( '/', function () { return view ( 'customAuthentication' ); } ); Route::post ( '/login', 'MainController@login' ); Route::post ( '/register', 'MainController@register' ); Route::get ( '/logout', 'MainController@logout' ); |
Working Demo Project on Github
See the output in screenshots below,





Working Demo Project on Github
We did not check for validations here, we look at validations in seperate post. Feel free to look at previous posts on routing and pagination
Why do not you put these codes in Gist? Not the entire application, but the created codes and complete views.
Great post.
Hi Lucas Mahle,
I ‘ve kept the gists in the demo page of this app , you can see the gists in application’s demo pagehttp://justlaravel.com/demos/customAuthentication/
Complete application is also available on github, https://github.com/avinashn/Custom-Authentication-in-Laravel
Nice one tutorial….
I want to make a user/client/cumstomer/employee etc.(one of them)form With login socia sites network. How i make it .
My aim is when i run this command php artisan make:auth
This make the admin pannel for our app.
2. How i make a news letters subscription function in laravel 5.2 for latest news.
3.How i use Paypal functionality .And how i check the account detail on my local system .Besause i want to work on localhost and i see the user payment detail in laravel 5.2
Please tell me about these topic…
There is tutorial already on sending emails, you can extend that to send newsletters subscription mails check it here http://justlaravel.com/sending-emails-using-sendgrid-laravel/
Tutorial on payment gateways is coming soon, stay tuned.
useless