laravel-otp
laravel-otp copied to clipboard
Add ValidationRule
add a validation rule to validate the token in the request
@mahdimsr Thanks for your PR. I'd like to know what your idea is about this PR? This validation rule just returns true or false. In case we need the user validated by mobile and token to login user.
For example, you are using sanctum:
$user = OTP()->validate('+989389599530', 'token_123');
$token = $user->createToken('ios')->plainTextToken;
auth()->login($user);
@mahdimsr Thanks for your PR. I'd like to know what your idea is about this PR? This validation rule just returns true or false. In case we need the user validated by mobile and token to login user.
For example, you are using sanctum:
$user = OTP()->validate('+989389599530', 'token_123'); $token = $user->createToken('ios')->plainTextToken; auth()->login($user);
the validation you used in your example is enough to validate and check. but what if the developer wants to return some error message if the token isn't valid? two options are to choose from:
- use the validate function like your example and return an error message in this way, you have to return the error badge message manually
- use request validate rule
@mahdimsr I agree with you. But there is a problem. When you call the validate method, this method revokes the token, therefore, we lose the token and we can not find the user, right?
IMO, we can have a method something like that in the Fouladgar\OTP\OTPBroker class:
...
public function isValidToken(OTPNotifiable $user, string $token): bool
{
return $this->tokenExists($user,$token);
}
...
And in our rule, use this method, not the validate method.
@mahdimsr I agree with you. But there is a problem. When you call the
validatemethod, this method revokes the token, therefore, we lose the token and we can not find the user, right? IMO, we can have a method something like that in theFouladgar\OTP\OTPBrokerclass:... public function isValidToken(OTPNotifiable $user, string $token): bool { return $this->tokenExists($user,$token); } ...And in our rule, use this method, not the
validatemethod.
oh, I didn't recognize that. you're right :). I will fix it. thanks for the comment
Dear @mahdimsr Because this issue seems to be inactive for quite some time now. I've closed this PR. Please feel free to reopen it.