I want to validate user using sentinel and i want to check that unique user email_address my post request looks like:
public function postRegister(Request $request)
{
$user = Sentinel::register($request->all());
$activation = Activation::create($user);
$this->sendEmail($user, $activation->code);
return redirect()->back()->with([
'sucess' => 'user registered successfully'
]);
}
Here i am adding first_name , last_name , email_address and password but i want to validate these fields and with unique email_address?
How i can do that?
Your help will be highly appreciated!
For example, you can use this https://laravel.com/docs/5.6/validation
$validatedData = $request->validate([
'email' => 'required|email|unique:users|max:255',
]);
After this, put $validateData to register
$user = Sentinel::register($validatedData);
But, for first step, i recommended use Custom request class, and put validation in rules() method. https://laravel.com/docs/5.6/validation#creating-form-requests
Related
I have one form in frontend where I have there is some city details , rooms details and user registration in one form like I have city name , room name , address etc email addresss and password in same form and I have done 2 logics in one controller for creating cities and registering user
It is saving the both data in correct table in the database
but I want that first user should register and if user is vcerified only the room details should be saved in database
I am in confusion wheather to apply if again or what
public function checkLogin(Request $request)
{
$user = User::create([
'name'=>$request->name,
'email'=>$request->email,
'password'=>$request->password,
'role_id' => config('quickadmin.default_role_id'),
]);
if ($user) {
if (Auth::check()) {
$city = TotalCity::create([
'name'=>$request->name,
'created_by'=>$request->created_by_id,
]);
}
return redirect()->to('/admin/home');
}
}
Let me show you how I'd probably write this logic:
public function checkLogin(Request $request)
{
$user = User::firstOrCreate([
'email'=> $request->email,
],
[
'name'=> $request->name,
'password'=> bcrypt($request->password),
'role_id' => config('quickadmin.default_role_id'),
]);
if (Auth::check()) {
// it's not clear if you utilize `email_verified_at`, if so
// if (Auth::check() && Auth::user()->email_verified_at) {
$city = TotalCity::create([
'name'=>$request->name,
'created_by'=> Auth::user()->id, // or $user->id depending on your preference
]);
}
return redirect('/admin/home');
}
The firstOrCreate() checks if an entry with that email exists, it gets it, otherwise creates it.
Furthermore, if I want to check for Authentication, I'd use 'auth' middleware in my route.
Route::get('example', 'ExampleController#checkLogin')->middleware('auth');
That removes the need of entire check:
if (Auth::check()) { ... }
I am trying to disable the password verification system from my laravel website. I want to login my users using only their first name and last name. Form wise and register wise and database wise, password field has been removed completely. But in login controller, i am having some issues, it does not seem to work. Here is my code:
public function login(Request $request)
{
$first_name = $request->first_name;
$last_name = $request->last_name;
$user = User::where(['first_name' => $first_name, 'last_name' => $last_name])->first();
if (!$user) {
return redirect()->back()->withInput($request->only('first_name', 'last_name'))->withErrors([
'first_name' => 'We could not find you in our database, if you think this is a mistake kindly contact the site administrators',
]);
}
Auth::login($user);
return redirecte('/');
}
in the above code, i am getting the error message
We could not find you in our database, if you think this is a mistake kindly contact the site administrators
regardless of what info (true of false) i insert in my form.
Yes thank you #laravel levaral for answering, but i found out the problem.
I am going to quote a user from laracasts
If you're going to group multiple where clauses into a single where(), each needs to be it's own array, within an array. You're sending a single array. You're also using =>, which isn't correct. The parameters for each where statement are separated by commas.
so for whoever wants to see the new working code:
public function login(Request $request)
{
$first_name = $request->first_name;
$last_name = $request->last_name;
$user = User::where('first_name', $first_name)
->where('last_name', $last_name)
->first();
if (!$user) {
return redirect()->back()->withInput($request->only('first_name', 'last_name'))->withErrors([
'first_name' => 'We could not find you in our database, if you think this is a mistake kindly contact the site administrators',
]);
}
Auth::login($user);
return redirect('/');
}
First of all, you have to check either the first_nameand last_name matches the database.
$user = User::where(['first_name' => $first_name, 'last_name' => $last_name])->first()
You have a problem in above lines.
public function login(Request $request)
{
$first_name = $request->first_name;
$last_name = $request->last_name;
$user = User::where(['first_name' => $first_name, 'last_name' => $last_name])->first();
if (!$user) {
return redirect()->back()->withInput($request->only('first_name', 'last_name'))->withErrors([
'first_name' => 'We could not find you in our database, if you think this is a mistake kindly contact the site administrators',
]);
}
Auth::loginUsingId($user->id);
return redirecte('/');
}
The following code is the implementation of the authentication based on whether or not a user enters a password in the edit page of user data.
How could I simplify this code using only a few methods?
...
$user = User::findOrFail($id); //Get role specified by id
if($request->password === null){
$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required|email|unique:users,email,'.$id
]);
$request->password = $user->password;
}
else{
//Validate name, email and password fields
$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required|email|unique:users,email,'.$id,
'password'=>'required|min:6|confirmed'
]);
}
$input = $request->only(['name', 'email', 'password']); //Retreive the name, email and password fields
$roles = $request['roles']; //Retreive all roles
$user->fill($input)->save();
...
Specs
Laravel ver.5.6
$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required|email|unique:users,email,'.$id,
'password'=>'nullable|required|min:6|confirmed'
]);
Use nullable rule in password validation. White saving the password use code like this:
if($request->password){
$user->password = bcrypt($request->password);
}
Hello friends I am using users table column like (USERNAME,EMAIL,PASSWORD)
if i am changing to column name as small letters is working fine. To Change column name as caps is not working give me any suggestion
This is my controller
public function postLogin(Request $request)
{
$this->validate($request, array('username' => 'required', 'password' => 'required'));
$credentials = $request->only('USERNAME', 'PASSWORD');
if (Auth::validate($credentials))
{
$user = Auth::getLastAttempted();
Auth::login($user, $request->has('remember'));
return redirect()->intended($this->redirectPath());
}
return redirect($this->loginPath())
->withInput($request->only('USERNAME', 'remember'))
->withErrors([
'username' => $this->getFailedLoginMessage(),
]);
}
Both user providers that you get with Laravel (EloquentUserProvider and DatabaseUserProvider) expect password to be stored in lowercase password field.
In order to make authentication work with PASSWORD field you need to do 2 things.
First, let providers know that user's password is stored in PASSWORD column. You can do this by implementing getAuthPassword method in your User model:
public function getAuthPassword() {
return $this->PASSWORD;
}
Secondly, password needs to be stored with password key in the credentials array you pass to Auth::validate(). You'll need to change the name of the name of the form field that user inputs password into to password OR create credentials array manually:
$credentials = [
'USERNAME' => $request->get('USERNAME'),
'password' => $request->get('PASSWORD'),
];
I am using Laravel 5.2, and I am trying to create a dashboard where the user can update his information, but I am facing one problem which is bypassing unique:users in validator.
if the user wants to keep same email, validator gives an error of 'The email has already been taken', also user should not change email to another email which is reserved by another user.
How can I avoid this validation in case if this user is the only user has this email.
my controller function:
public function update(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
// if fails, return response with errors
if($validator->fails())
return back()->withErrors($validator)->withInput();
$user = Auth::user();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = bcrypt($request->input('password'));
$user->update();
return back()->withInput();
}
Laravel's unique validator can take additional parameters that can help you exclude given ID from the unique check.
The syntax is:
unique:<table>,<column>,<id_to_exclude>
In your case, you'll need the follwing validation rule:
'email' => 'required|email|max:255|unique:users,email,'.$id
Just change your code to:
public function update(Request $request)
{
$id = Auth::user()->id;
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users'.$id,
'password' => 'required|min:6|confirmed',
]);
// if fails, return response with errors
if($validator->fails())
return back()->withErrors($validator)->withInput();
$user = Auth::user();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = bcrypt($request->input('password'));
$user->update();
return back()->withInput();
}
Why this works? Well the laravel Unique validation searches for the unique value in the table specified. So unique:users searches if the email exists in db. The user id here works as a way to exclude the check for this user.
Also, if you want that email should not be edited, then just exclude it from the request.
$input = $request->excpet(['email']); check docs