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') +