Custom Authentication (SignIn / SignUp) in Laravel

As routing and pagination is very simple in laravel, authentication is not much different, it is very simple too, everything is configured out of the box.

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,

// 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’);

// 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 place 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.

Note : we are not using a validations here, validations are discussed in separate post here, make sure you look at it

Firstly, lets 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.

  • SignIn/SignUp
  • SignIn/SignUp

above line of code calls the modal when SignIn/SignUp is clicked,
and the code for modal is,

X

Sign In

Sign Up

{{ csrf_field() }} Username Password Remember me Cancel Forgot password? {{ csrf_field() }} Email Username Password Confirm Password SignUp Cancel 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”; }

            onclick=”document.getElementById(‘authentication’).style.display=’none'”             class=”w3-closebtn w3-grey w3-hover-red w3-container w3-padding-16 w3-display-topright”>X                                                                                                                                                    {{ csrf_field() }} Username                                     class=”w3-input w3-border w3-margin-bottom” type=”text”                                     placeholder=”Enter Username” required> Password                                  Remember me                                                  onclick=”document.getElementById(‘authentication’).style.display=’none'”                             type=”button” class=”w3-btn w3-red”>Cancel                         Forgot password?                                                                                    {{ csrf_field() }} Email                              Username                              Password Confirm                                     Password                             SignUp                                          onclick=”document.getElementById(‘authentication’).style.display=’none'”                         type=”button” class=”w3-btn w3-red”>Cancel 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”; }

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.

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 ( ‘/’ ); }

public function register(Request $request) {         $user->name = $request->get ( ‘username’ );         $user->email = $request->get ( ’email’ );         $user->password = Hash::make ( $request->get ( ‘password’ ) );         $user->remember_token = $request->get ( ‘_token’ );

After registration, we will be able to login,

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 (); } }

public function login(Request $request) {         if (Auth::attempt ( array (                 ‘name’ => $request->get ( ‘username’ ),                 ‘password’ => $request->get ( ‘password’ )                     ‘name’ => $request->get ( ‘username’ )             return Redirect::back ();             Session::flash ( ‘message’, “Invalid Credentials , Please try again.” );             return Redirect::back ();

Logout functionality is done flushing session, and calling Auth::logout function,

public function logout() { Session::flush (); Auth::logout (); return Redirect::back (); }

public function logout() {         return Redirect::back ();

and routes file will look like,

You might also like

More Similar Posts