Search

Laravel Auth's Tags

How to create and customise login in laravel
Laravel has great features out of the box, like Routing, Security, migration etc. Laravel also has comes authentication feature. From version 6.X, Laravel has made some changes in authentication module. To activate Laravel auth module, simply run bellow command in Terminal. composer require laravel/ui --dev php artisan ui vue --auth This will insert login and register views as well as routes. You can change default options for the auth from the config/auth.php file. Here is also some tricks you can customise in authentication. You can change redirect url after the user successfully authenticated in App/Providers/RouteServiceProvider.php file. public const HOME = '/home'; You can also login user by its username instead of email address at App/Http/Controllers/auth/LoginController.php file. Just add username() method in LoginController. public function username() {     return 'username'; } Get the current registered user with bellow Auth class methods. use Illuminate\Support\Facades\Auth; // this will return user object $user = Auth::user(); // this will return user id $id = Auth::id() You can check if user is already logged in or not by check() method. It will return true if user is already logged in. use Illuminate\Support\Facades\Auth; if (Auth::check()) {     // user is logged in } You can also manually authenticate users by using attempt() method. public function login(Request $request) {     $credentials = $request->only('email', 'password');     if (Auth::attempt($credentials)) {         // user logged in         return redirect()->intended('dashboard');     } } If you want to login to different guard other than default one, just chain guard() method. if (Auth::guard('admin')->attempt($credentials)) {     // user logged in     return redirect()->intended('dashboard'); } You can also login user with remember me also. Just pass $remember = true boolean value. if (Auth::attempt($credentials, $remember)) {     // The user is being remembered... } If you want to protect some routes by authentication middleware, just add auth middleware. Route::get('my-profile', 'ProfileController@index')->middleware('auth'); You can also set middleware in the controllers also. Just add middleware() method in __construct() method in controller class. public function __construct() {     $this->middleware('auth'); } This will redirect user to loggin page if it is not logged in. If you want to change this url, change return route in redirectTo() function of app/Http/Middleware/Authenticate.php file. protected function redirectTo($request) {     return route('login'); } LoginController.php class already use Illuminate\Foundation\Auth\ThrottlesLogins.php trait. So if you want to restrict user from entering wrong credentials for several times, add bellow properties in the LoginController.php // Default is 5 protected $maxAttempts = 3; // Default is 1 protected $decayMinutes = 3; $maxAttempts is the number of wrong attempts to login. After that user will have to wait for $decayMinutes. This way you can customise Laravel authentication scafolding.
How to make Laravel Auth help of laravel-ui Package
You might have descried after installing a fresh Laravel 7 application that the composition:auth command no longer subsists. We’ve received lots of messages and emails about this very issue, so I cerebrated I’d inscribe up an expeditious tutorial on engendering auth scaffolding in Laravel 7. First off, you can find everything you require to ken in the Laravel 7 Authentication documentation. If you operate an expeditious walkthrough, here goes nothing. Laravel UI Laravel UI is an incipient first-party package that extracts the UI portion of a Laravel project into a separate laravel/ui package. The separate package enables the Laravel team to iterate on the UI package discretely from the main Laravel codebase. You can install the laravel/ui package via composer: composer require laravel/ui Once you’ve installed laravel/ui you have a couple of commands available to generate UI code, including authorization. If you intend to use Vue, React, or Bootstrap, the UI package provides the following command: php artisan ui --help Here are a few examples: php artisan ui vue php artisan ui react If you want to generate the auth scaffolding at the same time: php artisan ui vue --auth php artisan ui react --auth The ui:auth Command Besides the new ui command, the laravel/ui package comes with another command for generating the auth scaffolding: php artisan ui:auth If you run the ui:auth command, it will generate the auth routes, a HomeController, auth views, and a app.blade.php layout file. You can also generate the views only with: php artisan ui:auth --views The other cool thing here is that the console command will prompt you to confirm overwriting auth files if you’ve already run the command before.
Laravel 5.5 - Login With Only Mobile Number Using Laravel Custom Auth
Today, we are share with you how to modify laravel bydefault authentication and make it custom as per our requirement. in this totorials we are modify laravel authentication sysstem and make "how to login with only mobile number in laravel application using laravel custom authentication". but you also can modify as per your requirement. Laravel provide bydefault authentication system on users table's email and password fields and it very helpfull for us. but sometime we want to change laravel authentication and make is as per our requirment. Ex. we want username or password, we want mobile number and password and sometime we want only mobile number for authentication and not extra needed. so, how to built laravel custom authentication? we are share here with very simple example. if you fallow all step then you can change laravel auth and set your custom auth as per your choice. it is very easy. Create Laravel New Project First, we are create one new fresh laravel project run by following command. if you already have laravel project and you want implement custom auth your exist laravel application. so, this step skeep and move on next. composer create-project --prefer-dist laravel/laravel blog After installation please setup your database in .env file Create Laravel Auth Next, Generate laravel bydefault auth run by following command php artisan make:auth Run Laravel Migration Befor run laravel migration please add one extra mobile_no field in user table migration open it and add like that. Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('mobile_no')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); Next, then after run laravel migration using following command php artisan migrate Change in RedirectIfAuthenticated Next, now we are need some changes in RedirectIfAuthenticated middleware. so, open your app/Http/Middleware/RedirectIfAuthenticated.php file and make change like that. namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class RedirectIfAuthenticated { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = null) { if (Auth::check()) { return redirect('/home'); } return $next($request); } } [ADDCODE] Overwrite login() method Next, we are need to overwrite login() method. so, open your app/Http/Controllers/Auth/LoginController.php file and make some change following way. namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; use App\Http\Requests; use App\User; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ 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'); $this->user = new User; } public function login(Request $request) { // Check validation $this->validate($request, [ 'mobile_no' => 'required|regex:/[0-9]{10}/|digits:10', ]); // Get user record $user = User::where('mobile_no', $request->get('mobile_no'))->first(); // Check Condition Mobile No. Found or Not if($request->get('mobile_no') != $user->mobile_no) { \Session::put('errors', 'Your mobile number not match in our system..!!'); return back(); } // Set Auth Details \Auth::login($user); // Redirect home page return redirect()->route('home'); } } Change In Login Blade Next, open your resources/views/auth/login.blade.php file and make changes this way. @extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-heading">Login</div> <div class="panel-body"> <form class="form-horizontal" method="POST" action="{{ route('login') }}"> {{ csrf_field() }} <div class="form-group{{ $errors->has('mobile_no') ? ' has-error' : '' }}"> <label for="mobile_no" class="col-md-4 control-label">Enter Mobile No.</label> <div class="col-md-6"> <input id="mobile_no" type="text" class="form-control" name="mobile_no" value="{{ old('mobile_no') }}" required autofocus> @if ($errors->has('mobile_no')) <span class="help-block"> <strong>{{ $errors->first('mobile_no') }}</strong> </span> @endif </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <div class="checkbox"> <label> <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me </label> </div> </div> </div> <div class="form-group"> <div class="col-md-8 col-md-offset-4"> <button type="submit" class="btn btn-primary"> Login </button> <a class="btn btn-link" href="{{ route('password.request') }}"> Forgot Your Password? </a> </div> </div> </form> </div> </div> </div> </div> </div> @endsection Now we are ready to run our example so run bellow command ro quick run: php artisan serve Now you can open bellow URL on your browser: http://localhost:8000/login If you want to any problem then please write comment and also suggest for new topic for make tutorials in future. Thanks...