Skip to content

Commit

Permalink
Rename methods and variables
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoraddatz committed Apr 22, 2024
1 parent 1f2b263 commit d7365d1
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
26 changes: 13 additions & 13 deletions resources/views/code.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<script src="https://cdn.tailwindcss.com"></script>

<script>
const code_length = {{ config('totp-login.code.length') }};
const totp_code_length = {{ config('totp-login.code.length') }};
</script>
</head>
<body class="antialiased">
Expand Down Expand Up @@ -50,7 +50,7 @@

{{-- Code inputs --}}
<div class="flex justify-center" x-data="code()">
<template x-for="(l,i) in code_length" :key="`code_field_${i}`">
<template x-for="(l,i) in totp_code_length" :key="`code_field_${i}`">
<input :id="`code_field_${i}`"
:autofocus="i === 0"
:aria-label="`Code Element ${i + 1}`"
Expand Down Expand Up @@ -127,12 +127,12 @@ class="bg-transparent cursor-pointer text-light dark:text-gray-200 hover:underli
paste = paste.replace(/\s+/g, '');
if (isNaN(parseInt(paste)) || paste.length !== pin_length) {
if (isNaN(parseInt(paste)) || paste.length !== totp_code_length) {
return;
}
for (let i = 0; i < paste.length; i++) {
document.getElementById(`pin_field_${i}`).value = paste[i];
document.getElementById(`code_field_${i}`).value = paste[i];
}
document.getElementById(`submit`).focus();
Expand All @@ -143,27 +143,27 @@ class="bg-transparent cursor-pointer text-light dark:text-gray-200 hover:underli
function code() {
return {
resetValue(i) {
for (let x = 0; x < pin_length; x++) {
if (x >= i) document.getElementById(`pin_field_${x}`).value = '';
for (let x = 0; x < totp_code_length; x++) {
if (x >= i) document.getElementById(`code_field_${x}`).value = '';
}
},
stepForward(i) {
// Last input has been filled; there is no next input
if (document.getElementById(`pin_field_${i}`).value && i === pin_length - 1) {
if (document.getElementById(`code_field_${i}`).value && i === totp_code_length - 1) {
document.getElementById(`submit`).focus();
return;
}
// Return if the next input is already filled (conflict with paste)
if (document.getElementById(`pin_field_${i + 1}`).value) {
if (document.getElementById(`code_field_${i + 1}`).value) {
return;
}
// Next input is empty
if (document.getElementById(`pin_field_${i}`).value && i !== pin_length - 1) {
document.getElementById(`pin_field_${i + 1}`).focus();
document.getElementById(`pin_field_${i + 1}`).value = '';
if (document.getElementById(`code_field_${i}`).value && i !== totp_code_length - 1) {
document.getElementById(`code_field_${i + 1}`).focus();
document.getElementById(`code_field_${i + 1}`).value = '';
}
},
Expand All @@ -172,8 +172,8 @@ function code() {
return;
}
document.getElementById(`pin_field_${i - 1}`).focus();
document.getElementById(`pin_field_${i - 1}`).value = '';
document.getElementById(`code_field_${i - 1}`).focus();
document.getElementById(`code_field_${i - 1}`).value = '';
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/notification.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{!! $introLines[0] !!}

<div style="text-align:center;width:100%;margin:50px 0">
@foreach($pin as $digit)
@foreach($code as $digit)
<strong style="border:1px solid blue;padding:15px;font-size:20px;border-radius:5px;margin:0 2px">{{ $digit }}</strong>
@endforeach
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/HandleCodeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class HandleCodeRequest extends Controller
{
protected ?string $pin = null;
protected ?string $code = null;

/**
* @var \Illuminate\Database\Eloquent\Model
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/CreateAndSendLoginCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function handle(): void
{
$columns = config('totp-login.columns');
$notification = config('totp-login.notification');
$code = self::createPin();
$code = self::createCode();

$this->user->{$columns['code']} = Hash::make($code);
$this->user->{$columns['code_valid_until']} = now()->addSeconds(config('totp-login.code.expires_in'));
Expand All @@ -34,7 +34,7 @@ public function handle(): void
/**
* @throws \Exception
*/
public static function createPin(): string
public static function createCode(): string
{
return str_pad(
(string) random_int(0, (int) str_repeat('9', config('totp-login.code.length'))),
Expand Down
2 changes: 1 addition & 1 deletion src/Notifications/LoginCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class LoginCode extends Notification
{
public function __construct(protected readonly string $pin, protected readonly string $ip)
public function __construct(protected readonly string $code, protected readonly string $ip)
{
}

Expand Down
8 changes: 4 additions & 4 deletions src/Requests/CodeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function authenticate(): void
}

$this->ensureIsNotRateLimited();
$this->ensurePinIsNotExpired();
$this->validatePin();
$this->ensureCodeIsNotExpired();
$this->validateCode();

RateLimiter::clear($this->throttleKey());
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public function ensureIsNotRateLimited(): void
/**
* @throws \Illuminate\Validation\ValidationException
*/
public function ensurePinIsNotExpired(): void
public function ensureCodeIsNotExpired(): void
{
if (now() < $this->user->{config('totp-login.columns.code_valid_until')}) {
return;
Expand All @@ -105,7 +105,7 @@ public function ensurePinIsNotExpired(): void
/**
* @throws \Illuminate\Validation\ValidationException
*/
public function validatePin(): void
public function validateCode(): void
{
$this->formatCode();

Expand Down

0 comments on commit d7365d1

Please sign in to comment.