feat(auth): create two step verify page with func
This commit is contained in:
parent
e03b4d2ac1
commit
27e6fcec20
|
@ -3,14 +3,16 @@
|
|||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Mail\ActivationAccountMail;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rules;
|
||||
use Illuminate\View\View;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
|
||||
class RegisteredUserController extends Controller
|
||||
{
|
||||
|
@ -54,16 +56,21 @@ public function store(Request $request)
|
|||
return redirect()->back()->withInput();
|
||||
}
|
||||
|
||||
$activationCode = Str::random(4);
|
||||
|
||||
DB::beginTransaction();
|
||||
$user = new User();
|
||||
$user->username = $request->username;
|
||||
$user->name = $request->name;
|
||||
$user->email = $request->email;
|
||||
$user->password = Hash::make($request->password);
|
||||
$user->activation_code = $activationCode;
|
||||
$user->is_active = false;
|
||||
|
||||
try {
|
||||
$user->save();
|
||||
DB::commit();
|
||||
Mail::to($request->email)->send(new ActivationAccountMail($user));
|
||||
toast('Registrasi berhasil', 'success')->position('top')->autoclose(3000);
|
||||
return redirect()->route('auth.two_step_verify', ['email' => $request->email]);
|
||||
} catch (\Throwable $th) {
|
||||
|
|
|
@ -3,7 +3,11 @@
|
|||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Mail\ActivationAccountMail;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class TwoStepVerifyController extends Controller
|
||||
{
|
||||
|
@ -11,4 +15,42 @@ public function create($email)
|
|||
{
|
||||
return view('auth.two-step-verifycation', compact('email'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$user = User::where('email', $request->email)->first();
|
||||
$userActivationCode = $user->activation_code;
|
||||
$activationCodeRequest = $request->digit1 . $request->digit2 . $request->digit3 . $request->digit4;
|
||||
|
||||
if ($userActivationCode == $activationCodeRequest) {
|
||||
$user->activation_code = null;
|
||||
$user->is_active = 1;
|
||||
$user->save();
|
||||
DB::commit();
|
||||
toast('Akun anda telah diaktifkan', 'success')->position('top')->autoclose(3000);
|
||||
return redirect()->route('auth.login');
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollBack();
|
||||
toast($th->getMessage(), 'error')->position('top')->autoclose(3000);
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
public function resendEmail(Request $request)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$user = User::where('email', $request->email)->first();
|
||||
Mail::to($user->email)->send(new ActivationAccountMail($user));
|
||||
toast('Kode verifikasi telah dikirim', 'success')->position('top')->autoclose(3000);
|
||||
return redirect()->route('auth.two_step_verify', ['email' => $request->email]);
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollBack();
|
||||
toast($th->getMessage(), 'error')->position('top')->autoclose(3000);
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ActivationAccountMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
public function __construct($user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message envelope.
|
||||
*/
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Activation Account Mail',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message content definition.
|
||||
*/
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'auth.activation-account-mail',
|
||||
with: [
|
||||
'name' => $this->user->name,
|
||||
'activationCode' => $this->user->activation_code
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attachments for the message.
|
||||
*
|
||||
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
||||
*/
|
||||
public function attachments(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
|
@ -4,6 +4,8 @@
|
|||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
use function Laravel\Prompts\table;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
|
@ -18,6 +20,8 @@ public function up(): void
|
|||
$table->string('email')->unique();
|
||||
$table->string('password');
|
||||
$table->enum('role', ['admin', 'user'])->default('user');
|
||||
$table->string('activation_code')->nullable();
|
||||
$table->boolean('is_active')->default(false);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -20,14 +20,16 @@ public function run(): void
|
|||
'name' => 'Admin Sage',
|
||||
'email' => 'adminSage@gmail.com',
|
||||
'password' => Hash::make('admin123'),
|
||||
'role' => 'admin'
|
||||
'role' => 'admin',
|
||||
'is_active' => true
|
||||
]),
|
||||
User::create([
|
||||
'username' => 'petugas',
|
||||
'name' => 'Petugas Sage',
|
||||
'email' => 'petugasSage@gmail.com',
|
||||
'password' => Hash::make('petugas123'),
|
||||
'role' => 'user'
|
||||
'role' => 'user',
|
||||
'is_active' => true
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Kode Aktivasi</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Halo, {{ $name }}</h1>
|
||||
<p>Berikut adalah kode aktivasi untuk akun Anda:</p>
|
||||
<h2>{{ $activationCode }}</h2>
|
||||
<p>Masukkan kode ini di aplikasi untuk mengaktifkan akun Anda.</p>
|
||||
<p>Terima kasih!</p>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -76,14 +76,18 @@
|
|||
class="fw-semibold">{{ $email }}</span></p>
|
||||
</div>
|
||||
|
||||
<form>
|
||||
<form action="{{ route('auth.two_step_verify_post') }}" method="POST">
|
||||
@csrf
|
||||
|
||||
<input type="hidden" name="email" value="{{ $email }}">
|
||||
<div class="row">
|
||||
<div class="col-3">
|
||||
<div class="mb-3">
|
||||
<label for="digit1-input" class="visually-hidden">Dight 1</label>
|
||||
<input type="text"
|
||||
class="form-control form-control-lg bg-light border-light text-center"
|
||||
onkeyup="moveToNext(this, 2)" maxLength="1" id="digit1-input">
|
||||
onkeyup="moveToNext(this, 2)" maxLength="1" id="digit1-input"
|
||||
name="digit1" autocomplete="off">
|
||||
</div>
|
||||
</div><!-- end col -->
|
||||
|
||||
|
@ -92,7 +96,8 @@ class="form-control form-control-lg bg-light border-light text-center"
|
|||
<label for="digit2-input" class="visually-hidden">Dight 2</label>
|
||||
<input type="text"
|
||||
class="form-control form-control-lg bg-light border-light text-center"
|
||||
onkeyup="moveToNext(this, 3)" maxLength="1" id="digit2-input">
|
||||
onkeyup="moveToNext(this, 3)" maxLength="1" id="digit2-input"
|
||||
name="digit2" autocomplete="off">
|
||||
</div>
|
||||
</div><!-- end col -->
|
||||
|
||||
|
@ -101,7 +106,8 @@ class="form-control form-control-lg bg-light border-light text-center"
|
|||
<label for="digit3-input" class="visually-hidden">Dight 3</label>
|
||||
<input type="text"
|
||||
class="form-control form-control-lg bg-light border-light text-center"
|
||||
onkeyup="moveToNext(this, 4)" maxLength="1" id="digit3-input">
|
||||
onkeyup="moveToNext(this, 4)" maxLength="1" id="digit3-input"
|
||||
name="digit3" autocomplete="off">
|
||||
</div>
|
||||
</div><!-- end col -->
|
||||
|
||||
|
@ -110,15 +116,16 @@ class="form-control form-control-lg bg-light border-light text-center"
|
|||
<label for="digit4-input" class="visually-hidden">Dight 4</label>
|
||||
<input type="text"
|
||||
class="form-control form-control-lg bg-light border-light text-center"
|
||||
onkeyup="moveToNext(this, 4)" maxLength="1" id="digit4-input">
|
||||
onkeyup="moveToNext(this, 4)" maxLength="1"
|
||||
id="digit4-input" name="digit4" autocomplete="off">
|
||||
</div>
|
||||
</div><!-- end col -->
|
||||
</div>
|
||||
<div class="mt-3">
|
||||
<button type="submit" class="btn btn-success w-100">Verifikasi</button>
|
||||
</div>
|
||||
</form><!-- end form -->
|
||||
|
||||
<div class="mt-3">
|
||||
<button type="button" class="btn btn-success w-100">Verifikasi</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- end card body -->
|
||||
|
@ -126,8 +133,13 @@ class="form-control form-control-lg bg-light border-light text-center"
|
|||
<!-- end card -->
|
||||
|
||||
<div class="mt-4 text-center">
|
||||
<p class="mb-0">Tidak menerima email ? <a href="auth-pass-reset-basic.html"
|
||||
class="fw-semibold text-primary text-decoration-underline">Kirim ulang</a> </p>
|
||||
<form action="{{ route('auth.two_step_verify_resend') }}" method="POST">
|
||||
@csrf
|
||||
<input type="hidden" name="email" value="{{ $email }}">
|
||||
<p class="mb-0">Belum menerima email ? <input type="submit"
|
||||
class="btn btn-link m-0 p-0 fw-semibold" value="Kirim ulang">
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
@ -159,6 +171,7 @@ class="mdi mdi-heart text-danger"></i>
|
|||
<!-- end Footer -->
|
||||
</div>
|
||||
<!-- end auth-page-wrapper -->
|
||||
@include('sweetalert::alert')
|
||||
|
||||
<!-- JAVASCRIPT -->
|
||||
<script src="{{ asset('assets/libs/bootstrap/js/bootstrap.bundle.min.js') }}"></script>
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
|
||||
Route::controller(TwoStepVerifyController::class)->group(function () {
|
||||
Route::get('/verifikasi-akun/{email}', 'create')->name('two_step_verify');
|
||||
Route::post('/verifikasi-akun', 'store')->name('two_step_verify_post');
|
||||
Route::post('/verifikasi-akun/resend', 'resendEmail')->name('two_step_verify_resend');
|
||||
});
|
||||
|
||||
Route::controller(PasswordResetLinkController::class)->group(function () {
|
||||
|
|
Loading…
Reference in New Issue