feat: implement sweetalert for validation errors specifically on register and login
This commit is contained in:
parent
26863d4f04
commit
3e05b3b981
|
|
@ -21,13 +21,21 @@ public function showRegister()
|
|||
|
||||
public function login(Request $request)
|
||||
{
|
||||
$credentials = $request->validate([
|
||||
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
|
||||
'email' => ['required', 'email'],
|
||||
'password' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
if (! Auth::attempt($credentials, $request->boolean('remember'))) {
|
||||
return back()->withErrors(['email' => 'Email atau password tidak valid.'])->onlyInput('email');
|
||||
if ($validator->fails()) {
|
||||
return back()->withErrors($validator)->withInput()
|
||||
->with('error_title', 'Login Gagal')
|
||||
->with('error', 'Silakan periksa kembali email atau password Anda.');
|
||||
}
|
||||
|
||||
if (! Auth::attempt($request->only('email', 'password'), $request->boolean('remember'))) {
|
||||
return back()->withErrors(['email' => 'Email atau password tidak valid.'])->onlyInput('email')
|
||||
->with('error_title', 'Login Gagal')
|
||||
->with('error', 'Kredensial tidak valid.');
|
||||
}
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
|
@ -42,26 +50,39 @@ public function login(Request $request)
|
|||
|
||||
public function register(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
|
||||
'name' => ['required', 'string', 'max:100'],
|
||||
'email' => ['required', 'email', 'max:150', 'unique:users,email'],
|
||||
'phone' => ['nullable', 'string', 'max:20'],
|
||||
'password' => ['required', 'string', 'min:6', 'confirmed'],
|
||||
]);
|
||||
|
||||
$user = User::create([
|
||||
'name' => $data['name'],
|
||||
'email' => $data['email'],
|
||||
'phone' => $data['phone'] ?? null,
|
||||
'password' => Hash::make($data['password']),
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
return back()->withErrors($validator)
|
||||
->withInput()
|
||||
->with('error_title', 'Registrasi Gagal')
|
||||
->with('error', 'Silakan periksa kembali data Anda.');
|
||||
}
|
||||
|
||||
$user->assignRole('user');
|
||||
try {
|
||||
$user = User::create([
|
||||
'name' => $request->name,
|
||||
'email' => $request->email,
|
||||
'phone' => $request->phone ?? null,
|
||||
'password' => Hash::make($request->password),
|
||||
]);
|
||||
|
||||
Auth::login($user);
|
||||
$request->session()->regenerate();
|
||||
$user->assignRole('user');
|
||||
|
||||
return redirect()->route('home')->with('success', 'Registrasi berhasil.');
|
||||
Auth::login($user);
|
||||
$request->session()->regenerate();
|
||||
|
||||
return redirect()->route('home')->with('success', 'Registrasi berhasil. Selamat datang!');
|
||||
} catch (\Exception $e) {
|
||||
return back()->withInput()
|
||||
->with('error_title', 'Registrasi Gagal')
|
||||
->with('error', 'Terjadi kesalahan pada sistem.');
|
||||
}
|
||||
}
|
||||
|
||||
public function logout(Request $request)
|
||||
|
|
|
|||
|
|
@ -17,18 +17,6 @@
|
|||
</script>
|
||||
@endif
|
||||
|
||||
@if (session('error'))
|
||||
<script>
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Gagal!',
|
||||
text: @json(session('error')),
|
||||
confirmButtonColor: '#0f766e',
|
||||
confirmButtonText: 'OK'
|
||||
});
|
||||
</script>
|
||||
@endif
|
||||
|
||||
@if (session('info'))
|
||||
<script>
|
||||
Swal.fire({
|
||||
|
|
@ -89,18 +77,28 @@
|
|||
</script>
|
||||
@endif
|
||||
|
||||
@if ($errors->any())
|
||||
@if (session('error') || $errors->any())
|
||||
<script>
|
||||
let errorHtml = '';
|
||||
let errorText = '';
|
||||
|
||||
@if($errors->any())
|
||||
errorHtml = `<div class="text-start">
|
||||
<ul class="mb-0">
|
||||
@foreach ($errors->all() as $error)
|
||||
<li>{{ $error }}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</div>`;
|
||||
@elseif(session('error'))
|
||||
errorText = @json(session('error'));
|
||||
@endif
|
||||
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Validasi Gagal',
|
||||
html: `<div class="text-start">
|
||||
<ul class="mb-0">
|
||||
@foreach ($errors->all() as $error)
|
||||
<li>{{ $error }}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</div>`,
|
||||
title: @json(session('error_title') ?? (session('error') ? 'Gagal!' : 'Validasi Gagal')),
|
||||
html: errorHtml || undefined,
|
||||
text: errorText || undefined,
|
||||
confirmButtonColor: '#0f766e',
|
||||
confirmButtonText: 'OK'
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue