Yêu cầu thg 7 15, 2019 3:54 SA 245 0 2
  • 245 0 2
+1

hỏi về đăng nhập trong laravel

Chia sẻ
  • 245 0 2

em đang muốn làm về đăng nhập trong laravel,với mỗi id người dùng chỉ có thể đăng nhập và sửa thông tin của bản thân họ trong controller

public function post_login_check(Request $request){
        $arr =[
            'tutor_email' => $request->tutor_email,
            'tutor_password' => md5($request->tutor_password),
        ];

        if(Auth::guard('tutor')->attempt($arr))
        {
//            Session::put('tutor_id',$result->tutor_id);
            return redirect()->route('pages.registration');
        }else{
            return redirect()->route('pages.login')->with('message_err','bạn đã nhập sai tên đăng nhập hoặc mật khẩu');
        }

trong app\tutor

class Tutor extends Authenticatable implements MustVerifyEmail
{
    protected $table = "tutor";
    protected $primaryKey = "tutor_id";
    use Notifiable;

    protected $fillable=[
        'tutor_email','tutor_password','tutor_id',
    ];

    protected $hidden=[
        'tutor_password',
    ];

    protected $guard = 'tutor';
}

trong config\auth.php

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

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

        //xac thuc cho tutor
        'tutor' => [
            'driver' => 'session',
            'provider' => 'tutor',
        ],

        'tutor-api' => [
            'driver' => 'token',
            'provider' => 'tutor',
        ],
    ],

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

        'tutor' => [
            'driver' => 'eloquent',
            'model' => App\Tutor::class,
        ],

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

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

em bị lỗi này,mọi người giúp e với ạ

Avatar Hải Hà @HaiHaChan
thg 7 15, 2019 4:03 SA

Bạn sử dụng hàm attempt($arr), nó sẽ gọi tới hàm validateCredentials(), mà $arr truyền vào k có index là password kìa

thg 8 2, 2019 5:40 SA

@cq522138 Thớt đã resolved được vấn đề này chưa nhỉ?

2 CÂU TRẢ LỜI


Đã trả lời thg 7 15, 2019 4:02 SA
+1

Lỗi quá rõ ràng rồi mà bạn, bạn đang gửi request lên với 2 request name là tutor_email và tutor_password trong khi trong hàm validateCre khai bào 1 array có key là password. Trong form HTML sửa name từ tutor_password => password xem

Chia sẻ
Avatar Linh Vu @cq522138
thg 7 15, 2019 4:16 SA
thg 7 15, 2019 8:47 SA

Xịn quá @huusu1996 👏👏👏

Avatar Linh Vu @cq522138
thg 7 15, 2019 9:27 SA

mình xửa name trong html thành password,trong controler

        $arr =[
            'tutor_email' => $request->tutor_email,
            'password' => md5($request->password),
        ];
        if(Auth::guard('tutor')->attempt($arr))
        {
            return redirect()->route('pages.registration');
        }else{
            return redirect()->route('pages.login')->with('message_err','bạn đã nhập sai tên đăng nhập hoặc mật khẩu');
        }

mình dd($arr) thấy giống hệt trong csdl mà nó vẫn toàn vào else thì là lỗi gì ạ,giúp mình với

thg 8 2, 2019 5:37 SA

@cq522138 password là raw password thôi bạn ơi. Không cần md5, laravel sẽ tự compare hash mà.

Đã trả lời thg 7 15, 2019 5:09 SA
+1

Trong model b thêm hàm này xem sao:

public function getAuthPassword()
{
    return $this->tutor_password;
}
Chia sẻ
Avatar Linh Vu @cq522138
thg 7 15, 2019 9:20 SA

không được bạn ơi,vẫn bị lỗi ạ

Avatar No Naem @Naem
thg 7 15, 2019 9:53 SA

@cq522138 làm tương tự bạn này nhưng bạn thử bỏ md5() đi xem sao :v

Avatar Phạm Tuấn Anh @phamtuananh760
thg 7 15, 2019 10:10 SA

@cq522138 trong controller sửa như này xem

$arr =[
            'tutor_email' => $request->tutor_email,
            'password' => $request->tutor_password,
];
Avatar Linh Vu @cq522138
thg 7 15, 2019 10:15 SA

t sửa vậy nhưng code thì chạy nhưng nó sẽ chỉ chạy vào else trong controller, dd($arr) nó có dữ liệu đúng như trong csdl nhưng kết quả vẫn chỉ vào else

Avatar Phạm Tuấn Anh @phamtuananh760
thg 7 15, 2019 10:28 SA

@cq522138 bỏ md5 với mk ở trong csdl mã hóa rồi chứ

Avatar Linh Vu @cq522138
thg 7 15, 2019 10:43 SA

sửa rồi mà không được ạ

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí