Need some help with laravel.
I have this in APIController
public function login(Request $request)
{
$credentials = [
'email' => $request->email,
'password' => $request->password
];
if (auth()->attempt($credentials)) {
$token = auth()->user()->createToken('myToken')->accessToken;
return response()->json(['token' => $token], 200);
} else {
return response()->json(['error' => 'UnAuthorised'], 401);
}
}
When i login from postman, i got unauthorized. But when login from apps i failed, it return
GuzzleHttp \ Exception \ ClientException (401) Client error:
POST https://api.mydomain.com/api/login
resulted in a401 Unauthorized
response: {"error":"UnAuthorised"}
code that call login
$group = 'all';
$start_date = $request->start_date;
$end_date = $request->end_date;
$date1 = new DateTime($start_date);
$date2 = new DateTime($end_date);
$interval = $date1->diff($date2);
$count_days = $interval->days;
$health_care = Auth::user()->health_care_id;
$request->validate([
'start_date' => 'required',
'end_date' => 'required',
]);
$client = new Client();
$token = 'some-token-i-got-from-postman-login-result';
$client = new \GuzzleHttp\Client();
$cookieJar = new \GuzzleHttp\Cookie\CookieJar();
$response = $client->post('https://api.mydoman.com/api/login', [
'form_params' => [
'email' => 'name@mydoman.com',
'password' => 'mypassword',
'action' => 'login',
],
'cookies' => $cookieJar,
]);
my composer.json
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.2",
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/passport": "^8.0",
"laravel/tinker": "^1.0"
},
"require-dev": {
"facade/ignition": "^1.4",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
I run both front and web API on the same server.
Build on top of laragon(Apache and php 7.2)
Please help
Most likely this is environment issue, because my API works well in Live server. So i ended cloning my LIVE API into LIVE DEV-API. The cause might be because on local i run on windows but on LIVE i run on linux, so i think i need to do some adjustment to make it work on windows. So the cause still not solved, but at least i know it some environment problem. Thx you guys for your help and hope this help others
other solution: run with "php artisan serve" instead using webserver