Skip to content

Commit

Permalink
Resolves laravel-portugal#37 - A guest should be able to login and lo…
Browse files Browse the repository at this point in the history
…gout (Request Changed)
  • Loading branch information
dleicam committed Oct 24, 2020
1 parent cbc1bc3 commit f42f58f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
12 changes: 7 additions & 5 deletions domains/Accounts/Controllers/AccountsLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Domains\Accounts\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Validation\ValidationException;


class AccountsLoginController extends Controller
Expand All @@ -19,14 +20,15 @@ public function __construct(Auth $auth)

public function __invoke(Request $request): Response
{
$credentials = $this->validate($request, [
$credentials = $this->validate($request, [
'email' => ['required', 'email'],
'password' => ['required', 'string'],
]);

if (!$token = $this->auth->attempt($credentials)) {
return new Response('', Response::HTTP_UNPROCESSABLE_ENTITY);
}
if(!$token = $this->auth->attempt($credentials))
return new Response([
'message' => 'Credentials are incorrect or user doesn\'t exist',
], Response::HTTP_UNPROCESSABLE_ENTITY);

return new Response([
'access_token' => $token,
Expand Down
14 changes: 3 additions & 11 deletions domains/Accounts/Controllers/AccountsProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@

use App\Http\Controllers\Controller;
use Domains\Accounts\Resources\UserResource;
use Illuminate\Http\Response;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Http\Request;

class AccountsProfileController extends Controller
{
protected $auth;

public function __construct(Auth $auth)
{
$this->auth = $auth;
}

public function __invoke(): Response
public function __invoke(Request $request): UserResource
{
return new Response(UserResource::make($this->auth->user()), Response::HTTP_OK);
return new UserResource($request->user());
}
}
1 change: 0 additions & 1 deletion domains/Accounts/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function __construct(Auth $auth)

public function handle($request, Closure $next, $guard = null)
{

if (!$this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}
Expand Down
6 changes: 3 additions & 3 deletions domains/Accounts/Tests/Feature/AccountsProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function guest_cannot_see_profile(): void
/** @test */
public function authenticated_user_can_see_profile(): void
{
$token = auth()->login($this->user);
$this->get(route('accounts.me'), ['Authorization' => "Bearer {$token}"])
$this->actingAs($this->user)
->get(route('accounts.me'))
->seeJson([
"id" => $this->user->id,
"name" => $this->user->name,
Expand All @@ -42,6 +42,6 @@ public function authenticated_user_can_see_profile(): void
"updated_at" => $this->user->updated_at,
"deleted_at" => $this->user->deleted_at
])
->assertResponseStatus(Response::HTTP_OK);
->assertResponseStatus(Response::HTTP_CREATED);
}
}

0 comments on commit f42f58f

Please sign in to comment.