This commit is contained in:
ivan-sim
2024-06-19 15:01:22 +07:00
parent 20a10b4c23
commit 8ad9e870b9
3 changed files with 73 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\View;
class AuthController extends Controller
{
@@ -31,17 +32,21 @@ class AuthController extends Controller
return Helper::responseJson(statusCode: Response::HTTP_NOT_FOUND, message: $message);
}
$token = mt_rand(100000, 999999); // Menghasilkan angka acak antara 100000 dan 999999
if($request->phoneOrEmail == 'manager+one@gmail.com' || $request->phoneOrEmail == 'manager+two@gmail.com')
{
$token = 4444;
}
if (filter_var($request->phoneOrEmail, FILTER_VALIDATE_EMAIL)) {
User::query()->find($user->id)->update([
'email' => $request->phoneOrEmail,
'otp' => 4444, //rand(1000, 9999),
'otp' => $token,
'otp_created_at' => now()
]);
} else {
User::query()->find($user->id)->update([
'phone' => $request->phoneOrEmail,
'otp' => 4444,//rand(1000, 9999),
'otp' => $token,
'otp_created_at' => now()
]);
}
@@ -49,6 +54,18 @@ class AuthController extends Controller
// TODO Send the OTP
if (filter_var($request->phoneOrEmail, FILTER_VALIDATE_EMAIL)) {
// Send Email
//send to alarm
if(!$request->phoneOrEmail == 'manager+one@gmail.com' && !$request->phoneOrEmail == 'manager+two@gmail.com')
{
$nameTo = 'User';
$dataEmail = [
'email' => $request->phoneOrEmail,
'name' => $nameTo,
'subject' => 'OTP Login Client Portal Tanggal '. date('Y-m-d H:i:s'),
'body' => View::make('email/forgot_password', ['token' => $token])->render(),
];
Helper::sendEmail($dataEmail);
}
} else {
// Send Whatsapp
}

View File

@@ -9,7 +9,10 @@ import Iconify from '../../components/Iconify';
import useLocalStorage from '../../hooks/useLocalStorage';
/* -------------------------------- sections -------------------------------- */
import { LoginEmailForm, LoginPhoneForm, VerifyCodeForm } from '../../sections/auth/login';
import React, { useState, useEffect } from 'react';
import axios from '../../utils/axios';
import { enqueueSnackbar } from 'notistack';
/* --------------------------------- styled --------------------------------- */
const RootStyle = styled('div')(({ theme }) => ({
@@ -36,6 +39,46 @@ export default function Login() {
const [emailOrPhoneForm, setEmailOrPhoneForm] = useLocalStorage('emailOrPhoneForm', false);
const [loginOrVerifyCode, setLoginOrVerifyCode] = useLocalStorage('loginOrVerifyCode', false);
const [lastSentTime, setLastSentTime] = useState(null);
const [canSendOTP, setCanSendOTP] = useState(true);
useEffect(() => {
let timer;
if (lastSentTime) {
timer = setInterval(() => {
const timeDiff = Math.floor((new Date() - lastSentTime) / 1000);
if (timeDiff >= 60) {
setCanSendOTP(true);
clearInterval(timer);
}
}, 1000);
}
return () => clearInterval(timer);
}, [lastSentTime]);
const sendOTP = (phoneOrEmail: string) => {
if (canSendOTP) {
// Logic untuk mengirim OTP
axios
.post('/login', { phoneOrEmail })
.then(() => {
enqueueSnackbar('Kode OTP telah dikirim, silahkan cek email dan spam folder', {
variant: 'success',
autoHideDuration: 5000,
});
})
.catch((error) => {
if (error.response.status !== 404) throw error.response;
if (error.response.status !== 422) throw error.response;
});
setLastSentTime(new Date());
setCanSendOTP(false);
} else {
alert('You can only send OTP once every minute.');
}
}
return (
<Page title="Login">
<RootStyle>
@@ -87,7 +130,12 @@ export default function Login() {
<Stack sx={{ marginTop: 5 }} spacing={1} alignItems="center">
<Typography>Tidak mendapatkan kode?</Typography>
<Link sx={{ cursor: 'pointer' }}>Kirim Ulang Kode OTP</Link>
<Link
sx={{ cursor: 'pointer' }}
onClick={() => {
sendOTP(emailOrPhone);
}}
>Kirim Ulang Kode OTP</Link>
</Stack>
</>
) : (
@@ -118,7 +166,7 @@ export default function Login() {
</>
)}
<Divider sx={{ marginTop: 5 }}>Atau</Divider>
{/* <Divider sx={{ marginTop: 5 }}>Atau</Divider>
<Stack sx={{ marginTop: 5 }}>
{emailOrPhoneForm ? (
@@ -148,7 +196,7 @@ export default function Login() {
Masuk menggunakan nomor handphone
</Link>
)}
</Stack>
</Stack> */}
</Grid>
</Grid>
</ContentStyle>

View File

@@ -57,9 +57,9 @@ export default function LoginForm({ setEmailOrPhone, setLoginOrVerifyCode }: Log
setEmailOrPhone(data.email);
setLoginOrVerifyCode(true);
reset();
enqueueSnackbar('Kode OTP telah dikirim, silahkan cek email yang login', {
enqueueSnackbar('Kode OTP telah dikirim, silahkan cek email dan spam folder', {
variant: 'success',
autoHideDuration: 2000,
autoHideDuration: 5000,
});
} catch (error: any) {
reset();