Making simple authentication system using auth command in laravel

Making Simple Authentication System

Laravel is a free, open-source, powerful and fully MVC based framework. Laravel provides rich set of features and functionalities which speed up website development process. In this blog, we are going to understand making of simple authentication system using auth command in laravel and its working process.

 

Laravel Authentication –

Laravel makes authentication implementation very easy. The authentication configuration file is located at config/auth.php, which contains several well documented options for calling authentication services. Laravel’s authentication facilities are made up of “guards” and “providers”. Guards define how users are authenticated for each request. Providers define how users are retrieve from your persistent storage.

 

Laravel provides a quick way to scaffold(means quickly set up skeleton for your project) all of the routes, layout view, login view you need for authentication using one simple command:

php artisan make:auth

A HomeController will also be generate to handle post-login requests to your application’s dashboard. Once you run this command, you are ready to register and authenticate new users for your application.

 

Model, view and controller for the authentication system,

  • Model – By default, Laravel includes an App\User Eloquent model in your app directory.
  • Views – The php artisan make:auth command will create all of the views you need for authentication and place them in the resources/views/auth directory. It will also create a resources/views/layouts directory containing a base layout for your application. All these views used the Bootstrap CSS framework, but you are free to customize them however you wish.
  • Controllers – Laravel ships with several pre-built authentication controllers, which are located in the App\Http\Controllers\Auth namespace. The RegisterController handles new user registration, the LoginController handles authentication, the ForgotPasswordController handles e-mailing links for resetting passwords, and the ResetPasswordController contains the logic to reset passwords. Each of these controllers used a trait to includes their necessary methods. For many applications, you will not need to modify these controller at all.

 

You are free to customize these model,views,controller and its routes according to your project and requirements.

    • Path Customization –

      When a user is successfully authenticate, they will be redirect to the /home URI. You can customize the post-authentication redirect location by defining a redirectTo property on the LoginController, RegisterController, ResetPasswordController, and VerificationController:
      protected $redirectTo = '/';

 

    • Username Customization –

      By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:
      public function username()
      {
      return 'username';
      }

 

    • Guard Customization –

      You may also customize the “guard” that is used to authenticate and register users. To get started, define a guard method on your LoginController, RegisterController, and ResetPasswordController. The method should return a guard instance:
      protected function guard()
      use Illuminate\Support\Facades\Auth;
      {
      return Auth::guard('guard-name');
      }

 

    • Validation Customization –

      The validator method of the RegisterController contains the validation rules for new users of the application. You are free to modify this method as you wish.

 

    • Retrieving The Authenticated User –

      You may access the authenticated user via the Auth facade:
      use Illuminate\Support\Facades\Auth;
      // Get the currently authenticated user...
      $user = Auth::user();
      // Get the currently authenticated user's ID...
      $id = Auth::id();

 

    • Determining If The Current User Is Authenticated –

      You may use the check method on the Auth facade to determine if the user is already logged into your application, which will return true if the user is authenticated:
      use Illuminate\Support\Facades\Auth;
      if (Auth::check()) {
      // The user is logged in...
      }

 

    • Protecting Routes –

      To protect routes all you need to do is attach the middleware to a route definition:
      Route::get('profile', function () {
      // Only authenticated users may enter...
      })->middleware('auth');

 

    • Redirecting Unauthenticated Users –

      When the auth middleware detect an unauthorized user, it will redirect the user to the login name route. You may modify this behavior by updating the redirectTo function in your app/Http/Middleware/Authenticate.php file:
      protected function redirectTo($request)
      {
      return route('login');
      }

      For any specific controller, you may call the middleware method in controller’s constructor instead of attaching it in the route definition directly:
      public function __construct()
      {
      $this->middleware('auth');
      }

 

    • Remembering Users –

      If you would like to provide “remember me” functionality in your application, you may pass a boolean value as the second argument to the attempt method, which will keep the user authenticated indefinitely, or until they manually logout. Of course, your users table must include the string remember_token column, which will be used to store the “remember me” token.
      if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
      // The user is being remembered...
      }

 

  • Logging Out –

    To log users out of your application, you may use the logout method on the Auth facade. This will clear the authentication information in the user’s session:
    Auth::logout();