This commit is contained in:
2023-12-03 17:48:11 +05:00
parent 5f506bab65
commit 5e814d89fa
5 changed files with 116 additions and 40 deletions

View File

@@ -9,6 +9,7 @@ use App\Rules\PhoneCodeVerification;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class ResetPasswordController extends Controller
{
@@ -27,20 +28,38 @@ class ResetPasswordController extends Controller
{
$request->validate([
'username' => ['required', 'string', 'max:250', 'exists:users,username'],
'verification' => ['nullable', 'integer', 'digits:5'],
'password' => ['bail', 'nullable', 'string', 'min:8', 'confirmed'],
'verification' => ['nullable', 'integer', 'digits:5', Rule::requiredIf(fn () => $request->filled('step-verification'))],
'step-sms' => ['nullable'],
'step-verification' => ['nullable'],
'step-password' => ['nullable'],
'password' => ['bail', 'nullable', 'string', 'min:8', 'confirmed', Rule::requiredIf(fn () => $request->filled('step-password'))],
]);
if ($request->filled('verification')) {
return $this->verify();
}
$user = User::where('username', $request->username)->first();
// sendSMSVerification($user->phone);
$phone_code = rand(10000, 99999);
$verification = Verification::where(['username' => $user->phone])->first();
$verification ? $verification->update(['code' => $phone_code]) : Verification::create(['username' => $user->phone, 'code' => $phone_code]);
if ($request->filled('step-sms') && $request->isNotFilled('step-verification') && $request->isNotFilled('step-password')) {
return $this->sendVerification($request, $user);
}
if ($request->filled('step-verification') && $request->isNotFilled('step-password')) {
return $this->verify($request, $user);
}
if ($request->filled('step-password')) {
return $this->updatePassword($request, $user);
}
return response()->json();
}
/**
* Send verification code
* @param Request $request
* @param User $user
*/
public function sendVerification(Request $request, User $user): JsonResponse
{
sendSMSVerification($user->phone);
return response()->json([
'step' => 1,
@@ -50,10 +69,12 @@ class ResetPasswordController extends Controller
/**
* Verify phone number
* @param Request $request
* @param User $user
*/
public function verify(): JsonResponse
public function verify(Request $request, User $user): JsonResponse
{
$verification = Verification::where('username', $request->username)
$verification = Verification::where('username', $user->phone)
->where('code', $request->verification)
->first();
@@ -70,7 +91,22 @@ class ResetPasswordController extends Controller
return response()->json([
'step' => 2,
'message' => __("Now you can set your password, but please make sure that you dont forget it")
'message' => __("Now you can set your password, but please make sure that you don't forget it!")
]);
}
/**
* Update password
* @param Request $request
* @param User $user
*/
public function updatePassword(Request $request, User $user): JsonResponse
{
$user->update(['password' => bcrypt($request->password)]);
return response()->json([
'step' => 3,
'message' => __('Your password has been updated')
]);
}
}

View File

@@ -217,9 +217,12 @@
"Username": "Ulanyjy ady",
"Go to login page": "Login sahypa geç",
"Submit": "Tassyklamak",
"Verification": "Tassyklamak",
"verification": "tassyklamak",
"Verification code": "Tassyklaýyş belgi",
"Please, verify your phone": "Telefon beligiňizi tassyklamagyňyzy haýyş edýäris.",
"Enter your username to continue": "Dowam etmek üçin ulanyjy adyny giriziň",
"Incorrect verification code": "Nädogry belgi",
"Now you can set your password, but please make sure that you dont forget it!": "Indi açar sözüni täzeläp bilersiňiz, ýöne ýatdan çykarmaň!"
"Now you can set your password, but please make sure that you don't forget it!": "Indi açar sözüni täzeläp bilersiňiz, ýöne ýatdan çykarmaň!",
"Your password has been updated": "Siziň açar sözüňiz üýtgedildi"
}

7
migrate.md Normal file
View File

@@ -0,0 +1,7 @@
# Status
**pending**
**register**
**processing**
**completed**
**cancelled**

View File

@@ -43,3 +43,38 @@ function removeValidationClasess() {
element.innerHTML = ''
})
}
function showVerificationCodeBox() {
$_ID('verification-code-box').classList.remove('hidden')
$_ID('verification-code-box').insertAdjacentHTML('beforeend', `
<input
class="form-control form-input form-input-bordered w-full"
id="verification"
type="text"
name="verification"
value=""
>
<input type="hidden" name="step-verification" value="1">
<span id="verification-error-box" class="text-red-500 text-italic error-box"></span>
`)
}
function showPasswordBox() {
$_ID('username-box').classList.add('hidden')
$_ID('verification-code-box').innerHTML = ''
$_ID('reset-password-container').classList.remove('hidden')
$_ID('password-box').insertAdjacentHTML('beforeend', `
<input class="form-control form-input form-input-bordered w-full" id="password" type="password" name="password">
<input type="hidden" name="step-password" value="1">
<span id="password-error-box" class="text-red-500 text-italic error-box"></span>
`)
$_ID('password-confirm-box').insertAdjacentHTML('beforeend', `
<input class="form-control form-input form-input-bordered w-full" id="password_confirmation" type="password" name="password_confirmation">
<span id="password_confirmation-error-box" class="text-red-500 text-italic error-box"></span>
`)
}

View File

@@ -40,6 +40,7 @@
class="form-control form-input form-input-bordered w-full"
autofocus=""
>
<input type="hidden" name="step-sms" value="1">
<span id="username-error-box" class="text-red-500 text-italic error-box"></span>
</div>
@@ -48,31 +49,19 @@
<label class="block mb-2" for="verification">
{{ __('Verification code') }}
</label>
<input
class="form-control form-input form-input-bordered w-full"
id="verification"
type="text"
name="verification"
value=""
>
<span id="verification-error-box" class="text-red-500 text-italic error-box"></span>
</div>
<div class="hidden" id="reset-password-container">
<div class="mb-6">
<div class="mb-6" id="password-box">
<label class="block mb-2" for="password">
{{ __('Password') }}
</label>
<input class="form-control form-input form-input-bordered w-full" id="password" type="password" name="password">
</div>
<div class="mb-6">
<div class="mb-6" id="password-confirm-box">
<label class="block mb-2" for="password_confirmation">
{{ __('Confirm Password') }}
</label>
<input class="form-control form-input form-input-bordered w-full" id="password_confirmation" type="password" name="password_confirmation">
</div>
</div>
@@ -91,35 +80,41 @@
<script>
async function resetPassword(event) {
const response = await postData(event.target.action, getFormData(event))
if (response.errors) {
console.log(response.errors);
loopObject(response.errors, item => addValidationClasses(item))
} else {
removeValidationClasess()
if (response.step === 1) {
$_ID('verification-code-box').classList.remove('hidden')
showVerificationCodeBox()
Swal.fire({
title: '{{ __('Verification code') }}',
text: response.message,
icon: 'info'
})
title: '{{ __('Verification code') }}',
text: response.message,
icon: 'info'
})
}
if (response.step === 2) {
$_ID('username-box').classList.add('hidden')
$_ID('verification-code-box').classList.add('hidden')
$_ID('reset-password-container').classList.remove('hidden')
showPasswordBox()
Swal.fire({
title: '{{ __('Reset password') }}',
title: '{{ __('Reset Password') }}',
text: response.message,
icon: 'warning'
})
}
if (response.step === 3) {
await Swal.fire({
title: response.message,
showDenyButton: false,
showCancelButton: false,
})
window.location.href = '{{ route('login') }}'
}
}
}
</script>