Add remote login

This commit is contained in:
philipp lang 2024-02-22 00:03:11 +01:00
parent 8cd2f74477
commit 0bc00207b2
4 changed files with 67 additions and 0 deletions

View File

@ -57,5 +57,8 @@ class RouteServiceProvider extends ServiceProvider
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
Route::middleware('api')
->prefix('remote')
->group(base_path('routes/remote.php'));
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Remote\Actions;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Crypt;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Authentication\Auth;
class LoginAction
{
use AsAction;
/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'mglnr' => 'required|numeric',
'password' => 'required|string',
];
}
public function handle(ActionRequest $request): JsonResponse
{
Auth::login($request->mglnr, $request->password);
return response()->json([
'access_token' => Crypt::encryptString(json_encode(['mglnr' => $request->mglnr, 'password' => $request->password])),
]);
}
}

6
routes/remote.php Normal file
View File

@ -0,0 +1,6 @@
<?php
use App\Contribution\Actions\GenerateApiAction as ContributionGenerateApiAction;
use App\Remote\Actions\LoginAction;
Route::post('/nami/token', LoginAction::class)->name('remote.nami.token');

View File

@ -0,0 +1,23 @@
<?php
namespace Tests\Feature\Nami;
use Illuminate\Support\Facades\Crypt;
use Tests\EndToEndTestCase;
use Zoomyboy\LaravelNami\Authentication\Auth;
class LoginTest extends EndToEndTestCase
{
public function testItCanLoginRemotelyWithUser(): void
{
Auth::success(90100, 'secret');
$response = $this->postJson(route('remote.nami.token'), [
'mglnr' => 90100,
'password' => 'secret',
]);
$response->assertOk();
$accessTokenPayload = Crypt::decryptString($response->json('access_token'));
$this->assertEquals(['mglnr' => 90100, 'password' => 'secret'], json_decode($accessTokenPayload, true));
}
}