AuthController.php 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Api\V1\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Tymon\JWTAuth\JWTAuth;
  6. use Exception;
  7. class AuthController extends Controller
  8. {
  9. protected $jwt;
  10. public function __construct(JWTAuth $jwt)
  11. {
  12. $this->middleware('api', ['except' => ['login']]);
  13. $this->jwt = $jwt;
  14. }
  15. public function login(Request $request)
  16. {
  17. if (! $token = $this->jwt->attempt($request->only('email', 'password'))) {
  18. return $this->returnData(-1,'不存在的用户');
  19. }
  20. return $this->respondWithToken($token);
  21. }
  22. public function me(Request $request){
  23. return $this->returnData(0,'ok',[],0,$this->jwt->user());
  24. }
  25. protected function respondWithToken($token)
  26. {
  27. return $this->returnData(0,'ok',[],0,[
  28. 'access_token' => $token,
  29. 'token_type' => 'bearer',
  30. 'expires_in' => $this->jwt->factory()->getTTL() * 60
  31. ]);
  32. }
  33. }