Skip to content
  • Home
  • Blog
    • yttags
    • Youtube
  • Categories
  • Tools
  • Server-Side
    • php
    • Node.js
    • ASP.NET
    • Magento
    • Codeigniter
    • Laravel
    • Yii
  • JS
    • AngularJS
    • Ajax
    • VueJs
    • jQuery
    • ReactJS
    • JavaScript
  • Full Form
  • Guest Post
  • Advertise
  • About
  • Contact Us
Pakainfo

Pakainfo

Web Development & Good Online education

authentication login with username or email in laravel

July 8, 2018 Pakainfo Laravel, Mysql, Mysqli, php, Programming Leave a comment
Rate this post

authentication login with username or email in laravel

login with username or email in laravel 5 Example

Laravel comes with simple store auth out-of-the-box authorization email or password mechanism which is simple security incredibly easy to use. But it all the depends on several pre-defined function or things, one of the simple main purpose ones โ€“ DB table of any users structure and mechanism for login with email address field. What if we want to have email or username to identify a clients?authentication login with username or email in laravel Example

Read Also:  laravel 8 login and registration example | Laravel 8 Auth

login with username or email in laravel 5

    public function login(Request $request)
    {
        $field = filter_var($request->input('email_address_username'), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
        $request->merge([$field => $request->input('email_address_username')]);
        if (\Auth::attempt($request->only($field, 'password')))
        {
            return redirect('/');
        }
        return redirect('/login')->withErrors([
            'error' => 'These specific credentials have not match our admin panel records.',
        ]);
    }

Routing using Laravel

php artisan make:auth

Free Live Chat for Any Issue

laravel 5.3 or more version login with username or email Example

All we some need is add a just property in app/Http/AuthController.php:

protected $username = 'username';

 Illuminate\Foundation\Auth\AuthenticatesUsers.php
 
   public function postLogin(Request $request)
    {
        $this->validate($request, [
            $this->loginUsername() => 'required', 'password' => 'required',
        ]);

And then letโ€™s look into loginUsername() method.

 
    public function loginUsername()
    {
        return property_exists($this, 'username') ? $this->username : 'email';
    }

laravel Auth login with username or email

	
	<form method="POST" action="/auth/login">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="text" placeholder="username or email" name="username" value="{{ old('username') }}">
    </div>

    <div>
        Password
        <input type="password" placeholder="your strong password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>

and in we AuthenticatesAndRegisterUsers.php we can change email address to simple username also.

Read Also:  jQuery Delete Multiple Rows using checkbox PHP with MySQL

public function postLogin(Request $request_data)
{
	$this->validate($request_data, [
		'username' => 'required', 'password' => 'required',
	]);

	$credentials = $request_data->only('username', 'password');

	if ($this->auth->attempt($credentials, $request_data->has('remember')))
	{
		return redirect()->intended($this->redirectPath());
	}

	return redirect($this->loginPath())
				->withInput($request_data->only('username', 'remember'))
				->withErrors([
					'username' => $this->getFailedLoginMessage(),
				]);
}

Download

Example

Related posts:

  1. html code for login page with username and password validation
  2. Login Signup with Laravel
  3. Setup Laravel 6 Login Authentication
  4. Laravel 5.7 Get Current UserId UserName and Email
  5. Laravel Custom Login Registration Example Tutorial
  6. Laravel 7 User Login Authentication From Scratch
  7. how to make a login page in html with database?
  8. Vue.js User Signup And Login Authentication Using PHP
  9. PHP login logout script with session Example
  10. How to Setup Laravel 6 Login Authentication Tutorial
Read Also:  Laravel 5.5 Resolve errors The page has expired due to inactivity
laravel 5 login and registration tutoriallaravel 5.2 login with usernamelaravel 5.3 login with username or emaillaravel 5.4 login with username or emaillaravel 5.5 login with usernamelaravel 5.5 login with username or emaillaravel 5.6 login with username or emaillaravel auth

Post navigation

Previous Post:Set dynamically Next Page Url Using JQuery DataTable
Next Post:Laravel Autocomplete text field suggestions search using Database

Search

Write For Us

Weโ€™re accepting well-written informative guest posts and this is a great opportunity to collaborate.
Submit a guest post to [email protected]
Contact Us

Freelance web developer

Do you want to build a modern, lightweight, responsive website quickly?
Need a Website Or Web Application Contact : [email protected]
Note: Paid Service
Contact Me
Cricday

Categories

3movierulz (48) Ajax (464) AngularJS (377) ASP.NET (61) Bollywood (92) Codeigniter (174) CSS (96) Earn Money (61) Education (53) Entertainment (108) fullform (77) Google Adsense (62) Highcharts (77) Hollywood (93) JavaScript (1354) Jobs (39) jQuery (1421) Laravel (1083) LifeStyle (50) movierulz4 (47) Mysql (1029) Mysqli (890) Node.js (38) php (2110) Programming (2320) Python (96) ReactJS (37) Software (102) Software (77) Stories (78) tamilrockers (88) Tamilrockers kannada (48) Tamilrockers telugu (47) Tech (102) Technology (2359) Tips and Tricks (107) Tools (112) Top10 (295) Trading (50) Trending (45) VueJs (250) Web Technology (83) webtools (129) wordpress (165) World (123)

Advertise With Us

Increase visibility and sales with advertising. Let us promote you online.
Click Here

A To Z Full Forms

Access a complete full forms list with the meaning, definition, and example of the acronym or abbreviation.
Click Here

Web Development & Good Online education : Pakainfo by Pakainfo.
Top
Subscribe On YouTube : Download Source Code & New itsolutionstuck
We accept paid guest Posting on our Site : Guest Post Chat with Us On WhatsApp