Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\User;
- use Firebase\JWT\JWT;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Auth;
- use App\Http\Requests\UserRegistrationRequest;
- use App\Http\Requests\generateTokenRequest;
- class ApiAuthController extends Controller{
- private $request;
- public function __construct(Request $request) {
- date_default_timezone_set('Asia/Jakarta');
- $this->request = $request;
- }
- protected function jwt(User $user) {
- $key = env('JWT_KEY');
- $payload = [
- 'iss' => "laravel-JWT", // Issuer of the token
- 'uid' => $user->id,
- 'iat' => time(),
- 'exp' => time() + (60*60), // detik*menit
- ];
- $token = JWT::encode($payload, $key, 'HS256'); // encode token
- $timeCur = date('d-m-Y H:i:s',$payload['exp']);
- $start = strtotime('now');
- $end = strtotime($timeCur);
- $diff = $end - $start; // total detik
- $hours = floor($diff / (60*60) ); // hitung total jam
- $minutes = floor( ($diff-$hours*(60*60)) / 60); // hitung menit(reset perJam)
- return (object)[
- 'token' => $token,
- 'hours' => $hours,
- 'minutes' => $minutes,
- 'seconds' => $diff,
- ];
- }
- public function authenticate(generateTokenRequest $request){
- try{
- $username = $this->request->header('x-username');
- $password = $this->request->header('x-password');
- $user = User::where('username',$username)->first();
- if(!$user){
- return response()->json([
- 'status' => false,
- 'metaData' => [
- 'code' => 400,
- 'messsage' => 'Username not found',
- ],
- ],400);
- }
- if(Hash::check($password,$user->password)){
- $token = $this->jwt($user);
- return response()->json([
- 'status' => true,
- 'metaData' => [
- 'code' => 200,
- 'message' => 'Token generated',
- ],
- 'datas' => [
- 'token' => $token->token,
- 'expiration' => [
- 'seconds' => $token->seconds,
- 'minutes' => $token->minutes,
- 'hours' => $token->hours,
- ],
- ]
- ], 200);
- }
- return response()->json([
- 'status' => false,
- 'metaData' => [
- 'code' => 400,
- 'message' => 'Incorrect username or password',
- ],
- ], 400);
- }catch(\Throwable $th){
- Log::error($th->getMessage());
- DB::rollback();
- return response()->json([
- 'success' => false,
- 'metaData' => [
- 'code' => 500,
- 'message' => 'Login failed'
- ]
- ], 500);
- }
- }
- public function register(UserRegistrationRequest $request){
- try{
- DB::beginTransaction();
- $user = User::create([
- 'name' => $request->name,
- 'username' => $request->username,
- 'email' => $request->email,
- 'password' => Hash::make($request->password),
- ]);
- DB::commit();
- $token = $this->jwt($user);
- return response()->json([
- 'status' => true,
- 'metaData' => [
- 'code' => 201,
- 'message' => 'Registration successful',
- ],
- 'datas' => [
- 'token' => $token->token,
- 'expiration' => [
- 'seconds' => $token->seconds,
- 'minutes' => $token->minutes,
- 'hours' => $token->hours,
- ],
- ]
- ], 201);
- }catch(\Throwable $th){
- Log::error($th->getMessage());
- DB::rollback();
- return response()->json([
- 'success' => false,
- 'metaData' => [
- 'code' => 500,
- 'message' => 'User registration failed'
- ]
- ], 500);
- }
- }
- public function getUser(Request $request){
- return $request->auth; // getRequestFromMiddleware
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement