How to user Authenticate in laravel using Auth?

Auth::routes()

Auth::routes() this method is user for user authentication. this mentod is write in web.php file in routing.

The Auth::routes() method includes the routes for login, registration, logout, and password reset.

The Auth::routes() are in the laravel/ui package in the AuthRouteMethods.php file

vendor/laravel/ui/src/AuthRouteMethods.php

File name : AuthRouteMethods.php

public function auth()
{
return function ($options) {
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
if ($options['register'] ?? true) {
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
}

// Password Reset Routes...
if ($options['reset'] ?? true) {
$this->resetPassword();
}

// Password Confirmation Routes...
if ($options['confirm'] ??
class_exists($this->prependGroupNamespace('Auth\ConfirmPasswordController'))) {
$this->confirmPassword();
}

// Email Verification Routes...
if ($options['verify'] ?? false) {
$this->emailVerification();
}
};
}

The authentication controllers are located in the app/Http/Controllers/Auth folder.

In Laravel, you can protect a route using a middelware

Laravel has a builtin auth middleware, which exists in Illuminate\Auth\Middleware\Authenticate.

Open the app/Http/Controllers/HomeController.php file. It has the following code

File name : web.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{

public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('home');
}
}

The middleware() method can be either called from the controller or the route definition. So let's remove the call from the controller. Next, open the routes/web.php file and update the home route defintion as follows:

Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');

File name : web.php


How to Disable User Registration?

add a parameter in routes/web.php

File name : web.php

Auth::routes(['register' => false]);

after that you won’t see Register link in top-right corner, and the route /register will show 404 page.

How to enable Enable Email Verification in laravel?

email verification with database field users.email_verified_at. By default disabled.

To enable this function, just pass a parameter in routes/web.php

File name : web.php

Auth::routes(['verify' => true]);

Finally, if you need some routes available only to verified users, use verified Middleware:

Route::get('profile', function () {
// Only verified users may enter...
})->middleware('verified');

How to Disable Reset Password?

File name : web.php

Auth::routes(['reset' => false]);

Note: you can combine registration, verification, and reset in your routes/web.php:

File name : web.php

Auth::routes([
'register' => false,
'verify' => true,
'reset' => false
]);

where user authenticates routes are defined?

User Authentication routes are listed in one method in vendor/laravel/framework/src/Illuminate/Routing/Router.php

File name : router.php

public function auth(array $options = [])
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
if ($options['register'] ?? true) {
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
}

// Password Reset Routes...
if ($options['reset'] ?? true) {
$this->resetPassword();
}

// Email Verification Routes...
if ($options['verify'] ?? false) {
$this->emailVerification();
}
}

How to Redirect After Registration?

By default, new registered users are redirected to URL /home. but you want to change it. it’s done in a file app/Http/Controllers/Auth/RegisterController.php

File name : index.php

class RegisterController extends Controller
{
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
...........


}

you want to register to different URLs based on the role of new user. Then you can create a separate method in the same class RegisterController, with name redirectTo():

File name : RegisterController.php

protected function redirectTo()
{
if (auth()->user()->role_id == 1) {
return '/admin';
}
return '/dashboard';
}

How to Change Field Validation Rules?

File name : app/Http/Controllers/Auth/RegisterController.php

protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
]);
}

How to Disable Auto-Login after User Registration?

default behavior that you may want to change is auto-login immediately after the registration form. You may want to redirect your user to a separate “success” page and expect them to log in manually later.

you need to override register() method of a trait RegistersUsers.

File name : index.php

To disable auto-login, you need to delete this particular line:

$this->guard()->login($user);

File name : index.php


Single User Authentication.

File name : web.php

Route::get('/admin', function () {
return view('auth.login');
});
Auth::routes();
// for email verify
Auth::routes(['verify' => true]);

Login Controller

File name : App\Http\Controllers\Auth\LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Response;

class LoginController extends Controller
{

use AuthenticatesUsers;


protected $redirectTo = '/home';


public function __construct()
{
$this->middleware('guest')->except('logout');
}


public function login(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);

if (\Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
$request->session()->regenerate();
//return response(['success' => true], Response::HTTP_OK);
return redirect('/home')->with('loginmsg', 'Login Successfull');
}
else
{


//return redirect()->route('/admin')->with('error','Email-Address And Password Are Wrong.');
return back()->with('error', 'Sorry! Your Email and Password are Invalid.');
}




}



/*public function redirectTo()
{
// do whatever you want
return '/dashboard';
}*/

public function logout(Request $request)
{
Auth::logout();
$request->session()->flush(); // it remove the all session data
//$request->session()->forget('username'); // it remove the single session data
return redirect('/admin');

}

}

File name : index.php

Route::get('/home', 'HomeController@index')->name('home');

File name : index.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{

public function __construct()
{
//$this->middleware('auth');
$this->middleware(['auth', 'verified']);
}


public function index()
{
return view('admin.dashboard');
}



}





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial