Multi-Auth в laravel 6 (вход в систему с использованием нескольких таблиц, но на одной странице входа) - PullRequest
0 голосов
/ 26 января 2020

Я видел много постов об этой проблеме с множественной аутентификацией, но ни одна из них не помогает мне.

Итак, вот мой код,

<?php

namespace App\Traits\Auth;

use App\Http\Requests\LoginRequest;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

trait LoginTraits {
    use ThrottlesLogins;

    public function login(LoginRequest $request)
    {
        // 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.
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }
        //dd(Auth::guard()->attempt($this->credentials($request), $request->filled('remember')) == true);
        if(Auth::guard()->attempt($this->studentcredentials($request), $request->filled('remember')) == true)
        {
            $request->session()->regenerate();
            $this->clearLoginAttempts($request);
            return redirect()->intended('dashboard/student');

        }else{
            $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
        }

        if(Auth::guard('staff')->attempt($this->credentials($request), $request->filled('remember')) == true)
        {
            $request->session()->regenerate();
            $this->clearLoginAttempts($request);
            return redirect()->intended('dashboard/staff');
        }else{
            $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
        }
        /* if(Auth::guard('admin')->attempt($this->credentials($request), $request->filled('remember')))
        {
            $request->session()->regenerate();

            $this->clearLoginAttempts($request);

            return redirect()->intended('dashboard/admin');
        } */
    }

    public function studentcredentials(LoginRequest $request)
    {
       return $request->only($this->username() , 'password');

       /* if(is_numeric($request->only('email'))){
        return $request->only('mobile', 'password');
      }
      elseif (filter_var($request->only('email'), FILTER_VALIDATE_EMAIL)) {
        return $request->only('email', 'password');
      }
      return $request->only('assign_id', 'password'); */
    }

    public function credentials(LoginRequest $request)
    {
        return $request->only($this->username() , 'password');
    }

    /**
     * Get the failed login response instance.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function sendFailedLoginResponse(LoginRequest $request)
    {
        throw ValidationException::withMessages([
            $this->username() => [trans('auth.failed')],
        ]);
    }

    public function logout(HttpRequest $request)
    {
        $this->guard()->logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect('/');
    }

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

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    public function guard()
    {

        return Auth::guard();
    }
}

Код выше контроллер входа в систему

Итак, в основном это страница входа в систему, с которой я хочу аутентифицировать студентов и сотрудников с помощью электронной почты, мобильного или уникального_идентификатора. есть две разные таблицы, в каждой из которых есть пароли, электронные письма и все детали.

Я создал стражу в аутентификации. php страница.

...