Skip to content

Instantly share code, notes, and snippets.

@bogordesaincom
Last active October 19, 2022 03:35
Show Gist options
  • Select an option

  • Save bogordesaincom/62b9f17064db9fd1a64bde6fcfc3a847 to your computer and use it in GitHub Desktop.

Select an option

Save bogordesaincom/62b9f17064db9fd1a64bde6fcfc3a847 to your computer and use it in GitHub Desktop.
Sanctum Api Token
Route::group(['middleware' => ['auth:sanctum']], function () {
// Product Group
Route::apiResource('brands', BrandsController::class);
});
<?php
return [
'paths' => ['api/*'],
'allowed_methods' => ['*'],
'allowed_origins' => ['https://frontend.test', 'http://localhost:3000'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
];
'api' => [
// ini dicomment
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Spatie\ResponseCache\Middlewares\CacheResponse::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
public function store(LoginRequest $request): JsonResponse
{
if (! Auth::attempt($request->only('email', 'password'))) {
return responder()->error('unauthenticated')->respond();
}
$user = User::where('email', $request['email'])->firstOrFail();
$token = $user->createToken('auth_token')->plainTextToken;
return responder()->success(['token' => $token])->respond();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment