Home > Software engineering >  Laravel Auth::attempt ans Hash::check not working
Laravel Auth::attempt ans Hash::check not working

Time:08-16

I try make rest API with Laravel 8 Sanctum. And my database is MySql Maria DB.

I create LoginController and make function call login. When i try my API, it's always return Unauthorized. I pretty sure my USERNAME and PASSWORD is correct.

This is my LoginController

     public function store(Request $request) {
        $user  = User::create(
            [
                "USERNAME" => $request->username,
                "PASSWORD" => Hash::make($request->password),
                "ADM_MST_SITE_ID" => 0,
            ]
        );

        $token = $user->createToken('apiToken')->plainTextToken;

        $res = [
            'user' => $user,
            'token' => $token
        ];
        return response($res, 201);
    }

   public function login(Request $request)
    {
        $data = $request->validate([
            'username' => 'required|string',
            'password' => 'required|string'
        ]);

        $user = User::where('username', $data['username'])->first();

        $credentials = request(['username', 'password']);
        if(!Auth::attempt($credentials))
        return response()->json([
            'message' => 'Unauthorized'
        ], 401);

            $token = $user->createToken('apiToken')->plainTextToken;

            $res = [
                'user' => $user,
                'token' => $token
            ];

            return response($res, 201);
    }

Model

////
  protected $table = 'adm_mst_user';
  protected $guarded = ['ID'];
  
  public function getAuthPassword()
    {
        return $this->PASSWORD;
    }
////

The store function is work well, the new data are inserted to my database. But, when i login with username and password, it's not working.

I try 2 different auth check, using Auth::attempt and Hash::check.

I don't know where the error coming from. It's always return Unauthorized.

CodePudding user response:

$user = User::where('username', $data['username'])->first();
$this->guard()->login($user);

and make a guard function in same controller

protected function guard()
{
    return Auth::guard();
}

import use Illuminate\Support\Facades\Auth; in top

CodePudding user response:

include

use Illuminate\Support\Facades\Hash;

CodePudding user response:

You need to make it with email not with username

$credentials = request(['email', 'password']);

OR, modify your attempt code

if(!Auth::attempt(['username' => $credentials['username'], 'password' => $credentials['password']))

CodePudding user response:

this code worked with sanctum

use App\Models\User;
use Illuminate\Support\Facades\Hash;

function login($candidate)
{
    $user = User::where('username', $candidate['username'])->first();
    if (!$user || !Hash::check($candidate['password'], $user->password)) {
        return [
            'message' => 'These credentials do not match our records.'
        ];
    }
    $token = $user->createToken('my-token')->plainTextToken;
    return [
        'user' => $user,
        'token' => $token
    ];
}
  • Related