-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Hash; | ||
|
||
class AuthController extends Controller | ||
{ | ||
/** | ||
* Get a JWT via given credentials. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function login(Request $request) | ||
{ | ||
$credentials = $request->only(['email', 'password']); | ||
// response()->json($credentials); | ||
// if (!isset($credentials['password'])) { | ||
// return response()->json(['error' => 'Unauthorized'], 401); | ||
// } | ||
// $credentials['password'] = Hash::make($credentials['password']); | ||
|
||
if (! $token = auth('api')->attempt($credentials)) { | ||
return response()->json(['error' => 'Unauthorized'], 401); | ||
} | ||
|
||
return $this->respondWithToken($token); | ||
} | ||
|
||
/** | ||
* Get the authenticated User. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function me() | ||
{ | ||
return response()->json(auth('api')->user()); | ||
} | ||
|
||
/** | ||
* Log the user out (Invalidate the token). | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function logout() | ||
{ | ||
auth('api')->logout(); | ||
|
||
return response()->json(['message' => 'Successfully logged out']); | ||
} | ||
|
||
/** | ||
* Refresh a token. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function refresh() | ||
{ | ||
return $this->respondWithToken(auth('api')->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('api')->factory()->getTTL() * 60 | ||
]); | ||
} | ||
} |