I have a laravel app and I want to limit the users registered only to a specific target group that owns an email from a company. I have tried something in my Registrar.php
public function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
public function create(array $data)
{
$result = null;
$confirmation_code = str_random(30);
$data = array_add($data, 'conf',$confirmation_code);
if(!$data['email'].ends_with(('email'), 'specificdomain.com')){
Flash::message('Welcome ' .$data["name"]. '. Thanks for registering. We have sent you a validation link in your e-mail address!');
Mail::send('emails.verify', $data, function($message) use ($data) {
$message->to($data['email'], $data['name'])
->subject('Verify your email address');
});
$result = User::create([
'name' => $data['name'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'confirmation_code' => $confirmation_code
]);
}else{
$result = Flash::warning('Hi ' .$data["name"]. '. We appreciate your interest on using our System. However at the moment we offer this service only to this company!');
//break;
}
return $result;
}
This throws the following exception
Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, Laracasts/Flash/Flash given.
And i cannot break at the else statement because i get the following:
Cannot break/continue 1 level
Apparently I have to return Users::create([....]) but to do so I have to keep this block outside of the if statement. If I do that i cannot check if the email domain is the required one. So I am asking, how can I integrate this in the public function validator(array $data){.....} block?
All the help is appreciated.
You could expand the email validation in your validator rules like:
'email' => 'required|email|max:255|unique:users|regex:/(.*)your\.domain\.com$/i',
(or pass it as an array if you need to pipe in your regex)
You can then add an array of messages to your validator like:
$messages = array(
'email.regex' => 'We appreciate your interest on using our System. However at the moment we offer this service only to this company!',
);
Where you call the Validator add messages as 3rd argument:
// Where $rules is the array you pass on now
$validator = Validator::make($data, $rules, $messages);
In the laravel documentation you can ready everything about responses.
You can not return a Flash. You can use Flash to (in your case) put a message into the Session, that will be removed after the request. I'm not entirely sure how you call the create function and what the expected result that is returned should be, but I would be consistent with this. Since you can now solve it with a validation message, you should only have to Flash a success message or an error.
I recently faced this issue using Laravel v5.3 and solved it by extending the Validator facade. The rule consisted of:
'email' => 'required|email|allowed_domain|max:255|unique:users'
And the extension was put into the app/Providers/AuthServiceProvider.php boot method:
Validator::extend('allowed_domain', function($attribute, $value, $parameters, $validator) {
return in_array(explode('@', $value)[1], $this->allowedDomains);
}, 'Domain not valid for registration.');
$this->allowedDomains being an array of allowed domains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With