Browse Source

Update: API,Helpers Format API

master
unknown 1 year ago
parent
commit
276a8b7bcb
  1. 47
      app/Helpers/ResponseFormatter.php
  2. 20
      app/Http/Controllers/API/v1/AssetStatusApi/AssetStatusApiController.php
  3. 49
      app/Http/Controllers/API/v1/AuthApi/EditApiController.php
  4. 73
      app/Http/Controllers/API/v1/AuthApi/LoginApiController.php
  5. 30
      app/Http/Controllers/API/v1/AuthApi/LogoutApiController.php
  6. 52
      app/Http/Controllers/API/v1/AuthApi/RegisterApiController.php
  7. 22
      app/Http/Controllers/API/v1/PeminjamanApi/PeminjamanApiController.php
  8. 27
      app/Models/User.php
  9. 1
      composer.json
  10. 418
      composer.lock
  11. 4
      config/auth.php
  12. 300
      config/jwt.php
  13. 5
      database/migrations/2023_10_23_075245_create_users_table.php
  14. 4
      resources/views/dashboard/peminjaman.blade.php
  15. 14
      routes/api.php

47
app/Helpers/ResponseFormatter.php

@ -0,0 +1,47 @@
<?php
namespace App\Helpers;
/**
* Format response.
*/
class ResponseFormatter
{
/**
* API Response
*
* @var array
*/
protected static $response = [
'meta' => [
'code' => 200,
'status' => 'success',
'message' => null,
],
'data' => null,
];
/**
* Give success response.
*/
public static function success($data = null, $message = null)
{
self::$response['meta']['message'] = $message;
self::$response['data'] = $data;
return response()->json(self::$response, self::$response['meta']['code']);
}
/**
* Give error response.
*/
public static function error($data = null, $message = null, $code = 400)
{
self::$response['meta']['status'] = 'error';
self::$response['meta']['code'] = $code;
self::$response['meta']['message'] = $message;
self::$response['data'] = $data;
return response()->json(self::$response, self::$response['meta']['code']);
}
}

20
app/Http/Controllers/API/v1/AssetStatusApi/AssetStatusApiController.php

@ -0,0 +1,20 @@
<?php
namespace App\Http\Controllers\API\v1\AssetStatusApi;
use App\Helpers\ResponseFormatter;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AssetStatusApiController extends Controller
{
public function index()
{
$m_asset = \App\Models\m_asset::get();
return ResponseFormatter::success([
'message' => 'Data asset berhasil diambil',
'asset' => $m_asset
]);
}
}

49
app/Http/Controllers/API/v1/AuthApi/EditApiController.php

@ -0,0 +1,49 @@
<?php
namespace App\Http\Controllers\API\v1\AuthApi;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
class EditApiController extends Controller
{
/**
* Handle the incoming request.
*/
public function __invoke(Request $request)
{
// Mendapatkan pengguna yang terotentikasi dari token JWT
$user = JWTAuth::parseToken()->authenticate();
// Validasi input
$request->validate(
[
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email,' . $user->id,
// Tambahkan validasi untuk bidang lain jika diperlukan
],
[
'name.required' => 'Nama harus diisi',
'email.required' => 'Email harus diisi',
'email.email' => 'Email tidak valid',
'email.unique' => 'Email sudah terdaftar',
]
);
// Memperbarui data pengguna
$user->name = $request->input('name');
$user->email = $request->input('email');
// Tambahkan perubahan lain sesuai kebutuhan
// Menyimpan perubahan ke database
$user->save();
// Memberikan respon
return response()->json([
'success' => true,
'message' => 'Pengguna berhasil diperbarui',
'user' => auth()->guard('api')->user(),
], 200);
}
}

73
app/Http/Controllers/API/v1/AuthApi/LoginApiController.php

