'Is there a conflict between GuzzleHttp and Laravel Sanctum?
I have two laravel projects. The first one is an API which works perfectly. This is my login method:
/**
* Login api
*
* @return JsonResponse
*/
public function login( LoginRequest $request ) : JsonResponse
{
$remember = $request->has( 'remember' );
if( Auth::attempt( [
'username' => $request->username,
'password' => $request->password,
], $remember )
){
$user = Auth::user();
$user->tokens()->delete();
$success = [
'token' => $user->createToken( config( 'app.name' ) )->plainTextToken,
'user' => new UserResource( $user ),
];
return $this->sendResponse( $success, __( 'users.auth.login-succesful' ) );
}
else{
return $this->sendError( __( 'users.auth.login-failed' ), [], 401 );
}
}
When I use postman to verify, everything is peachy, I send this POST request to the login page:
{
"username" : "someones_username",
"password" : "**secure_pass123",
"remember" : true
}
And I get this response from the API (which is the expected one):
{
"success": true,
"data": {
"token": "1|FH2T3Hrv1zEWu4nVDFx6jCjTV5oY2yJUQfNKgexT",
"user": {
"username": "someones_username",
"person": {
"name": "User",
"last": "Lastname",
"slug": "user-lastname",
"document_number": "00000000",
"document_type": {
"id": 1,
"slug": "id-card",
"short": "ic",
"name": "Identification Card",
}
},
"permissions": []
}
},
"message": "users.auth.login-succesful"
}
Now, when I request the Login URL from my Admin project, I get a 500 error message. But it is really weird, because the error starts with this:
[2022-03-16 05:29:53] local.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.deleted_at' in 'where clause' (SQL: select * from `users` where `users`.`deleted_at` is null limit 1) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.deleted_at' in 'where clause' (SQL: select * from `users` where `users`.`deleted_at` is null limit 1) at C:\\webdev\\projects\\api\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:716)
Of course the column exists. I tried dumping the LoginRequest $request and it has all the data. So I don't really know what is going on. The .env is correct, it is referencing the correct database.
I am using Laravel Sanctum in my API and GuzzleHttp in my Admin.
I have activated the SoftDeletes trait in my Model,but the deleted_at column does exist in my migrations. Here is the up method in my CreateUsersTable:
public function up()
{
Schema::create( 'users', function( Blueprint $table ){
$table->id();
$table->string( 'username' )->unique();
$table->string( 'password' );
$table->rememberToken();
$table->json( 'data' )->nullable();
$table->foreignId( 'person_id' )->constrained( 'people' );
$table->timestamp( 'email_verified_at' )->nullable();
$table->timestamp( 'deactivated_at' )->nullable();
$table->softDeletes();
$table->timestamp( 'created_at' )->useCurrent();
$table->timestamp( 'updated_at' )->useCurrent()->useCurrentOnUpdate();
} );
}
And my User class goes like this:
class User extends Authenticatable
{
use Activable, HasData, HasApiTokens, HasFactory, HasRoles, Notifiable, SoftDeletes;
...
...
}
The LoginController in my Admin:
/**
* Tries to login the user
*
*/
public function login( Request $request )
{
$params = request( [ 'username', 'password', 'remember', ] );
return $this->requestURL( 'login', 'POST', $params, [
'Content-Type' => 'application/json',
] );
}
public function requestURL(
string $url, string $method = 'POST', array $params = [], array $headers = []
)
{
$client = new Client(); //GuzzleHttp\Client
$url = config( 'superveci.api' ) . '/' . $url;
$headers = $this->makeHeaders( $headers );
return $client->request( $method, $url, [
'json' => $params,
'headers' => $headers,
'verify' => false,
] )->getBody();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
