pos-gis/app/Http/Controllers/Auth/LoginController.php

199 lines
5.7 KiB
PHP
Raw Normal View History

2024-10-07 06:13:42 +00:00
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use App\User;
use Validator, Redirect, Auth, Session, DB, Hash;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
// $this->middleware('guest')->except('logout');
}
public function username()
{
$identity = request()->get('identity');
$password = request()->get('password');
$fieldName = filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$fieldName => $identity]);
return $fieldName;
}
/**
* Validate the user login.
* @param Request $request
*/
protected function validateLogin(Request $request)
{
$this->validate(
$request,
[
'identity' => 'required|string',
'password' => 'required|string',
],
[
'identity.required' => 'Username or email is required',
'password.required' => 'Password is required',
]
);
}
/**
* @param Request $request
* @throws ValidationException
*/
protected function sendFailedLoginResponse(Request $request)
{
$request->session()->put('login_error', trans('auth.failed'));
throw ValidationException::withMessages(
[
'error' => [trans('auth.failed')],
]
);
}
public function login_admin(Request $request)
{
if (Auth::guard('admin')->check()) {
$datas['title'] = 'Dashboard';
$datas['ref_user'] = \DB::select("SELECT * FROM users_admin WHERE status_user = 't'");
return $this->bsGetView('content.dashboard',$datas);
}else{
return view('auth.login');
}
}
public function logout_admin() {
$user = DB::table('users_admin')->where('id',Auth::guard('admin')->user()->id)->first();
$event = "Logout User ID:".Auth::guard('admin')->user()->id;
$this->auditTrailLog($event,"Logout Log WEBMIN_BANK",'',json_encode($user),'');
Session::flush();
Auth::guard('admin')->logout();
return redirect()->route('admin.login');
}
public function authenticate(Request $request)
{
$credentials = $request->only('username', 'password');
$user = DB::table('users_admin')->where('username', $request->input('username'))->first();
if (!is_null($user)) {
$checkPassword = $this->hashPasswordCheck($request->input('password'),$user->password);
if($checkPassword != $user->password){
$data = User::find($user->id);
$data->wrong_password = $data->wrong_password+1;
$data->save();
if ($data->wrong_password > 6) {
$data = User::find($user->id);
$data->status_user = 'f';
$data->save();
$event = 'Status User Telah di block, silahkan hubungi admin';
$this->auditTrailLog($event,"Invalid Login Log",'',json_encode($user),'');
return response()->json([
'rc' => 0,
'rm' => 'Status User Telah di block, silahkan hubungi admin'
]);
}else{
$event = 'Username atau Password salah';
$this->auditTrailLog($event,"Invalid Login Log",'',json_encode($user),'');
return response()->json([
'rc' => 0,
'rm' => 'Username atau Password salah'
]);
}
}
if ($user->status_user == 'f'){
$event = 'Akun anda tidak aktif. Silahkan hubungi admin.';
$this->auditTrailLog($event,"Invalid Login Log",'',json_encode($user),'');
return response()->json([
'rc' => 1,
'rm' => 'Akun anda tidak aktif. Silahkan hubungi admin.'
]);
}
Session::regenerate();
Session::put('id', $user->id);
$event = 'Login User Success';
$this->auditTrailLog($event,"Login Log",'',json_encode($user),'');
Auth::guard('admin')->loginUsingId($user->id);
// Auth::loginUsingId();
return response()->json([
'rc' => 3,
'rm' => 'success',
'role' => $user->role
]);
} else {
$event = 'Username atau Password salah, user :'.$request->input('username');
$this->auditTrailLog($event,"Invalid Login Log",'',json_encode($user),'');
// login failed
return response()->json([
'rc' => 0,
'rm' => 'Username atau Password salah'
]);
}
}
// }
}