Laravel 6.0.1自定义身份验证防护

时间:2019-09-10 07:52:04

标签: php laravel laravel-6

我正在使用名为“ students”的自定义身份验证保护来创建自定义登录控制器,但登录身份验证保护尝试时始终返回false。

class login extends Controller
{
    use AuthenticatesUsers;

    public function index()
    {
        return view('voters.Auth.login');
    }

    public function checklogin(Request $request)
    {
        $this->validate($request, [
            'votersid' => 'required',
            'passcode' => 'required|alphaNum|min:3'
        ]);

        $studentid = $request->get('votersid');
        $pass = $request->get('passcode');

        if (Auth::guard('student')->attempt(['Student_ID' => $studentid, 'passcode' => $pass])) {
            return redirect('login/success');
        }

        return back()
            ->withInput($request->all())
            ->with('error', 'Please check your Student ID / Passcode.');
    }

    public function successlogin()
    {
        if (Auth::guard('student')->check()) {
            return view('successlogin');
        }

        return redirect('/vote/login')
            ->with('error', 'Please Login first to authenticate.');
    }

    public function logout(Request $request)
    {
        Auth::logout();
        $request->session()->invalidate();

        return redirect('/');
    }
}

配置如下

'guards' => [ 
    'web' => [ 'driver' => 'session', 'provider' => 'users', ], 
    'student' => [ 'driver' => 'session', 'provider' => 'student', ],
    'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ],
 ],
...
'providers' => [ 
    'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ],
    'student' => [ 'driver' => 'eloquent', 'model' => App\Student::class, ], 
    // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], 

学生模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Student extends Authenticatable
{
    use Notifiable;


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'Student_ID', 'password', 'FirstName', 'LastName', 'MiddleName', 'GradeLvl',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }

    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmail);
    }

    /**
     * Get the name of the unique identifier for the user.
     *
     * @return string
     */
    public function getAuthIdentifierName()
    {
        // TODO: Implement getAuthIdentifierName() method.
    }

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        // TODO: Implement getAuthIdentifier() method.
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        // TODO: Implement getAuthPassword() method.
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        // TODO: Implement getRememberToken() method.
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param string $value
     * @return void
     */
    public function setRememberToken($value)
    {
        // TODO: Implement setRememberToken() method.
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        // TODO: Implement getRememberTokenName() method.
    }
}

期望使用自定义身份验证保护措施登录,但是当我输入该信息时,正确的保护措施总是返回false而不是true或成功登录。

0 个答案:

没有答案