Laravel 5.8多重身份验证注册和登录

时间:2019-06-02 15:48:44

标签: php laravel

我已经创建了Student表用于注册学生,并让他们在注册后直接登录...但是似乎数据已插入到表中,但是他们无法重定向到他们的页面,甚至无法登录

我尝试过使用学生卫士并向注册控制器提供功能,该控制器将在注册后重定向他们,但无济于事。...laravel中还是新的东西

/ *我的注册码* /

/* my register controller */
    <?php

    namespace App\Http\Controllers\Auth;

    use App\User;
    use App\Student;
    use App\Http\Controllers\Controller;
    use Illuminate\Support\Facades\Hash;
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Foundation\Auth\RegistersUsers;
    use Illuminate\Http\Request;

    class RegisterController extends Controller
    {
        /*
        |--------------------------------------------------------------------------
        | Register Controller
        |--------------------------------------------------------------------------
        |
        | This controller handles the registration of new users as well as their
        | validation and creation. By default this controller uses a trait to
        | provide this functionality without requiring any additional code.
        |
        */

        use RegistersUsers;

        /**
         * Where to redirect users after registration.
         *
         * @var string
         */
        protected $redirectTo = '/home';

        /**
         * Create a new controller instance.
         *
         * @return void
         */
        // public function __construct()
        // {
        //     $this->middleware('guest');
        // }

          public function __construct()
        {
            $this->middleware('guest');
            $this->middleware('guest:student');
        }

        /**
         * Get a validator for an incoming registration request.
         *
         * @param  array  $data
         * @return \Illuminate\Contracts\Validation\Validator
         */
        protected function validator(array $data)
        {
            return Validator::make($data, [
                'name' => ['required', 'string', 'max:255'],
                'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
                'password' => ['required', 'string', 'min:8', 'confirmed'],
            ]);
        }

        /**
         * Create a new user instance after a valid registration.
         *
         * @param  array  $data
         * @return \App\User
         */
        protected function create(array $data)
        {
            return User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'role_id' => $data['role_id'],
                'password' => Hash::make($data['password']),
            ]);
        }

        public function showStudentRegisterForm()
        {
            return view('auth.register', ['url' => 'student']);
        }

         protected function createStudent(Request $request)
        {
            // dd($request);
            // $this->validator($request->all())->validate();
            $student = Student::create([
                'fname' => $request['fname'],
                'lname' => $request['lname'],
                'email' => $request['email'],
                'role_id' => $request['role_id'],
                'password' => Hash::make($request['password']),
            ]);
            return redirect()->intended('/home');
        }
    }


    /* My Login controller */

    <?php

    namespace App\Http\Controllers\Auth;

    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;

    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->middleware('guest:student')->except('logout');
        }

         public function showStudentLoginForm()
        {
            return view('auth.login', ['url' => 'student']);
        }

        public function studentLogin(Request $request)
        {
            $this->validate($request, [
                'email'   => 'required|email',
                'password' => 'required|min:6'
            ]);

            if (Auth::guard('student')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {

                return redirect()->intended('/home');
            }
            return back()->withInput($request->only('email', 'remember'));
        }
    }



    /* redirect if authenticated */

    <?php

    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 ($guard == "student" && Auth::guard($guard)->check()) {
                    return redirect('/home');
                }


            if (Auth::guard($guard)->check()) {
                return redirect('/home');
            }


            return $next($request);
        }


    }


    /* my routes */


    Route::get('/login/student', 'Auth\LoginController@showStudentLoginForm');
    Route::get('/register/student', 'Auth\RegisterController@showStudentRegisterForm');
    Route::post('/login/student', 'Auth\LoginController@StudentLogin');
    Route::post('/register/student', 'Auth\RegisterController@createStudent');


    /* my guards */


    <?php

    return [

        /*
        |--------------------------------------------------------------------------
        | Authentication Defaults
        |--------------------------------------------------------------------------
        |
        | This option controls the default authentication "guard" and password
        | reset options for your application. You may change these defaults
        | as required, but they're a perfect start for most applications.
        |
        */

        'defaults' => [
            'guard' => 'web',
            'passwords' => 'users',
        ],

        /*
        |--------------------------------------------------------------------------
        | Authentication Guards
        |--------------------------------------------------------------------------
        |
        | Next, you may define every authentication guard for your application.
        | Of course, a great default configuration has been defined for you
        | here which uses session storage and the Eloquent user provider.
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | Supported: "session", "token"
        |
        */

        'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],

            'api' => [
                'driver' => 'token',
                'provider' => 'users',
                'hash' => false,
            ],

            'student' => [
                'driver' => 'session',
                'provider' => 'students',
            ],
        ],

        /*
        |--------------------------------------------------------------------------
        | User Providers
        |--------------------------------------------------------------------------
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | If you have multiple user tables or models you may configure multiple
        | sources which represent each model / table. These sources may then
        | be assigned to any extra authentication guards you have defined.
        |
        | Supported: "database", "eloquent"
        |
        */

        'providers' => [
            'users' => [
                'driver' => 'eloquent',
                'model' => App\User::class,
            ],

            'students' => [
                'driver' => 'eloquent',
                'model' => App\Student::class,
            ],

            // 'users' => [
            //     'driver' => 'database',
            //     'table' => 'users',
            // ],
        ],

        /*
        |--------------------------------------------------------------------------
        | Resetting Passwords
        |--------------------------------------------------------------------------
        |
        | You may specify multiple password reset configurations if you have more
        | than one user table or model in the application and you want to have
        | separate password reset settings based on the specific user types.
        |
        | The expire time is the number of minutes that the reset token should be
        | considered valid. This security feature keeps tokens short-lived so
        | they have less time to be guessed. You may change this as needed.
        |
        */

        'passwords' => [
            'users' => [
                'provider' => 'users',
                'table' => 'password_resets',
                'expire' => 60,
            ],
        ],

    ];

1 个答案:

答案 0 :(得分:0)

您应该在注册后登录该学生,这就是您的代码的样子

protected function createStudent(Request $request)
    {

        $student = Student::create([
            'fname' => $request['fname'],
            'lname' => $request['lname'],
            'email' => $request['email'],
            'role_id' => $request['role_id'],
            'password' => Hash::make($request['password']),
        ]);

      //After registration login the use then redirect
       if (Auth::guard('student')
                 ->attempt(['email' => $request->email, 'password' => $request->password])) {

            return redirect()->intended('/home');
        }

    }