M
M
Maxim2020-02-18 22:36:11
Laravel
Maxim, 2020-02-18 22:36:11

How to make authorization by login and email?

How to make it possible to log in both by login and by email?
laravel6.x

use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::HOME;

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2020-02-18
@Kostik_1993

Did you throw this piece of code here as an example of "I tried, but it didn't work" ?
Well, let's spread our brains together. You have the AuthenticatesUsers
trait . You need to copy the username method from it to your controller. Now he is like this

public function username()
    {
        return 'email';
    }

Your task is to check what you received from the form, login or email
. For this, the filter_var($string, FILTER_VALIDATE_EMAIL) method is suitable for you, which is already in PHP
$string is what you sent from the form, depending on what it is, you must return the name by which field to search for a user in the table - email or name
public function username()
    {
        // вот тут ты должен выполнить проверку что пришло и в зависимости от этого сделать return название столбца email или name
    }

Further myself!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question