unknown
1 year ago
16 changed files with 1059 additions and 9 deletions
@ -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']); |
||||
} |
||||
} |
@ -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 |
||||
]); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
||||
} |
@ -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!', |
||||
]); |
||||
} |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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 |
||||
]); |
||||
} |
||||
} |
@ -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, |
||||
], |
||||
]; |
Loading…
Reference in new issue