Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Bhujbal Knowledge City

MET Institute of Engineering

Seminar
On
Core Knowledge of laravel and
Authentication
Presented by
Praveen Dinesh Datrange
Guided by
Prof. Javed Attar
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering
CONTENTS
 Laravel Introduction

 What is Authentication

 Why we use Authentication in Laravel

 Authentication process

 Email Verifaction

 Reset Password Via Link

 Advantages

 Drawbacks

 Conclusion
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering

LARAVEL INTRODUCTION
 Laravel is an open-source PHP framework, which is robust and
easy to understand. It follows a model-view-controller design
pattern. Laravel reuses the existing components of different
frameworks which helps in creating a web application. The web
application thus designed is more structured and pragmatic.

 Laravel offers a rich set of functionalities which incorporates


the basic features of PHP frameworks like CodeIgniter, Yii and
other programming languages like Ruby on Rails. Laravel has
a very rich set of features which will boost the speed of web
development.

 If you are familiar with Core PHP and Advanced PHP, Laravel
will make your task easier. It saves a lot time if you are
planning to develop a website from scratch. Moreover, a
website built in Laravel is secure and prevents several web
attacks.
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering

Let's first understand the MVC architecture.


:MVC is divided into three letters shown below:

 M: 'M' stands for Model. A model is a class that deals with a


database. For example, if we have users in an application then
we will have users model that deals with a database to query the
table of users if we have users model, then we will also have a
users table. We conclude from the example that the model is
going to have a table for that specific model.

 V: 'V' stands for View. A view is a class that deals with an HTML.


Everything that we can see on the application in the browser is
the view or the representation.

 C: 'C' stands for Controller. A controller is the middle-man that


deals with both model and view. A controller is the class that
retrieves the data from the model and sends the data to the view
class.
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering

What is Authentication

 Authentication is the process of identifying the user


credentials. In web applications, authentication is
managed by sessions which take the input parameters
such as email or username and password, for user
identification. If these parameters match, the user is said
to be authenticated

 User authentication is a common feature in web


applications. Laravel eases designing authentication as it
includes features such as register, forgot
password and send password reminders.

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering

WHY WE USE AUTHENTICATION IN LARVEL

 Laravel makes implementing authentication very simple. In fact,


almost everything is configured for you out of the box. The
authentication configuration file is located at config/auth.php, which
contains several well documented options for tweaking the
behavior of the authentication services.

At its core, Laravel's authentication facilities are made up of


"guards" and "providers". Guards define how users are
authenticated for each request. For example, Laravel ships with
a session guard which maintains state using session storage and
cookies.

Providers define how users are retrieved from your persistent


storage. Laravel ships with support for retrieving users using
Eloquent and the database query builder. However, you are free to
define additional providers as needed for your application.
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering

AUTHENTICATION PROCESS
Step 1:- Create the application
 We need to run command to create laravel project.
laravel new multi-auth
Step 2:- Create the database
• We will use SQL database for our application. It is lightweight, fast
and uses a simple flat file.
Step 3: Creating migrations
Create migration for admins
php artisan make:migration create_admins_table
Now, create a migration for bloggers
php artisan make:migration create_users_table

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering
AUTHENTICATION PROCESS
Step 4:- Migrate the database
Now that we have defined our tables, let us migrate the database:
php artisan migrate
Now, we have 2 different tables users, admins, to use these different
tables to authenticate, we need to define 2 models for them. These
models will be like the user model and extend the Authenticable class.
Step 5: Set up the models
Admin model
To make the model for the admins, run the following command:
php artisan make:model Admin
User model
php artisan make:model Writer
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering
AUTHENTICATION PROCESS
Step 6 :-Define the guards
Laravel guards define how users are authenticated for each
request. Laravel comes with some guards for authentication, but we
can also create ours as well.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering
AUTHENTICATION PROCESS
We added two new guards admin and user and set their providers.
These providers tell Laravel what to use for authentication or
validation when we try to use the guard.

'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering

AUTHENTICATION PROCESS
Step 7:- Set up the controllers
To use our guards for authentication, we can either modify the existing
authentication controllers or create new ones.

Modify LoginController
Open the LoginController in app/Http/Controllers/Auth and edit as follows:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{ use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
npm
phpinstall
artisan&&
ui vue
npm--auth
run dev

Bhujbal Knowledge City


MET Institute of Engineering

AUTHENTICATION PROCESS
Now, Modify RegisterController
Open the RegisterController and edit it
registration is complete.

Step 8 :- Set up authentication pages


We will use Laravel’s auth scaffolding to generate pages and
controllers for our authentication system. Run the following
command to generate the authentication pages:
Below 6.0 laravel version
php artisan make:auth
laravel version 6.0 & Above
composer require laravel/ui –dev
php artisan ui vue --auth
npm install && npm run dev

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering

AUTHENTICATION PROCESS
This will generate view files in resources/views/auth along with routes to handle
basic authentication for our application. Is that cool or what?

 User Login Screen

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering

AUTHENTICATION PROCESS
 Admin Registration Screen

Step 9 :- Create the pages authenticated users will access


Now that we are done setting up the login and register page, let us make
the pages the admin and writers will see when they are authenticated.
Open the terminal and run the following commands to create new files.
Next, we will insert the corresponding code snippets to the files.
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering
AUTHENTICATION PROCESS
Step 10:- Set up the routes
<?php Route::view('/', 'welcome');
Auth::routes();
Route::get('/login/admin', 'Auth\LoginController@showAdminLoginForm');
Route::get('/login/writer', 'Auth\LoginController@showWriterLoginForm');

Step 11:- Modify how our users are redirected if authenticated


So, to solve that, open
the app/Http/Controllers/Middleware/RedirectIfAuthenticated.php file and
replace with this:
// app/Http/Controllers/Middleware/RedirectIfAuthenticated.php
// app/Http/Controllers/Middleware/RedirectIfAuthenticated.php
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{ public function handle($request, Closure $next, $guard = null)
{ if ($guard == "admin" && Auth::guard($guard)->check())
{ return redirect('/admin'); }
if ($guard == "writer" && Auth::guard($guard)->check())
{ return redirect('/writer'); }
if (Auth::guard($guard)->check())
{ return redirect('/home'); }
return $next($request);}

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering

AUTHENTICATION PROCESS
Step 12:- Modify authentication exception handler
The unauthenticated method we just added resolves this issue we
have. It receives an AuthenticationExpection exception by default
which carries that guard information. Sadly, we cannot access that,
because it is protected 

Step 13 : Run the application

php artisan key:generate


php artisan serve

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering

Email Verification
In laravel old version we are doing email verification process manually,
but in laravel 5.8 they provide in build email verification setup for new registered
users to must have to verify his email before proceed. You just need to make
some basic setup with need to use middleware, routes and mail configuration

Step 1:- Migration


• in this command we add some new column in table After doing the basic
configuration, migrate the tables by this command:
php artisan migrate

Step 2 : Setup Email Configuration


•Here, we need to add email configuration in .env file. We are sending email after
user registration so we need to add email smtp details for send email.
MAIL_DRIVER=smtp
MAIL_HOST=ssl://smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=pravincafe@gmail.com
MAIL_PASSWORD=***********
MAIL_ENCRYPTION=null
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering
Email Verification
Step 3: Create Auth
• Laravel provide very quick way to create registration, login and forgot
password with routes by auth command, So simply run bellow command to
create:
php artisan make:auth
Step 4: Email Verification Setup
• Open User model from app/User.php and implements MustVerifyEmail in
User class. So the code will look like:

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering
Email Verification
Step 5 : Configure Auth Route
• In this step, we need to add extra parameter inside Auth::routes(). Open
routes/web.php and do the change like this:
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify'=>true]);
To protect the HomeController route, we need to do this in construct() method:
public function __construct()
{
$this->middleware(['auth' => 'verified']);
}
Step 6 : Test Email Verification
• Open a browser and go to your project by entering http://localhost:8000/register. Im
using a custom domain in localhost project. So that Im opening
https://laravel.dev/register (Custom Domain with SSL). Fill up the registration form:
• run the command
php artisan serve
Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City
Bhujbal Knowledge City
MET Institute of Engineering
Email Verification
User registration screen

 verify link

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering
Reset Password via email
Since we have already setup the mail driver, now all we have to do is click Forgot Your
Password button on the login form and it will open the password email view. Enter
your email, click Send Password Reset Link button and it will send the password
reset email to your mailtrap inbox. Now open the email from mailtrap and click Reset
Password button. This will open the password reset form where you can reset the
password.
Step 1 Create a Route and a Controller
• Create a routes, controller and methods through which the email address that
requires password reset will be submitted to via the password reset form. The name
of these routes, controllers and methods are totally up to you
Step 2 Change the action property on the password reset form
• The password reset form can be found here
resources/view/auth/passwords/email.blade.php
Step 3 Create token and Send Password Reset Link via email
• Then add the validatePasswordRequest method in the ResetPasswordController and
use or modify.

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering
Reset Password via email
step 4- Run the application
• php artisan serve
 Reset Password Link

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering
Advantages and disadvantages
Advantages
• The web application becomes more scalable, owing to the Laravel
framework.
• Considerable time is saved in designing the web application, since Laravel
reuses the components from other framework in developing web application
• It includes namespaces and interfaces, thus helps to organize and manage
resources
• The blade template engine provides an easy experience to add any kind of
logic into the HTML file. It has become so easy to add new features into
applications without hacking the core.
• The blade template engine provides an easy experience to add any kind of
logic into the HTML file. It has become so easy to add new features into
applications without hacking the core.
• Create good looking frontends

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering
Advantages and disadvantages
Disadvantage
• Laravel is a lightweight framework so it has less inbuilt support when
compared to Django and Ruby on Rails. This problem can be solved by
integrating thirdparty tools but for large or custom websites, the tasks can
become tedious and complicated.
• The development is not as fast compared to ruby on rails
• Many methods included in the reverse routing process are complex.

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering
Conclusion
Laravel is a powerful MVC PHP framework, designed for developers who need
a simple and elegant toolkit to create full-featured web applications. Laravel
was created by Taylor Otwell. This is a brief tutorial that explains the basics
of Laravel framework.

it is a MVC framework that can manange think in model view and controller
Laravel offers a rich set of functionalities which incorporates the basic features
of PHP frameworks like CodeIgniter, Yii and other programming languages
like Ruby on Rails. Laravel has a very rich set of features which will boost
the speed of web development.

we dived deep into Laravel authentication. We defined multiple guards to


handle multiple authentications and access control. We also handle
redirection for authenticated user and redirection for an unauthenticated
user.

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering

Question ??

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City


Bhujbal Knowledge City
MET Institute of Engineering

Thank you!

Name-Praveen Datrange, Roll No-06 MCA Department, Bhujbal Knowledge City

You might also like