8000 [Bugfix] Fix sending JSON API responses outside of a JSON API · tekord/laravel-json-api@bf91774 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 17, 2023. It is now read-only.

Commit bf91774

Browse files
committed
[Bugfix] Fix sending JSON API responses outside of a JSON API
1 parent 9ee72ab commit bf91774

File tree

7 files changed

+134
-2
lines changed

7 files changed

+134
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ behaviour can be maintained by setting the `by-resource` config option to the st
3131
- [#176](https://github.com/cloudcreativity/laravel-json-api/issues/176)
3232
Do not import model class in Eloquent adapter stub to avoid collisions with class name when using the legacy
3333
*not-by-resource* behaviour.
34+
- An exception is no longer triggered when create JSON API responses when there is no booted JSON API handling
35+
the request.
3436

3537
## [1.0.0-alpha.2] - 2018-05-06
3638

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@
2626
<directory suffix=".php">src/</directory>
2727
</whitelist>
2828
</filter>
29+
<php>
30+
<env name="APP_KEY" value="base64:BMfTqJC1cFk6A/jTPsjQgC+cROx7TDaEeGIAat6CuqY="/>
31+
</php>
2932
</phpunit>

src/Services/JsonApiService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,22 @@ public function api($apiName = 'default')
6969
}
7070

7171
/**
72-
* Get the JSON API request, if there is one.
72+
* Get the JSON API request, if there is an inbound API handling the request.
7373
*
7474
* @return RequestInterface|null
7575
*/
7676
public function request()
7777
{
78-
if (!$this->container->bound('json-api.request')) {
78+
if (!$this->container->bound(Api::class)) {
7979
return null;
8080
}
8181

8282
return $this->container->make('json-api.request');
8383
}
8484

8585
/**
86+
* Get the inbound JSON API request.
87+
*
8688
* @return RequestInterface
8789
*/
8890
public function requestOrFail()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace DummyApp\Http\Controllers\Auth;
19+
20+
use CloudCreativity\LaravelJsonApi\Http\Controllers\CreatesResponses;
21+
use CloudCreativity\LaravelJsonApi\Utils\Helpers;
22+
use Illuminate\Foundation\Validation\ValidatesRequests;
23+
use Illuminate\Http\Request;
24+
use Illuminate\Http\Response;
25+
use Illuminate\Routing\Controller;
26+
use Illuminate\Foundation\Auth\AuthenticatesUsers;
27+
28+
class LoginController extends Controller
29+
{
30+
/*
31+
|--------------------------------------------------------------------------
32+
| Login Controller
33+
|--------------------------------------------------------------------------
34+
|
35+
| This controller handles authenticating users for the application and
36+
| redirecting them to your home screen. The controller uses a trait
37+
| to conveniently provide its functionality to your applications.
38+
|
39+
*/
40+
41+
use AuthenticatesUsers, CreatesResponses, ValidatesRequests;
42+
43+
/**
44+
* Where to redirect users after login.
45+
*
46+
* @var string
47+
*/
48+
protected $redirectTo = '/home';
49+
50+
/**
51+
* @var string
52+
*/
53+
protected $api = 'default';
54+
55+
/**
56+
* Create a new controller instance.
57+
*
58+
* @return void
59+
*/
60+
public function __construct()
61+
{
62+
$this->middleware('guest')->except('logout');
63+
}
64+
65+
/**
66+
* @param Request $request
67+
* @param $user
68+
* @return Response|null
69+
*/
70+
protected function authenticated(Request $request, $user)
71+
{
72+
if (Helpers::wantsJsonApi($request)) {
73+
return $this->reply()->content($user);
74+
}
75+
76+
return null;
77+
}
78+
}

tests/dummy/routes/json-api.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
use CloudCreativity\LaravelJsonApi\Facades\JsonApi;
1919
use CloudCreativity\LaravelJsonApi\Routing\ApiGroup;
20+
use Illuminate\Support\Facades\Auth;
21+
use Illuminate\Support\Facades\Route;
22+
23+
Route::group(['middleware' => 'web'], function () {
24+
Auth::routes();
25+
});
2026

2127
JsonApi::register('default', [], function (ApiGroup $api) {
2228
$api->resource('comments', [
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace CloudCreativity\LaravelJsonApi\Tests\Integration\Auth;
4+
5+
use CloudCreativity\LaravelJsonApi\Tests\Integration\TestCase;
6+
use DummyApp\User;
7+
8+
class LoginTest extends TestCase
9+
{
10+
11+
public function testLogin()
12+
{
13+
$user = factory(User::class)->create([
14+
'password' => bcrypt('secret'),
15+
]);
16+
17+
$credentials = ['email' => $user->email, 'password' => 'secret'];
18+
19+
$expected = [
20+
'data' => [
21+
'type' => 'users',
22+
'id' => $user->getKey(),
23+
'attributes' => [
24+
'name' => $user->name,
25+
],
26+
],
27+
];
28+
29+
$this->postJson('/login', $credentials, ['Accept' => 'application/vnd.api+json'])
30+
->assertSuccessful()
31+
->assertJson($expected);
32+
}
33+
}

tests/lib/Integration/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ protected function getPackageAliases($app)
8484
];
8585
}
8686

87+
/**
88+
* @param Application $app
89+
*/
90+
protected function getEnvironmentSetUp($app)
91+
{
92+
$app['config']->set('auth.providers.users.model', DummyApp\User::class);
93+
}
94+
8795
/**
8896
* @param Application $app
8997
*/

0 commit comments

Comments
 (0)
0