@ -0,0 +1,73 @@
<?php
namespace App\Http\Controllers\API\v1\AuthApi;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Helpers\ResponseFormatter;
class LoginApiController extends Controller
{
/**
* @param Request $request
* @return mixed
*/
public function fetch(Request $request)
{
return ResponseFormatter::success($request->user(), 'Data profile user berhasil diambil');
}
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
try {
// set validation
$validator = Validator::make($request->all(), [
'email' => 'required',
'password' => 'required'
]);
// if validation fails
if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
// get credentials from request
$credentials = $request->only('email', 'password');
// if auth failed
if (!$token = auth()->guard('api')->attempt($credentials)) {
return response()->json([
'success' => false,
'message' => 'Email atau Password Anda salah'
], 401);
}
// if auth success
$user = auth()->guard('api')->user();
// Load roles for the user
// $user->load('roles');
return ResponseFormatter::success([
'token_type' => 'Bearer',
'user' => $user,
'token' => $token
// 'roles' => $user->roles
], 'Authentication successful');
} catch (\Exception $e) {
return ResponseFormatter::error([
'message' => 'Terjadi kesalahan saat memproses permintaan',
'error' => $e->getMessage()
], 'Authentication failed', 500);
}
}
}

30
app/Http/Controllers/API/v1/AuthApi/LogoutApiController.php

@ -0,0 +1,30 @@
<?php
namespace App\Http\Controllers\API\v1\AuthApi;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
class LogoutApiController extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
//remove token
$removeToken = JWTAuth::invalidate(JWTAuth::getToken());
if ($removeToken) {
//return response JSON
return response()->json([
'success' => true,
'message' => 'Logout Berhasil!',
]);
}
}
}

52
app/Http/Controllers/API/v1/AuthApi/RegisterApiController.php

@ -0,0 +1,52 @@
<?php
namespace App\Http\Controllers\API\v1\AuthApi;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
class RegisterApiController extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
//set validation
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:8|confirmed'
]);
//if validation fails
if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
//create user
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password)
]);
//return response JSON user is created
if ($user) {
return response()->json([
'success' => true,
'user' => $user,
], 201);
}
//return JSON process insert failed
return response()->json([
'success' => false,
], 409);
}
}

22
app/Http/Controllers/API/v1/PeminjamanApi/PeminjamanApiController.php

@ -0,0 +1,22 @@
<?php
namespace App\Http\Controllers\API\v1\PeminjamanApi;
use Illuminate\Http\Request;
use App\Helpers\ResponseFormatter;
use App\Http\Controllers\Controller;
use App\Models\asset_status;
use App\Models\BarangMasuk;
class PeminjamanApiController extends Controller
{
public function index()
{
$peminjaman = \App\Models\asset_status::get();
return ResponseFormatter::success([
'message' => 'Data peminjaman berhasil diambil',
'peminjam' => $peminjaman
]);
}
}

27
app/Models/User.php

@ -3,12 +3,13 @@
namespace App\Models; namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail; // use Illuminate\Contracts\Auth\MustVerifyEmail;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable class User extends Authenticatable implements JWTSubject
{ {
use HasApiTokens, HasFactory, Notifiable; use HasApiTokens, HasFactory, Notifiable;
@ -43,4 +44,24 @@ class User extends Authenticatable
'email_verified_at' => 'datetime', 'email_verified_at' => 'datetime',
'password' => 'hashed', 'password' => 'hashed',
]; ];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
} }

1
composer.json

@ -12,6 +12,7 @@
"laravel/sanctum": "^3.2", "laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8", "laravel/tinker": "^2.8",
"maatwebsite/excel": "^3.1", "maatwebsite/excel": "^3.1",
"php-open-source-saver/jwt-auth": "^2.1",
"simplesoftwareio/simple-qrcode": "^4.2" "simplesoftwareio/simple-qrcode": "^4.2"
}, },
"require-dev": { "require-dev": {

418
composer.lock generated

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "eebc71566b355489dede69a8f81b8d19", "content-hash": "19876d5e68b20a66510edcf50f8c0318",
"packages": [ "packages": [
{ {
"name": "bacon/bacon-qr-code", "name": "bacon/bacon-qr-code",
@ -1801,6 +1801,146 @@
}, },
"time": "2023-02-15T16:40:09+00:00" "time": "2023-02-15T16:40:09+00:00"
}, },
{
"name": "lcobucci/clock",
"version": "2.3.0",
"source": {
"type": "git",
"url": "https://github.com/lcobucci/clock.git",
"reference": "c7aadcd6fd97ed9e199114269c0be3f335e38876"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/lcobucci/clock/zipball/c7aadcd6fd97ed9e199114269c0be3f335e38876",
"reference": "c7aadcd6fd97ed9e199114269c0be3f335e38876",
"shasum": ""
},
"require": {
"php": "~8.1.0 || ~8.2.0",
"stella-maris/clock": "^0.1.7"
},
"provide": {
"psr/clock-implementation": "1.0"
},
"require-dev": {
"infection/infection": "^0.26",
"lcobucci/coding-standard": "^9.0",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.9.4",
"phpstan/phpstan-deprecation-rules": "^1.1.1",
"phpstan/phpstan-phpunit": "^1.3.2",
"phpstan/phpstan-strict-rules": "^1.4.4",
"phpunit/phpunit": "^9.5.27"
},
"type": "library",
"autoload": {
"psr-4": {
"Lcobucci\\Clock\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Luís Cobucci",
"email": "lcobucci@gmail.com"
}
],
"description": "Yet another clock abstraction",
"support": {
"issues": "https://github.com/lcobucci/clock/issues",
"source": "https://github.com/lcobucci/clock/tree/2.3.0"
},
"funding": [
{
"url": "https://github.com/lcobucci",
"type": "github"
},
{
"url": "https://www.patreon.com/lcobucci",
"type": "patreon"
}
],
"time": "2022-12-19T14:38:11+00:00"
},
{
"name": "lcobucci/jwt",
"version": "4.0.4",
"source": {
"type": "git",
"url": "https://github.com/lcobucci/jwt.git",
"reference": "55564265fddf810504110bd68ca311932324b0e9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/lcobucci/jwt/zipball/55564265fddf810504110bd68ca311932324b0e9",
"reference": "55564265fddf810504110bd68ca311932324b0e9",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"ext-openssl": "*",
"lcobucci/clock": "^2.0",
"php": "^7.4 || ^8.0"
},
"require-dev": {
"infection/infection": "^0.20",
"lcobucci/coding-standard": "^6.0",
"mikey179/vfsstream": "^1.6",
"phpbench/phpbench": "^0.17",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-deprecation-rules": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
"phpstan/phpstan-strict-rules": "^0.12",
"phpunit/php-invoker": "^3.1",
"phpunit/phpunit": "^9.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"autoload": {
"psr-4": {
"Lcobucci\\JWT\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Luís Cobucci",
"email": "lcobucci@gmail.com",
"role": "Developer"
}
],
"description": "A simple library to work with JSON Web Token and JSON Web Signature",
"keywords": [
"JWS",
"jwt"
],
"support": {
"issues": "https://github.com/lcobucci/jwt/issues",
"source": "https://github.com/lcobucci/jwt/tree/4.0.4"
},
"funding": [
{
"url": "https://github.com/lcobucci",
"type": "github"
},
{
"url": "https://www.patreon.com/lcobucci",
"type": "patreon"
}
],
"time": "2021-09-28T19:18:28+00:00"
},
{ {
"name": "league/commonmark", "name": "league/commonmark",
"version": "2.4.0", "version": "2.4.0",
@ -2629,6 +2769,73 @@
], ],
"time": "2023-06-21T08:46:11+00:00" "time": "2023-06-21T08:46:11+00:00"
}, },
{
"name": "namshi/jose",
"version": "7.2.3",
"source": {
"type": "git",
"url": "https://github.com/namshi/jose.git",
"reference": "89a24d7eb3040e285dd5925fcad992378b82bcff"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/namshi/jose/zipball/89a24d7eb3040e285dd5925fcad992378b82bcff",
"reference": "89a24d7eb3040e285dd5925fcad992378b82bcff",
"shasum": ""
},
"require": {
"ext-date": "*",
"ext-hash": "*",
"ext-json": "*",
"ext-pcre": "*",
"ext-spl": "*",
"php": ">=5.5",
"symfony/polyfill-php56": "^1.0"
},
"require-dev": {
"phpseclib/phpseclib": "^2.0",
"phpunit/phpunit": "^4.5|^5.0",
"satooshi/php-coveralls": "^1.0"
},
"suggest": {
"ext-openssl": "Allows to use OpenSSL as crypto engine.",
"phpseclib/phpseclib": "Allows to use Phpseclib as crypto engine, use version ^2.0."
},
"type": "library",
"autoload": {
"psr-4": {
"Namshi\\JOSE\\": "src/Namshi/JOSE/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alessandro Nadalin",
"email": "alessandro.nadalin@gmail.com"
},
{
"name": "Alessandro Cinelli (cirpo)",
"email": "alessandro.cinelli@gmail.com"
}
],
"description": "JSON Object Signing and Encryption library for PHP.",
"keywords": [
"JSON Web Signature",
"JSON Web Token",
"JWS",
"json",
"jwt",
"token"
],
"support": {
"issues": "https://github.com/namshi/jose/issues",
"source": "https://github.com/namshi/jose/tree/master"
},
"time": "2016-12-05T07:27:31+00:00"
},
{ {
"name": "nesbot/carbon", "name": "nesbot/carbon",
"version": "2.69.0", "version": "2.69.0",
@ -3116,6 +3323,100 @@
}, },
"time": "2022-09-06T12:16:56+00:00" "time": "2022-09-06T12:16:56+00:00"
}, },
{
"name": "php-open-source-saver/jwt-auth",
"version": "2.1.0",
"source": {
"type": "git",
"url": "https://github.com/PHP-Open-Source-Saver/jwt-auth.git",
"reference": "5b4e3eec31c8da03d58b64c4e28c469b334bec4c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-Open-Source-Saver/jwt-auth/zipball/5b4e3eec31c8da03d58b64c4e28c469b334bec4c",
"reference": "5b4e3eec31c8da03d58b64c4e28c469b334bec4c",
"shasum": ""
},
"require": {
"ext-json": "*",
"illuminate/auth": "^6|^7|^8.67|^9|^10",
"illuminate/contracts": "^6|^7|^8.67|^9|^10",
"illuminate/http": "^6|^7|^8.67|^9|^10",
"illuminate/support": "^6|^7|^8.67|^9|^10",
"lcobucci/jwt": "^4.0",
"namshi/jose": "^7.0",
"nesbot/carbon": "^1.0|^2.0",
"php": "^7.4|^8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"illuminate/console": "^6|^7|^8.67|^9|^10",
"illuminate/routing": "^6|^7|^8.67|^9|^10",
"mockery/mockery": "^1.4.4",
"orchestra/testbench": "^4.18|^5.8|^6.3|^7|^8",
"phpstan/phpstan": "^1",
"phpunit/phpunit": "^8.5|^9.4|^10",
"rector/rector": "^0.12.4",
"vlucas/phpdotenv": "^5.2.0",
"yoast/phpunit-polyfills": "^1.0.2"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-develop": "2.0-dev"
},
"laravel": {
"aliases": {
"JWTAuth": "PHPOpenSourceSaver\\JWTAuth\\Facades\\JWTAuth",
"JWTFactory": "PHPOpenSourceSaver\\JWTAuth\\Facades\\JWTFactory"
},
"providers": [
"PHPOpenSourceSaver\\JWTAuth\\Providers\\LaravelServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"PHPOpenSourceSaver\\JWTAuth\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Sean Tymon",
"email": "tymon148@gmail.com",
"homepage": "https://tymon.xyz",
"role": "Forked package creator | Developer"
},
{
"name": "Eric Schricker",
"email": "eric.schricker@adiutabyte.de",
"role": "Developer"
},
{
"name": "Fabio William Conceição",
"email": "messhias@gmail.com",
"role": "Developer"
}
],
"description": "JSON Web Token Authentication for Laravel and Lumen",
"homepage": "https://github.com/PHP-Open-Source-Saver/jwt-auth",
"keywords": [
"Authentication",
"JSON Web Token",
"auth",
"jwt",
"laravel"
],
"support": {
"issues": "https://github.com/PHP-Open-Source-Saver/jwt-auth/issues",
"source": "https://github.com/PHP-Open-Source-Saver/jwt-auth"
},
"time": "2023-02-17T11:42:33+00:00"
},
{ {
"name": "phpoffice/phpspreadsheet", "name": "phpoffice/phpspreadsheet",
"version": "1.29.0", "version": "1.29.0",
@ -4130,6 +4431,53 @@
}, },
"time": "2021-02-08T20:43:55+00:00" "time": "2021-02-08T20:43:55+00:00"
}, },
{
"name": "stella-maris/clock",
"version": "0.1.7",
"source": {
"type": "git",
"url": "https://github.com/stella-maris-solutions/clock.git",
"reference": "fa23ce16019289a18bb3446fdecd45befcdd94f8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/stella-maris-solutions/clock/zipball/fa23ce16019289a18bb3446fdecd45befcdd94f8",
"reference": "fa23ce16019289a18bb3446fdecd45befcdd94f8",
"shasum": ""
},
"require": {
"php": "^7.0|^8.0",
"psr/clock": "^1.0"
},
"type": "library",
"autoload": {
"psr-4": {
"StellaMaris\\Clock\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Andreas Heigl",
"role": "Maintainer"
}
],
"description": "A pre-release of the proposed PSR-20 Clock-Interface",
"homepage": "https://gitlab.com/stella-maris/clock",
"keywords": [
"clock",
"datetime",
"point in time",
"psr20"
],
"support": {
"source": "https://github.com/stella-maris-solutions/clock/tree/0.1.7"
},
"time": "2022-11-25T16:15:06+00:00"
},
{ {
"name": "symfony/console", "name": "symfony/console",
"version": "v6.3.2", "version": "v6.3.2",
@ -5417,6 +5765,74 @@
], ],
"time": "2022-11-03T14:55:06+00:00" "time": "2022-11-03T14:55:06+00:00"
}, },
{
"name": "symfony/polyfill-php56",
"version": "v1.20.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php56.git",
"reference": "54b8cd7e6c1643d78d011f3be89f3ef1f9f4c675"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php56/zipball/54b8cd7e6c1643d78d011f3be89f3ef1f9f4c675",
"reference": "54b8cd7e6c1643d78d011f3be89f3ef1f9f4c675",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"type": "metapackage",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-php56/tree/v1.20.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-10-23T14:02:19+00:00"
},
{ {
"name": "symfony/polyfill-php72", "name": "symfony/polyfill-php72",
"version": "v1.27.0", "version": "v1.27.0",

4
config/auth.php

@ -40,6 +40,10 @@ return [
'driver' => 'session', 'driver' => 'session',
'provider' => 'users', 'provider' => 'users',
], ],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
], ],
/* /*

300
config/jwt.php

@ -0,0 +1,300 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| JWT Authentication Secret
|--------------------------------------------------------------------------
|
| Don't forget to set this in your .env file, as it will be used to sign
| your tokens. A helper command is provided for this:
| `php artisan jwt:secret`
|
| Note: This will be used for Symmetric algorithms only (HMAC),
| since RSA and ECDSA use a private/public key combo (See below).
|
*/
'secret' => env('JWT_SECRET'),
/*
|--------------------------------------------------------------------------
| JWT Authentication Keys
|--------------------------------------------------------------------------
|
| The algorithm you are using, will determine whether your tokens are
| signed with a random string (defined in `JWT_SECRET`) or using the
| following public & private keys.
|
| Symmetric Algorithms:
| HS256, HS384 & HS512 will use `JWT_SECRET`.
|
| Asymmetric Algorithms:
| RS256, RS384 & RS512 / ES256, ES384 & ES512 will use the keys below.
|
*/
'keys' => [
/*
|--------------------------------------------------------------------------
| Public Key
|--------------------------------------------------------------------------
|
| A path or resource to your public key.
|
| E.g. 'file://path/to/public/key'
|
*/
'public' => env('JWT_PUBLIC_KEY'),
/*
|--------------------------------------------------------------------------
| Private Key
|--------------------------------------------------------------------------
|
| A path or resource to your private key.
|
| E.g. 'file://path/to/private/key'
|
*/
'private' => env('JWT_PRIVATE_KEY'),
/*
|--------------------------------------------------------------------------
| Passphrase
|--------------------------------------------------------------------------
|
| The passphrase for your private key. Can be null if none set.
|
*/
'passphrase' => env('JWT_PASSPHRASE'),
],
/*
|--------------------------------------------------------------------------
| JWT time to live
|--------------------------------------------------------------------------
|
| Specify the length of time (in minutes) that the token will be valid for.
| Defaults to 1 hour.
|
| You can also set this to null, to yield a never expiring token.
| Some people may want this behaviour for e.g. a mobile app.
| This is not particularly recommended, so make sure you have appropriate
| systems in place to revoke the token if necessary.
| Notice: If you set this to null you should remove 'exp' element from 'required_claims' list.
|
*/
'ttl' => env('JWT_TTL', 60),
/*
|--------------------------------------------------------------------------
| Refresh time to live
|--------------------------------------------------------------------------
|
| Specify the length of time (in minutes) that the token can be refreshed
| within. I.E. The user can refresh their token within a 2 week window of
| the original token being created until they must re-authenticate.
| Defaults to 2 weeks.
|
| You can also set this to null, to yield an infinite refresh time.
| Some may want this instead of never expiring tokens for e.g. a mobile app.
| This is not particularly recommended, so make sure you have appropriate
| systems in place to revoke the token if necessary.
|
*/
'refresh_ttl' => env('JWT_REFRESH_TTL', 20160),
/*
|--------------------------------------------------------------------------
| JWT hashing algorithm
|--------------------------------------------------------------------------
|
| Specify the hashing algorithm that will be used to sign the token.
|
| See here: https://github.com/namshi/jose/tree/master/src/Namshi/JOSE/Signer/OpenSSL
| for possible values.
|
*/
'algo' => env('JWT_ALGO', 'HS256'),
/*
|--------------------------------------------------------------------------
| Required Claims
|--------------------------------------------------------------------------
|
| Specify the required claims that must exist in any token.
| A TokenInvalidException will be thrown if any of these claims are not
| present in the payload.
|
*/
'required_claims' => [
'iss',
'iat',
'exp',
'nbf',
'sub',
'jti',
],
/*
|--------------------------------------------------------------------------
| Persistent Claims
|--------------------------------------------------------------------------
|
| Specify the claim keys to be persisted when refreshing a token.
| `sub` and `iat` will automatically be persisted, in
| addition to the these claims.
|
| Note: If a claim does not exist then it will be ignored.
|
*/
'persistent_claims' => [
// 'foo',
// 'bar',
],
/*
|--------------------------------------------------------------------------
| Lock Subject
|--------------------------------------------------------------------------
|
| This will determine whether a `prv` claim is automatically added to
| the token. The purpose of this is to ensure that if you have multiple
| authentication models e.g. `App\User` & `App\OtherPerson`, then we
| should prevent one authentication request from impersonating another,
| if 2 tokens happen to have the same id across the 2 different models.
|
| Under specific circumstances, you may want to disable this behaviour
| e.g. if you only have one authentication model, then you would save
| a little on token size.
|
*/
'lock_subject' => true,
/*
|--------------------------------------------------------------------------
| Leeway
|--------------------------------------------------------------------------
|
| This property gives the jwt timestamp claims some "leeway".
| Meaning that if you have any unavoidable slight clock skew on
| any of your servers then this will afford you some level of cushioning.
|
| This applies to the claims `iat`, `nbf` and `exp`.
|
| Specify in seconds - only if you know you need it.
|
*/
'leeway' => env('JWT_LEEWAY', 0),
/*
|--------------------------------------------------------------------------
| Blacklist Enabled
|--------------------------------------------------------------------------
|
| In order to invalidate tokens, you must have the blacklist enabled.
| If you do not want or need this functionality, then set this to false.
|
*/
'blacklist_enabled' => env('JWT_BLACKLIST_ENABLED', true),
/*
| -------------------------------------------------------------------------
| Blacklist Grace Period
| -------------------------------------------------------------------------
|
| When multiple concurrent requests are made with the same JWT,
| it is possible that some of them fail, due to token regeneration
| on every request.
|
| Set grace period in seconds to prevent parallel request failure.
|
*/
'blacklist_grace_period' => env('JWT_BLACKLIST_GRACE_PERIOD', 0),
/*
|--------------------------------------------------------------------------
| Show blacklisted token option
|--------------------------------------------------------------------------
|
| Specify if you want to show black listed token exception on the laravel logs.
|
*/
'show_black_list_exception' => env('JWT_SHOW_BLACKLIST_EXCEPTION', true),
/*
|--------------------------------------------------------------------------
| Cookies encryption
|--------------------------------------------------------------------------
|
| By default Laravel encrypt cookies for security reason.
| If you decide to not decrypt cookies, you will have to configure Laravel
| to not encrypt your cookie token by adding its name into the $except
| array available in the middleware "EncryptCookies" provided by Laravel.
| see https://laravel.com/docs/master/responses#cookies-and-encryption
| for details.
|
| Set it to true if you want to decrypt cookies.
|
*/
'decrypt_cookies' => false,
/*
|--------------------------------------------------------------------------
| Providers
|--------------------------------------------------------------------------
|
| Specify the various providers used throughout the package.
|
*/
'providers' => [
/*
|--------------------------------------------------------------------------
| JWT Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to create and decode the tokens.
|
*/
'jwt' => PHPOpenSourceSaver\JWTAuth\Providers\JWT\Lcobucci::class,
/*
|--------------------------------------------------------------------------
| Authentication Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to authenticate users.
|
*/
'auth' => PHPOpenSourceSaver\JWTAuth\Providers\Auth\Illuminate::class,
/*
|--------------------------------------------------------------------------
| Storage Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to store tokens in the blacklist.
|
*/
'storage' => PHPOpenSourceSaver\JWTAuth\Providers\Storage\Illuminate::class,
],
];

