diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index bd28029..ff281fd 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -2,25 +2,78 @@ namespace App\Http\Controllers; -use App\Route; -use App\Legislature; +use Illuminate\Support\Facades\Auth; use App\Http\Controllers\Controller; use Tymon\JWTAuth\Facades\JWTAuth; use Tymon\JWTAuth\Exceptions\JWTException; class AuthController extends Controller { + /** + * Create a new AuthController instance. + * + * @return void + */ + public function __construct() { + $this->middleware('auth:api', ['except' => ['login']]); + } - public function authenticate(\Illuminate\Http\Request $request) { - $credentials = $request->only('username', 'password'); // grab credentials from the request - try { - if (!$token = JWTAuth::attempt($credentials)) { // attempt to verify the credentials and create a token for the user - return response()->json(['error' => 'invalid_credentials'], 401); - } - } catch (JWTException $e) { - return response()->json(['error' => 'could_not_create_token'], 500); // something went wrong whilst attempting to encode the token + /** + * Get a JWT via given credentials. + * + * @return \Illuminate\Http\JsonResponse + */ + public function login() { + $credentials = request(['username', 'password']); + + if (!$token = auth()->attempt($credentials)) { + return response()->json(['error' => 'Unauthorized'], 401); } - return response()->json(['token' => "Bearer $token"]); + return $this->respondWithToken($token); + } + + /** + * Get the authenticated User. + * + * @return \Illuminate\Http\JsonResponse + */ + public function me() { + return response()->json(auth()->user()); + } + + /** + * Log the user out (Invalidate the token). + * + * @return \Illuminate\Http\JsonResponse + */ + public function logout() { + auth()->logout(); + + return response()->json(['message' => 'Successfully logged out']); + } + + /** + * Refresh a token. + * + * @return \Illuminate\Http\JsonResponse + */ + public function refresh() { + return $this->respondWithToken(auth()->refresh()); + } + + /** + * Get the token array structure. + * + * @param string $token + * + * @return \Illuminate\Http\JsonResponse + */ + protected function respondWithToken($token) { + return response()->json([ + 'access_token' => $token, + 'token_type' => 'bearer', + 'expires_in' => auth()->factory()->getTTL() * 60 + ]); } } ?> \ No newline at end of file diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php new file mode 100644 index 0000000..a3af7dd --- /dev/null +++ b/app/Http/Controllers/HomeController.php @@ -0,0 +1,28 @@ +middleware('auth'); + } + + /** + * Show the application dashboard. + * + * @return \Illuminate\Http\Response + */ + public function index() + { + return view('home'); + } +} diff --git a/app/User.php b/app/User.php index cbf0815..17a6b1b 100644 --- a/app/User.php +++ b/app/User.php @@ -2,9 +2,9 @@ namespace App; +use Tymon\JWTAuth\Contracts\JWTSubject; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; -use Tymon\JWTAuth\Contracts\JWTSubject; class User extends Authenticatable implements JWTSubject { use Notifiable; diff --git a/config/auth.php b/config/auth.php index 321aee8..f046234 100644 --- a/config/auth.php +++ b/config/auth.php @@ -14,7 +14,7 @@ return [ */ 'defaults' => [ - 'guard' => 'web', + 'guard' => 'api', 'passwords' => 'users', ], diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php new file mode 100644 index 0000000..d8437bf --- /dev/null +++ b/resources/views/home.blade.php @@ -0,0 +1,23 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
Dashboard
+ +
+ @if (session('status')) +
+ {{ session('status') }} +
+ @endif + + You are logged in! +
+
+
+
+
+@endsection diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php new file mode 100644 index 0000000..8846226 --- /dev/null +++ b/resources/views/layouts/app.blade.php @@ -0,0 +1,80 @@ + + + + + + + + + + + {{ config('app.name', 'Laravel') }} + + + + + +
+ + + @yield('content') +
+ + + + + diff --git a/routes/api.php b/routes/api.php index c641ca5..d0cc38d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,6 +13,12 @@ use Illuminate\Http\Request; | */ -Route::middleware('auth:api')->get('/user', function (Request $request) { - return $request->user(); -}); +Route::group([ + 'middleware' => 'api', + 'prefix' => 'auth' +], function ($router) { + Route::post('login', 'AuthController@login'); + Route::post('logout', 'AuthController@logout'); + Route::post('refresh', 'AuthController@refresh'); + Route::post('me', 'AuthController@me'); +}); \ No newline at end of file diff --git a/routes/class/user.php b/routes/class/user.php index 6f307f8..825c06b 100644 --- a/routes/class/user.php +++ b/routes/class/user.php @@ -10,15 +10,16 @@ */ // Auth -Route::post('/api/rpc/user/auth/isauth', 'UserController@isAuth'); -//Route::post('/api/rpc/user/auth/login', 'UserController@login'); -Route::post('/api/rpc/user/auth/login', 'AuthController@authenticate'); -Route::post('/api/rpc/user/auth/register', 'UserController@register'); -Route::post('/api/rpc/user/auth/logout', 'UserController@logout'); -Route::group(['middleware' => 'jwt'], function () { - // Protected routes - Route::resource('getUserPosts', 'BoardController'); -}); +/*Route::group([ + 'middleware' => 'api' +], function ($router) { + Route::post('/api/rpc/user/auth/login', 'AuthController@login'); + Route::post('/api/rpc/user/auth/logout', 'AuthController@logout'); + Route::post('/api/rpc/user/auth/refresh', 'AuthController@refresh'); + Route::post('/api/rpc/user/auth/me', 'AuthController@me'); +});*/ + +//Route::post('/api/rpc/user/auth/register', 'UserController@register'); // Owner Route::get('/api/rpc/user/owner/countownersofentry/{id}', 'UserController@countOwnersOfEntry');