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)
|
public function login(Request $request)
|
||||||
{
|
{
|
||||||
$credentials = $request->validate([
|
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
|
||||||
'email' => ['required', 'email'],
|
'email' => ['required', 'email'],
|
||||||
'password' => ['required', 'string'],
|
'password' => ['required', 'string'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (! Auth::attempt($credentials, $request->boolean('remember'))) {
|
if ($validator->fails()) {
|
||||||
return back()->withErrors(['email' => 'Email atau password tidak valid.'])->onlyInput('email');
|
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();
|
$request->session()->regenerate();
|
||||||
|
|
@ -42,18 +50,26 @@ public function login(Request $request)
|
||||||
|
|
||||||
public function register(Request $request)
|
public function register(Request $request)
|
||||||
{
|
{
|
||||||
$data = $request->validate([
|
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
|
||||||
'name' => ['required', 'string', 'max:100'],
|
'name' => ['required', 'string', 'max:100'],
|
||||||
'email' => ['required', 'email', 'max:150', 'unique:users,email'],
|
'email' => ['required', 'email', 'max:150', 'unique:users,email'],
|
||||||
'phone' => ['nullable', 'string', 'max:20'],
|
'phone' => ['nullable', 'string', 'max:20'],
|
||||||
'password' => ['required', 'string', 'min:6', 'confirmed'],
|
'password' => ['required', 'string', 'min:6', 'confirmed'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if ($validator->fails()) {
|
||||||
|
return back()->withErrors($validator)
|
||||||
|
->withInput()
|
||||||
|
->with('error_title', 'Registrasi Gagal')
|
||||||
|
->with('error', 'Silakan periksa kembali data Anda.');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
$user = User::create([
|
$user = User::create([
|
||||||
'name' => $data['name'],
|
'name' => $request->name,
|
||||||
'email' => $data['email'],
|
'email' => $request->email,
|
||||||
'phone' => $data['phone'] ?? null,
|
'phone' => $request->phone ?? null,
|
||||||
'password' => Hash::make($data['password']),
|
'password' => Hash::make($request->password),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user->assignRole('user');
|
$user->assignRole('user');
|
||||||
|
|
@ -61,7 +77,12 @@ public function register(Request $request)
|
||||||
Auth::login($user);
|
Auth::login($user);
|
||||||
$request->session()->regenerate();
|
$request->session()->regenerate();
|
||||||
|
|
||||||
return redirect()->route('home')->with('success', 'Registrasi berhasil.');
|
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)
|
public function logout(Request $request)
|
||||||
|
|
|
||||||
|
|
@ -17,18 +17,6 @@
|
||||||
</script>
|
</script>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
@if (session('error'))
|
|
||||||
<script>
|
|
||||||
Swal.fire({
|
|
||||||
icon: 'error',
|
|
||||||
title: 'Gagal!',
|
|
||||||
text: @json(session('error')),
|
|
||||||
confirmButtonColor: '#0f766e',
|
|
||||||
confirmButtonText: 'OK'
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
@endif
|
|
||||||
|
|
||||||
@if (session('info'))
|
@if (session('info'))
|
||||||
<script>
|
<script>
|
||||||
Swal.fire({
|
Swal.fire({
|
||||||
|
|
@ -89,18 +77,28 @@
|
||||||
</script>
|
</script>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
@if ($errors->any())
|
@if (session('error') || $errors->any())
|
||||||
<script>
|
<script>
|
||||||
Swal.fire({
|
let errorHtml = '';
|
||||||
icon: 'error',
|
let errorText = '';
|
||||||
title: 'Validasi Gagal',
|
|
||||||
html: `<div class="text-start">
|
@if($errors->any())
|
||||||
|
errorHtml = `<div class="text-start">
|
||||||
<ul class="mb-0">
|
<ul class="mb-0">
|
||||||
@foreach ($errors->all() as $error)
|
@foreach ($errors->all() as $error)
|
||||||
<li>{{ $error }}</li>
|
<li>{{ $error }}</li>
|
||||||
@endforeach
|
@endforeach
|
||||||
</ul>
|
</ul>
|
||||||
</div>`,
|
</div>`;
|
||||||
|
@elseif(session('error'))
|
||||||
|
errorText = @json(session('error'));
|
||||||
|
@endif
|
||||||
|
|
||||||
|
Swal.fire({
|
||||||
|
icon: 'error',
|
||||||
|
title: @json(session('error_title') ?? (session('error') ? 'Gagal!' : 'Validasi Gagal')),
|
||||||
|
html: errorHtml || undefined,
|
||||||
|
text: errorText || undefined,
|
||||||
confirmButtonColor: '#0f766e',
|
confirmButtonColor: '#0f766e',
|
||||||
confirmButtonText: 'OK'
|
confirmButtonText: 'OK'
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue