Skip to content

Commit

Permalink
Merge 05ab154 into 78b12cc
Browse files Browse the repository at this point in the history
  • Loading branch information
kresnasatya committed Aug 24, 2019
2 parents 78b12cc + 05ab154 commit 9560912
Show file tree
Hide file tree
Showing 15 changed files with 1,047 additions and 76 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

PASSPORT_CLIENT_ID=
PASSPORT_CLIENT_SECRET=
47 changes: 47 additions & 0 deletions app/Http/Controllers/Api/Auth/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Http\Controllers\Api\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
public function login(Request $request)
{
$http = new \GuzzleHttp\Client;

try {
$response = $http->post(url('/oauth/token'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('services.passport.client_id'),
'client_secret' => config('services.passport.client_secret'),
'username' => $request->emil,
'password' => $request->password,
]
]);

return $response->getBody();
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
if ($e->getCode() === 400) {
return response()->json('Invalid request. Please input an email or a password', $e->getCode());
} else if ($e->getCode() === 401) {
return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
}

return response()->json('Something wrong on the server', $e->getCode());
}
}

public function logout()
{
auth()->user()->tokens->each(function ($token, $key) {
$token->revoke();
});

return response()->json('Logged out successfully', 200);
}
}
6 changes: 5 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -27,6 +28,9 @@ public function boot()
{
$this->registerPolicies();

//
// Register passport routes
Passport::routes(function ($router) {
$router->forAccessTokens();
});
}
}
3 changes: 2 additions & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
use Notifiable;
use Notifiable, HasApiTokens;

/**
* The attributes that are mass assignable.
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"php": "^7.1.3",
"backup-manager/laravel": "^1.3",
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "5.8.*",
"laravel/passport": "^7.3",
"laravel/tinker": "^1.0",
"luthfi/formfield": "^1.0",
"riverskies/laravel-mobile-detect": "^1.3"
Expand Down
Loading

0 comments on commit 9560912

Please sign in to comment.