Created
June 20, 2020 12:45
-
-
Save blogcacanid/170cc1a3b20c9968805e5b16b3e241ce to your computer and use it in GitHub Desktop.
AuthController.php Rest API Dengan JSON Web Token Lumen 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Support\Facades\Auth; | |
use App\Http\Controllers\Controller; | |
use Validator; | |
use Illuminate\Http\Request; | |
use App\User; | |
class AuthController extends Controller | |
{ | |
public function __construct() | |
{ | |
$this->middleware('auth:api', ['except' => ['login','register']]); | |
} | |
/** | |
* Store a new user. | |
* | |
* @param Request $request | |
* @return Response | |
*/ | |
public function register(Request $request) | |
{ | |
$validator = Validator::make($request->all(), [ | |
'name' => 'required|between:2,100', | |
'email' => 'required|email|unique:users|max:50', | |
'password' => 'required|confirmed|string|min:6', | |
]); | |
$user = User::create(array_merge( | |
$validator->validated(), | |
['password' => app('hash')->make($request->password)] | |
)); | |
return response()->json([ | |
'message' => 'Successfully registered', | |
'user' => $user | |
], 201); | |
} | |
public function register123(Request $request) | |
{ | |
//validate incoming request | |
$this->validate($request, [ | |
'username' => 'required|string|unique:users', | |
'password' => 'required|confirmed', | |
]); | |
try | |
{ | |
$user = new User; | |
$user->username= $request->input('username'); | |
$user->password = app('hash')->make($request->input('password')); | |
$user->save(); | |
return response()->json( [ | |
'entity' => 'users', | |
'action' => 'create', | |
'result' => 'success' | |
], 201); | |
} | |
catch (\Exception $e) | |
{ | |
return response()->json( [ | |
'entity' => 'users', | |
'action' => 'create', | |
'result' => 'failed' | |
], 409); | |
} | |
} | |
/** | |
* Get a JWT via given credentials. | |
* | |
* @param Request $request | |
* @return Response | |
*/ | |
public function login(Request $request) | |
{ | |
$validator = Validator::make($request->all(), [ | |
'email' => 'required|email', | |
'password' => 'required|string|min:6', | |
]); | |
if ($validator->fails()) { | |
return response()->json($validator->errors(), 422); | |
} | |
if (! $token = auth()->attempt($validator->validated())) { | |
return response()->json(['error' => 'Unauthorized'], 401); | |
} | |
return $this->createNewToken($token); | |
} | |
public function login2(Request $request) | |
{ | |
//validate incoming request | |
$this->validate($request, [ | |
'username' => 'required|string', | |
'password' => 'required|string', | |
]); | |
$credentials = $request->only(['username', 'password']); | |
if (! $token = Auth::attempt($credentials)) { | |
return response()->json(['message' => 'Unauthorized'], 401); | |
} | |
return $this->respondWithToken($token); | |
} | |
/** | |
* Get user details. | |
* | |
* @param Request $request | |
* @return Response | |
*/ | |
public function profile() | |
{ | |
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->createNewToken(auth()->refresh()); | |
} | |
/** | |
* Get the token array structure. | |
* | |
* @param string $token | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function createNewToken($token) | |
{ | |
return response()->json([ | |
'access_token' => $token, | |
'token_type' => 'bearer', | |
'expires_in' => auth()->factory()->getTTL() * 60 | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment