daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Dec 17th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.     namespace App\Http\Controllers;
  4.  
  5.     use App\User;
  6.     use Illuminate\Http\Request;
  7.     use Illuminate\Support\Facades\Hash;
  8.     use Illuminate\Support\Facades\Validator;
  9.     use JWTAuth;
  10.     use Tymon\JWTAuth\Exceptions\JWTException;
  11.  
  12.     class UserController extends Controller
  13.     {
  14.         public function authenticate(Request $request)
  15.         {
  16.             $credentials = $request->only('email', 'password');
  17.  
  18.             try {
  19.                 if (! $token = JWTAuth::attempt($credentials)) {
  20.                     return response()->json(['error' => 'invalid_credentials'], 400);
  21.                 }
  22.             } catch (JWTException $e) {
  23.                 return response()->json(['error' => 'could_not_create_token'], 500);
  24.             }
  25.             $user = auth()->user();
  26.             return response()->json(compact('user', 'token'));
  27.         }
  28.  
  29.         public function register(Request $request)
  30.         {
  31.                 $validator = Validator::make($request->all(), [
  32.                 'name' => 'required|string|max:255',
  33.                 'email' => 'required|string|email|max:255|unique:users',
  34.                 'password' => 'required|string|min:6|confirmed',
  35.             ]);
  36.  
  37.             if($validator->fails()){
  38.                     return response()->json($validator->errors()->toJson(), 400);
  39.             }
  40.  
  41.             $user = User::create([
  42.                 'name' => $request->get('name'),
  43.                 'email' => $request->get('email'),
  44.                 'password' => Hash::make($request->get('password')),
  45.             ]);
  46.  
  47.             $token = JWTAuth::fromUser($user);
  48.  
  49.             return response()->json(compact('user','token'),201);
  50.         }
  51.  
  52.         public function getAuthenticatedUser()
  53.         {
  54.             try {
  55.  
  56.                     if (! $user = JWTAuth::parseToken()->authenticate()) {
  57.                             return response()->json(['user_not_found'], 404);
  58.                     }
  59.  
  60.             } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
  61.  
  62.                     return response()->json(['token_expired'], $e->getStatusCode());
  63.  
  64.             } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
  65.  
  66.                     return response()->json(['token_invalid'], 401);
  67.  
  68.             } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
  69.  
  70.                     return response()->json(['token_absent'], $e->getStatusCode());
  71.  
  72.             }
  73.  
  74.             return response()->json(compact('user'));
  75.         }
  76.         public function logout()
  77.         {
  78.             auth('api')->logout();
  79.             return response()->json(['message' => 'Successfully logged out']);
  80.         }
  81.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top