Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure validity Value is Cast to Integer When Adding to created_at Timestamp #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shahsawoodshinwari
Copy link

Pull Request Description

This pull request addresses an issue where the validity value was not being properly cast to an integer before being added to the created_at timestamp. This could result in errors when the validity value was stored as a string in the database.

Changes:

  • Ensure the validity value is cast to an integer before being used in the validate method.
  • Update the validate method in the Otp class to properly handle the type conversion.

Issue:
When the validity value is stored as a string in the database, adding this value to the created_at timestamp causes an error since the addMinutes method expects an integer. This fix ensures that the validity value is always treated as an integer, preventing such errors.

Implementation:

public function validate(string $identifier, string $token): object
{
  $otp = Model::where('identifier', $identifier)->where('token', $token)->first();

  if ($otp instanceof Model) {
    if ($otp->valid) {
      $now = Carbon::now();
      $validity = $otp->created_at->addMinutes((int) $otp->validity); // Ensure casting to int

      $otp->update(['valid' => false]);

      if (strtotime($validity) < strtotime($now)) {
        return (object) [
          'status' => false,
          'message' => 'OTP Expired'
        ];
      }

      $otp->update(['valid' => false]);

      return (object) [
        'status' => true,
        'message' => 'OTP is valid'
      ];
    }

    $otp->update(['valid' => false]);

    return (object) [
      'status' => false,
      'message' => 'OTP is invalid'
    ];
  }

  return (object) [
    'status' => false,
    'message' => 'OTP not found'
  ];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant