211 lines
6.8 KiB
PHP
211 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Login;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use thiagoalessio\TesseractOCR\TesseractOCR;
|
|
use Intervention\Image\ImageManagerStatic as Image;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
class LoginController extends Controller
|
|
{
|
|
/**
|
|
* Create a new AuthController instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth:api', ['except' => ['login', 'authenticate', 'register', 'hai']]);
|
|
}
|
|
|
|
public function login()
|
|
{
|
|
return view('index');
|
|
}
|
|
|
|
/**
|
|
* Get a JWT via given credentials.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function authenticate(Request $request)
|
|
{
|
|
$credentials = $request->validate([
|
|
'email' => ['required', 'email'],
|
|
'password' => ['required', 'min:8'],
|
|
]);
|
|
|
|
if (Auth::attempt($credentials)) {
|
|
if (Auth::user()->status == 'Finished') {
|
|
$request->session()->regenerate();
|
|
|
|
if (ucwords(Auth::user()->role) == 'Admin') {
|
|
return redirect()->intended('/dashboard');
|
|
} else {
|
|
return redirect()->intended('/');
|
|
}
|
|
} else {
|
|
Session::flash('message', 'Akun tidak ditemukan atau sedang dalam pengajuan');
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
return redirect()->back()
|
|
->withErrors([
|
|
'email' => 'Email dengan'.$credentials['email'].' tidak tersedia.',
|
|
])
|
|
->onlyInput('email');
|
|
}
|
|
|
|
public function hai()
|
|
{
|
|
echo 'sukses';
|
|
}
|
|
|
|
/**
|
|
* Log the user out (Invalidate the token).
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function logout(Request $request)
|
|
{
|
|
Auth::logout();
|
|
|
|
$request->session()->invalidate();
|
|
|
|
$request->session()->regenerateToken();
|
|
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
public function registerStore(Request $request)
|
|
{
|
|
$validatedData = $request->validate([
|
|
'nama' => 'required|max:255',
|
|
'email' => 'required|string|email|unique:users',
|
|
'password' => 'required|string|min:8',
|
|
'nohp' => 'required',
|
|
'nik' => 'required',
|
|
'alamat' => 'required',
|
|
'foto_ktp' => 'required|image|mimes:jpeg,svg,png,jpg',
|
|
'foto_wajah' => 'required|image|mimes:jpeg,svg,png,jpg',
|
|
'foto_profil' => 'image|mimes:jpeg,svg,png,jpg',
|
|
'gender' => 'required',
|
|
]);
|
|
|
|
// $validatedData['email_verified_at'] = now();
|
|
|
|
$validatedData['foto-ktp'] = '';
|
|
$validatedData['foto-wajah'] = '';
|
|
$validatedData['foto-profil'] ='';
|
|
if ($request->hasFile('foto_ktp') && $request->hasFile('foto_wajah')) {
|
|
// $namaGambarOri = $request->file('foto-gambar')->getClientOriginalName();
|
|
// $namaGambar = round(microtime(true) * 1000) . '-' . str_replace(' ', '-', $namaGambarOri);
|
|
$tipeFotoKtp = $request->file('foto_ktp')->getClientMimeType();
|
|
$tipeFotoWajah = $request->file('foto_wajah')->getClientMimeType();
|
|
$validatedData['foto-ktp'] = 'Foto-KTP-' . $request->nama . '.' . $tipeFotoKtp;
|
|
$validatedData['foto-wajah'] = 'Foto-Wajah' . $request->nama . '.' . $tipeFotoWajah;
|
|
// Simpan foto
|
|
$request->file('foto-ktp')->storeAs('public/foto-ktp', $validatedData['foto-ktp']);
|
|
$request->file('foto-wajah')->storeAs('public/foto-wajah', $validatedData['foto-wajah']);
|
|
}
|
|
|
|
if($request->hasFile('foto_profil')){
|
|
$tipeFotoProfil = $request->file('foto_profil')->getClientMimeType();
|
|
$validatedData['foto-profil'] = 'Foto-Profil-' . $request->nama . '.' . $tipeFotoProfil;
|
|
// Simpan foto
|
|
$request->file('foto-profil')->storeAs('public/foto-profil', $validatedData['foto-profil']);
|
|
}
|
|
|
|
//OCR
|
|
try {
|
|
$imagePath = storage_path('foto-ktp/' . $validatedData['foto-ktp']);
|
|
|
|
$image = Image::make($imagePath);
|
|
|
|
$image->greyscale(); // Convert to grayscale
|
|
$image->contrast(10); // Increase contrast, adjust the value as needed
|
|
|
|
$preprocessedImagePath = storage_path('preprocessed_image.jpg');
|
|
$image->save($preprocessedImagePath);
|
|
|
|
$result = (new TesseractOCR($preprocessedImagePath))->run();
|
|
|
|
// (5) Normalize
|
|
|
|
$lines = explode("\n", $result);
|
|
$nikOCR = '';
|
|
$nikInputan = $request->nik;
|
|
$namaInputan = $request->nama;
|
|
|
|
foreach ($lines as $line) {
|
|
// normalize NIK
|
|
if (strpos($line, 'NIK') !== false) {
|
|
$nikOCR = preg_replace('/[^0-9]/', '', $line);
|
|
}
|
|
|
|
// Mencari nama
|
|
if (strpos($line, $namaInputan) !== false) {
|
|
$namaOCR = trim(substr($line, strpos($line, ':') + 1));
|
|
}
|
|
}
|
|
|
|
//Selesai
|
|
|
|
$percent = 0.0;
|
|
|
|
if (similar_text($nikInputan, $nikOCR, $percent) >= 70 && similar_text($namaOCR, $namaOCR, $percent) >= 70) {
|
|
$validatedData['status'] = 'Progress';
|
|
} else {
|
|
$validatedData['status'] = 'Pending';
|
|
}
|
|
} catch (\Exception $e) {
|
|
$validatedData['status'] = 'Pending';
|
|
}
|
|
//OCR
|
|
|
|
//Deteksi wajah belum
|
|
|
|
$validatedData['remember_token'] = Str::random(10);
|
|
$validatedData['password'] = Hash::make($request->password);
|
|
User::create([
|
|
'id' => Str::uuid(),
|
|
'nama' => $validatedData['nama'],
|
|
'email' => $validatedData['email'],
|
|
'email_verified_at' => null,
|
|
'password' => $validatedData['password'],
|
|
'role' => 'User',
|
|
'nohp' => $validatedData['nohp'],
|
|
'nik' => $validatedData['nik'],
|
|
'alamat' => $validatedData['alamat'],
|
|
'foto_ktp' => $validatedData['foto-ktp'],
|
|
'foto_wajah' => $validatedData['foto-wajah'],
|
|
'foto_profil' => $validatedData['foto-profil'],
|
|
'status' => $validatedData['status'],
|
|
'gender' => $validatedData['gender'],
|
|
'remember_token' => Str::random(10),
|
|
]);
|
|
return redirect('/login')->with('daftar', 'Daftar berhasil, silahkan login');
|
|
}
|
|
|
|
public function register()
|
|
{
|
|
return view('index');
|
|
}
|
|
|
|
public function approveUser(Request $request)
|
|
{
|
|
}
|
|
|
|
public function deniedUser(Request $request)
|
|
{
|
|
}
|
|
}
|