-
-
Save saniyathossain/8f9183e8a144765966901c7788f1b49a to your computer and use it in GitHub Desktop.
A Laravel rule to ensure Gmail emails actually exist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
class ValidGmailAddress implements Rule | |
{ | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$response = Http::post('https://accounts.google.com/InputValidator?resource=signup&service=mail', ['input01' => [ | |
'Input' => 'GmailAddress', 'GmailAddress' => Str::before($value, '@gmail.com'), 'FirstName' => '', 'LastName' => '' | |
], 'Locale' => 'en'])->json(); | |
return data_get($response, 'input01.ErrorMessage') == 'That username is taken. Try another.'; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'The provided :attribute is not a valid Gmail address.'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment