refactor: use complete AJAX fetch for auth forms without intermediate redirect page
This commit is contained in:
parent
ee51d217d7
commit
66a035dde1
|
|
@ -27,12 +27,24 @@ public function login(Request $request)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
|
if ($request->wantsJson() || $request->ajax()) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => $validator->errors()->first()
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
return back()->withErrors($validator)->withInput()
|
return back()->withErrors($validator)->withInput()
|
||||||
->with('error_title', 'Login Gagal')
|
->with('error_title', 'Login Gagal')
|
||||||
->with('error', 'Silakan periksa kembali email atau password Anda.');
|
->with('error', 'Silakan periksa kembali email atau password Anda.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! Auth::attempt($request->only('email', 'password'), $request->boolean('remember'))) {
|
if (! Auth::attempt($request->only('email', 'password'), $request->boolean('remember'))) {
|
||||||
|
if ($request->wantsJson() || $request->ajax()) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => 'Email atau password tidak valid.'
|
||||||
|
], 401);
|
||||||
|
}
|
||||||
return back()->withErrors(['email' => 'Email atau password tidak valid.'])->onlyInput('email')
|
return back()->withErrors(['email' => 'Email atau password tidak valid.'])->onlyInput('email')
|
||||||
->with('error_title', 'Login Gagal')
|
->with('error_title', 'Login Gagal')
|
||||||
->with('error', 'Kredensial tidak valid.');
|
->with('error', 'Kredensial tidak valid.');
|
||||||
|
|
@ -42,12 +54,24 @@ public function login(Request $request)
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|
||||||
if ($user->hasRole('admin')) {
|
if ($user->hasRole('admin')) {
|
||||||
return back()->with('auth_success', 'Login berhasil sebagai admin.')
|
if ($request->wantsJson() || $request->ajax()) {
|
||||||
->with('redirect_url', route('admin.dashboard'));
|
return response()->json([
|
||||||
|
'success' => true,
|
||||||
|
'message' => 'Login berhasil sebagai admin.',
|
||||||
|
'redirect_url' => route('admin.dashboard')
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return redirect()->route('admin.dashboard')->with('success', 'Login berhasil sebagai admin.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return back()->with('auth_success', 'Login berhasil.')
|
if ($request->wantsJson() || $request->ajax()) {
|
||||||
->with('redirect_url', route('home'));
|
return response()->json([
|
||||||
|
'success' => true,
|
||||||
|
'message' => 'Login berhasil.',
|
||||||
|
'redirect_url' => route('home')
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return redirect()->route('home')->with('success', 'Login berhasil.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register(Request $request)
|
public function register(Request $request)
|
||||||
|
|
@ -60,6 +84,12 @@ public function register(Request $request)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
|
if ($request->wantsJson() || $request->ajax()) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => $validator->errors()->first()
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
return back()->withErrors($validator)
|
return back()->withErrors($validator)
|
||||||
->withInput()
|
->withInput()
|
||||||
->with('error_title', 'Registrasi Gagal')
|
->with('error_title', 'Registrasi Gagal')
|
||||||
|
|
@ -79,9 +109,21 @@ public function register(Request $request)
|
||||||
Auth::login($user);
|
Auth::login($user);
|
||||||
$request->session()->regenerate();
|
$request->session()->regenerate();
|
||||||
|
|
||||||
return back()->with('auth_success', 'Registrasi berhasil. Selamat datang!')
|
if ($request->wantsJson() || $request->ajax()) {
|
||||||
->with('redirect_url', route('home'));
|
return response()->json([
|
||||||
|
'success' => true,
|
||||||
|
'message' => 'Registrasi berhasil. Selamat datang!',
|
||||||
|
'redirect_url' => route('home')
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return redirect()->route('home')->with('success', 'Registrasi berhasil. Selamat datang!');
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
if ($request->wantsJson() || $request->ajax()) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => 'Terjadi kesalahan pada sistem.'
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
return back()->withInput()
|
return back()->withInput()
|
||||||
->with('error_title', 'Registrasi Gagal')
|
->with('error_title', 'Registrasi Gagal')
|
||||||
->with('error', 'Terjadi kesalahan pada sistem.');
|
->with('error', 'Terjadi kesalahan pada sistem.');
|
||||||
|
|
|
||||||
|
|
@ -88,8 +88,49 @@
|
||||||
return showError('Validasi Gagal', 'Password tidak boleh kosong.');
|
return showError('Validasi Gagal', 'Password tidak boleh kosong.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all validation passes, submit the form
|
// Instead of standard submit, do AJAX fetch
|
||||||
this.submit();
|
const submitBtn = form.querySelector('button[type="submit"]');
|
||||||
|
const originalBtnHtml = submitBtn.innerHTML;
|
||||||
|
submitBtn.disabled = true;
|
||||||
|
submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>Memproses...';
|
||||||
|
|
||||||
|
const formData = new FormData(form);
|
||||||
|
|
||||||
|
fetch(form.action, {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
headers: {
|
||||||
|
'X-Requested-With': 'XMLHttpRequest',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
Swal.fire({
|
||||||
|
icon: 'success',
|
||||||
|
title: 'Berhasil!',
|
||||||
|
text: data.message,
|
||||||
|
timer: 2000,
|
||||||
|
timerProgressBar: true,
|
||||||
|
showConfirmButton: false,
|
||||||
|
allowOutsideClick: false,
|
||||||
|
allowEscapeKey: false,
|
||||||
|
willClose: () => {
|
||||||
|
window.location.href = data.redirect_url;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
submitBtn.disabled = false;
|
||||||
|
submitBtn.innerHTML = originalBtnHtml;
|
||||||
|
showError('Login Gagal', data.message || 'Kredensial tidak valid.');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
submitBtn.disabled = false;
|
||||||
|
submitBtn.innerHTML = originalBtnHtml;
|
||||||
|
showError('Error', 'Terjadi kesalahan pada sistem.');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -132,8 +132,49 @@
|
||||||
return showError('Validasi Gagal', 'Anda harus menyetujui syarat dan ketentuan.');
|
return showError('Validasi Gagal', 'Anda harus menyetujui syarat dan ketentuan.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all validation passes, submit the form
|
// Instead of standard submit, do AJAX fetch
|
||||||
this.submit();
|
const submitBtn = form.querySelector('button[type="submit"]');
|
||||||
|
const originalBtnHtml = submitBtn.innerHTML;
|
||||||
|
submitBtn.disabled = true;
|
||||||
|
submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>Memproses...';
|
||||||
|
|
||||||
|
const formData = new FormData(form);
|
||||||
|
|
||||||
|
fetch(form.action, {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
headers: {
|
||||||
|
'X-Requested-With': 'XMLHttpRequest',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
Swal.fire({
|
||||||
|
icon: 'success',
|
||||||
|
title: 'Berhasil!',
|
||||||
|
text: data.message,
|
||||||
|
timer: 2000,
|
||||||
|
timerProgressBar: true,
|
||||||
|
showConfirmButton: false,
|
||||||
|
allowOutsideClick: false,
|
||||||
|
allowEscapeKey: false,
|
||||||
|
willClose: () => {
|
||||||
|
window.location.href = data.redirect_url;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
submitBtn.disabled = false;
|
||||||
|
submitBtn.innerHTML = originalBtnHtml;
|
||||||
|
showError('Registrasi Gagal', data.message || 'Silakan periksa kembali data Anda.');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
submitBtn.disabled = false;
|
||||||
|
submitBtn.innerHTML = originalBtnHtml;
|
||||||
|
showError('Error', 'Terjadi kesalahan pada sistem.');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,3 @@
|
||||||
@if (session('auth_success'))
|
|
||||||
<script>
|
|
||||||
Swal.fire({
|
|
||||||
icon: 'success',
|
|
||||||
title: 'Berhasil!',
|
|
||||||
text: @json(session('auth_success')),
|
|
||||||
timer: 2000,
|
|
||||||
timerProgressBar: true,
|
|
||||||
showConfirmButton: false,
|
|
||||||
willClose: () => {
|
|
||||||
window.location.href = @json(session('redirect_url'));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
@endif
|
|
||||||
|
|
||||||
@if (session('success'))
|
@if (session('success'))
|
||||||
<script>
|
<script>
|
||||||
Swal.fire({
|
Swal.fire({
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue