본문 바로가기
Infomation

Laravel 10 Auth::attempt field define

by Happens 2023. 8. 30.
반응형

Laravel 10 Auth::attempt 를 쓸려고 보니까 잘 안된다..

xdebug로 까보니 인증쪽 credentials하는 부분에서 user가 null로 넘어온다.. 

뭐여... 

User 모델에 있는 인증 소스와 똑같이 Extends로 선언하고

getAuthPassword에 필드이름을 적어준다.

https://github.com/laravel/laravel/blob/10.x/app/Models/User.php

<?php

namespace App\Models;

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

class User extends Authenticatable
{
    use Notifiable, HasFactory;

    protected $hidden = [
        'Password',
    ];
    
    public function getAuthPassword() {
        return $this->Password;
    }
}

기본 필드는 password로 선언되어있다. 

attempt 선언시 첫번째 필드에 인증에 사용될 ID 필드를 선언하고 

두번째 필드는 기존대로 password로 넘겨준다.

        $credentials = ['ID' => $this->email, 'password'=> $this->password];
        // dd($credentials);
        if(Auth::attempt($credentials)) {

            return redirect()->route('counter');

        }

잘된다.

반응형