A
A
Alexander Koregin2019-12-04 00:48:27
Laravel
Alexander Koregin, 2019-12-04 00:48:27

Why does attempt return false?

I encountered an error during authentication: when trying to pass it, the Auth::attempt method returns false, although the phone and password are correct.
I think the point is that in my table password is called as passhash.
Table - User
hv0J9.png
Registration class:

protected function create(Request $data)
{
    if (User::where('phone', '=', $data['phone'])->exists()) {
        return response()->json(array('status' => 'error', 'msg'=>'The phone number already exists'), 422);
    } else {
        $user_id = User::create([
            'first_name' => $data['first_name'],  
            'surname' => $data['surname'],
            'phone' => $data['phone'],
            'passhash' => bcrypt($data['password']),
        ]);
        return response()->json(array('status' => 'created', 'id' => $user_id['id']), 201);
    }
}

Authentication class
public function login(Request $request)
{
    $credentials = [
        'phone' => $request['phone'],
        'password' => $request['password'],
    ];

    if (Auth::attempt($credentials)) {
        return response()->json(array('token' => 'Вы успешно авторизировались!'), 200);
    } else {
        return response()->json([
            'error' => 'Incorrect phone or password',
        ], 404);
    }
}

public function getAuthPassword()
{
    return $this->passhash;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2019-12-04
@New_Horizons

'passhash' => \Hash::make($data['password']),
try like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question