Summer Sale! All accounts are 50% off this week.

abkrim's avatar
Level 13

Validation API Login fail in tests, working in real life

I've and App with API. In this app, there're a route for login via API, and get a new token for use with api midddleware auth.

I've tested with Postman and work fine

{
    "success": true,
    "data": {
        "token": "3|TSYAMjT3MOhUh8A4M8e2thOwv89jvEjoUP279z56",
        "name": "Admin"
    },
    "message": "User signed in"
}

When try to test

function call_api_login_return_token()
    {
        $password = bcrypt('password'); // Also tried Hass::make('password')
        $user = User::factory()->create(['password' => $password]);

        $params = [
            'email' => $user->email,
            'password' => $password
        ];

        $response = $this->postJson('api/login', $params, ['Accept' => 'application/json']);

        $response->dump();
    }

Get error Unauthorised

{#1749
  +"success": false
  +"message": "Unauthorised"
  +"data": {#1752
    +"error": "Unauthorised"
  }
}

I've tried check database test (deactivate refresh) and user is correct.

0 likes
7 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60
$password = bcrypt('password');

$params = [
   'email' => $user->email,
   'password' => $password,
];

$this->postJson('api/login', $params)

You have to send unhashed password, something like

$params = [
   'email' => $user->email,
   'password' => 'password',
];
1 like
tykus's avatar

You are trying to login with the hashed password, rather than the unhashed (original) value:

$params = [
     'email' => $user->email,
    // 'password' => $password
    'password' => 'password',
];
2 likes
abkrim's avatar
Level 13

Sometimes, after 6 hours, being in front of the screen one does not see the obvious

A lot of thanks @sergiu17 and @tykus

Sergiu17's avatar

Thanks @tykus , shouldn't be worried about this

Liked 10 replies, so you have 500 points too ))

Please or to participate in this conversation.