5
database/migrations/2023_10_23_075245_create_users_table.php

@ -24,8 +24,9 @@ return new class extends Migration
$table->string('jenis_kelamin', 30)->nullable(); $table->string('jenis_kelamin', 30)->nullable();
$table->string('agama', 30)->nullable(); $table->string('agama', 30)->nullable();
$table->string('foto', 255)->nullable(); $table->string('foto', 255)->nullable();
$table->bigInteger('role_id')->unsigned()->nullable(); // $table->bigInteger('role_id')->unsigned()->nullable();
$table->foreign('role_id')->references('id')->on('m_roles')->onDelete('set null'); // $table->foreign('role_id')->references('id')->on('m_roles')->onDelete('set null');
$table->foreignId('role_id')->constrained('m_roles')->onDelete('cascade');
$table->bigInteger('warehouse_id')->unsigned()->nullable(); $table->bigInteger('warehouse_id')->unsigned()->nullable();
$table->foreign('warehouse_id')->references('id')->on('m_warehouses')->onDelete('set null'); $table->foreign('warehouse_id')->references('id')->on('m_warehouses')->onDelete('set null');
$table->text('address')->nullable(); $table->text('address')->nullable();

4
resources/views/dashboard/peminjaman.blade.php

@ -64,10 +64,10 @@
data-target="#editDataModal{{ $data['id'] }}"> data-target="#editDataModal{{ $data['id'] }}">
<i class="fa fa-edit mr-2" style="font-size: 20px"></i> <i class="fa fa-edit mr-2" style="font-size: 20px"></i>
</a> </a>
<a href="{{ route('hapusPeminjaman.destroy', $data->id) }}" {{-- <a href="{{ route('hapusPeminjaman.destroy', $data->id) }}"
onclick="return confirm('Apakah Anda yakin ingin menghapus data ini?')"> onclick="return confirm('Apakah Anda yakin ingin menghapus data ini?')">
<i class="fa fa-trash text-danger mr-2" style="font-size: 20px"></i> <i class="fa fa-trash text-danger mr-2" style="font-size: 20px"></i>
</a> </a> --}}
</td> </td>
</tr> </tr>
@endforeach @endforeach

14
routes/api.php

@ -14,6 +14,20 @@ use Illuminate\Support\Facades\Route;
| |
*/ */
Route::prefix('v1')->group(function () {
Route::post('/register', App\Http\Controllers\API\v1\AuthApi\RegisterApiController::class)->name('register');
Route::post('/login', App\Http\Controllers\API\v1\AuthApi\LoginApiController::class)->name('login');
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/logout', App\Http\Controllers\API\v1\AuthApi\LogoutApiController::class)->name('logout');
Route::put('/user/edit', App\Http\Controllers\API\v1\AuthApi\EditApiController::class)->name('user.edit');
Route::get('/peminjaman-barang', [App\Http\Controllers\API\v1\PeminjamanApi\PeminjamanApiController::class, 'index'])->name('peminjaman-barang');
Route::get('/m-asset', [App\Http\Controllers\API\v1\AssetStatusApi\AssetStatusApiController::class, 'index'])->name('status.barang');
});
Route::middleware('auth:sanctum')->get('/user', function (Request $request) { Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user(); return $request->user();
}); });

Loading…
Cancel
Save