Как сделать вход по логину и email Laravel 5.2?

Здравствуйте! Как сделать вход и по логину и по email, в зависимости от того что вводит пользователь? Вход в laravel 5.2 по умолчанию через email, можно сделать по логину добавив в AuthController.php
protected $username = 'login';
Но тогда по email не входит.
  • Вопрос задан
  • 2267 просмотров
Решения вопроса 1
wielski
@wielski
✔ Совет: Вам помогли? Отметьте ответы решением.
Во первых, при валидации сделайте поле имени пользователя обязательным, и латиницей. Думаю, это логично.
Далее, в AuthController добавьте функцию:

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

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        $throttles = $this->isUsingThrottlesLoginsTrait();

        if ($throttles && $this->hasTooManyLoginAttempts($request)) {
            return $this->sendLockoutResponse($request);
        }

        $credentials = $this->getCredentials($request);
        $login = $request->input($this->loginUsername());
        $field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email':'username';

        $request->merge([$field => $login]);
        $credentials = $request->only($field, 'password');

        if (Auth::attempt($credentials, $request->has('remember'))) {
            return $this->handleUserWasAuthenticated($request, $throttles);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        if ($throttles) {
            $this->incrementLoginAttempts($request);
        }

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


UPD: Сделал более актуальный и правильный вариант благодаря ответу JhaoDa
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы