as the title says..
My project was hashing passwords properly until recently I noticed that passwords of new users dont get hashed while it is supposed to be as I am using Hash::make and I used Hash on the top of the controller. Please Help...
here is my User controller
I am using laravel 5.6 if this would help...
namespace App\Http\Controllers\Auth;
use App\User;
use Hash;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectPath = '/admin';
protected function validator(array $data)
{
return Validator::make($data, [
'f_name' => 'required|string|max:255',
'user_id' => 'required|string|max:255|unique:person',
'm_name' => 'required|string|max:255',
'g_name' => 'required|string|max:255',
's_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:person',
'field_of_study'=> 'string|max:255',
'gender' => 'required|boolean',
'b_date' => 'required|date|max:255',
'type' => 'required|integer|max:3',
'job_title' => 'string|max:255',
'orgnaization' => 'required|string|max:255',
'sector' => 'boolean',
'mobile' => 'required|string|min:10',
'password' =>'required|string|min:10',
]);
}
protected function create(array $data)
{
return User::create([
'f_name' => $data['f_name'],
'user_id' => $data['user_id'],
'm_name' => $data['m_name'],
'g_name' => $data['g_name'],
's_name' => $data['s_name'],
'address' => $data['address'],
'field_of_study' => $data['field_of_study'],
'gender' => $data['gender'],
'b_date' => $data['b_date'],
'type' => $data['type'],
'job_title' => $data['job_title'],
'orgnaization' => $data['orgnaization'],
'mobile' => $data['mobile'],
'sector' => $data['sector'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
public function register(Request $request)
{
if($user=User::create($request->all()))
{
return redirect('Admin')->with('message', 'done');
}
else
{
return redirect()->back()->with('error', 'error');
}
}
}
and here is my model
protected $fillable = [
'f_name','m_name','g_name','s_name','address','user_id','field_of_study','gender','b_date','type','job_title','orgnaization','mobile','sector', 'email', 'password',
];
protected $hidden = [
'password',
];
use
if ($user = $this->create($request->all()))
instead of
if($user=User::create($request->all()))
You're not calling your controller function, you're just doing User::create. that's why none of your data is being hashed
Maybe this code help you:
$inputData = $request->all();
$inputData['password'] = bcrypt($request->password);
User::create($inputData);
Related
ok, I'm trying to login the users after success registration into their panels. the database line built successfully. but alternately user login become successful but once not.
Here is the Request Controller Code:
$request->merge(['phone'=> '09123456789']);
$request->merge(['name' => 'User']);
$request->merge(['password' => 'pass123456789']);
$request->merge(['email' => 'user#email.com']);
$credentials = $request->validate([
'name' => 'required',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/',
'password' => 'required',
'email' => 'required',
]);
User::create([
'name' => $request->name,
'email' => $request->email,
'phone' => $credentials['phone'],
'password' => Hash::make($credentials['password'])
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->route('panel');
}
The Model:
protected $fillable = [
'name',
'phone',
'email',
'password',
];
let me know if I didn't describe perfectly
First, I would stop using validation in the controller and use form request validation instead. It's been the standard since Laravel 5.
Try this simplified code using Auth::login() instead:
public function createUser(Request $request)
{
$request->validate([
'name' => 'required|string|max:64',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/',
'password' => 'required|string|max:64',
'email' => 'required|email|max:64',
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'password' => Hash::make($request->password),
]);
Auth::login($user);
return redirect()->route('panel');
}
I'm Importing a CSV file to a livewire component and trying to run some validation for each row of the file but I'm having problems doing this. It seems that my validation is doing nothing.
Here is how my Livewire component looks like:
namespace App\Http\Livewire\Modals;
use Validator;
use Livewire\Component;
use App\Http\Traits\Csv;
use App\Models\AccountUser;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Auth;
class ImportExtensions extends Component
{
use WithFileUploads;
public $clientID;
public $showModal = false;
public $upload;
public $columns;
public $fieldColumnMap = [
'first_name' => '',
'last_name' => '',
'email' => '',
'password' => '',
'extension' => '',
'user_type' => '',
];
protected $rules = [
'fieldColumnMap.first_name' => 'required|max:255',
'fieldColumnMap.last_name' => 'required|max:255',
'fieldColumnMap.email' => 'required|max:255',
'fieldColumnMap.password' => 'required|max:255',
'fieldColumnMap.extension' => 'required|max:255',
'fieldColumnMap.user_type' => 'required|max:255',
];
protected $validationAttributes = [
'fieldColumnMap.first_name' => 'First Name',
'fieldColumnMap.last_name' => 'Last Name',
'fieldColumnMap.email' => 'Email',
'fieldColumnMap.password' => 'Password',
'fieldColumnMap.extension' => 'Extension',
'fieldColumnMap.user_type' => 'User Type',
];
public function updatingUpload($value)
{
Validator::make(
['upload' => $value],
['upload' => 'required|mimes:txt,csv'],
)->validate();
}
public function updatedUpload()
{
$this->columns = Csv::from($this->upload)->columns();
$this->guessWhichColumnsMapToWhichFields();
}
public function import()
{
// Validate that you are importing any data
$this->validate();
$importCount = 0;
Csv::from($this->upload)
->eachRow( function ($row) use (&$importCount){
$eachRow = $this->extractFieldsFromRow($row);
//Validate the data of each Row to make to make sure you don't import duplicate records
$this->validateOnly(collect($eachRow), [
'fieldColumnMap.first_name' => 'required|max:255',
'fieldColumnMap.last_name' => 'required|max:255',
'fieldColumnMap.email' => 'required|max:255|email|unique:account_users, email',
'fieldColumnMap.extension' => 'required|numeric|unique:account_users, extension',
'fieldColumnMap.password' => 'required|max:255',
'fieldColumnMap.user_type' => 'required|in:user,admin',
]);
//If validation fails, it should skip the create extension part and run the next row
//If validation pass, then create the Extension
AccountUser::create([
'user_id' => Auth::user()->id,
'account_id' => $this->clientID,
'first_name' => $eachRow['first_name'],
'last_name' => $eachRow['last_name'],
'email' => $eachRow['email'],
'password' => $eachRow['password'],
'extension' => $eachRow['extension'],
'user_type' => $eachRow['user_type'],
]);
$importCount++;
});
$this->reset();
$this->emit('refreshExtensions');
$this->notify('Successfully Imported '.$importCount.' Extensions');
}
Also, how can I make so that if the validation fails it goes to the next row instead of trying to create the extension.
Thanks.
I was able to create custom rules for just this. if one row fails validation, I just throw an error. So, basically either all rows pass or all fails.
Here is how it looks like now:
public function import()
{
// Validate that you are importing any data
$this->validate();
$importCount = 0;
Csv::from($this->upload)
->eachRow( function ($row) use (&$importCount){
$eachRow = $this->extractFieldsFromRow($row);
$validatedData = Validator::make([
'first_name' => $eachRow['first_name'],
'last_name' => $eachRow['last_name'],
'email' => $eachRow['email'],
'password' => $eachRow['password'],
'extension' => $eachRow['extension'],
'user_type' => $eachRow['user_type'],
],[
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|unique:account_users',
'extension' => 'required|numeric|unique:account_users',
'password' => 'required|max:255',
'user_type' => 'required|in:user,admin',
],);
if($validatedData->fails()){
$this->notify(['error','Oops something went wrong!']);
}else{
AccountUser::create([
'user_id' => Auth::user()->id,
'account_id' => $this->clientID,
'first_name' => $eachRow['first_name'],
'last_name' => $eachRow['last_name'],
'email' => $eachRow['email'],
'password' => $eachRow['password'],
'extension' => $eachRow['extension'],
'user_type' => $eachRow['user_type'],
]);
$importCount++;
}
});
$this->reset();
$this->emit('refreshExtensions');
if($importCount!=0) $this->notify(['success','Successfully Imported '.$importCount.' Extensions']);
}
I have two tables: profile and users. I tried to code it on RegisterController using the Laravel UI package. However, it didn't work properly. What is the best approach to implement it?
public function register(Request $request)
{
$year = date_diff(date_create($request->birthday),
date_create(date('Y-m-d')));
$profile_data = [
'last_name' => $request->last_name,
'first_name' => $request->first_name,
'middle_name' => $request->middle_name,
'birthday' => $request->birthday,
'age' => $year->format('%y')
];
$profile = Profile::create($profile_data);
return User::create([
'email' => $request->email,
'password' => Hash::make($request->password),
'profile_id' => $profile->id
]);
}
protected function create(array $data)
{
return User::create([
'email' => $data['email'],
'password' => Hash::make($data['password']),
'profile_id' => $data['profile_id']
]);
}
How to check if user exists in my table or redirect back with a message using Laravel 5.6?
Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given, called in C:\wamp\www\zainsurgalt\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 35
protected function create(array $data)
{
$isEmailExists = Niigem::where('email', $data['email'])->count();
if($isEmailExists){
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
else{
return Redirect::back()->withErrors(['msg', 'The Message']);
}
}
I Added my Create method here
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Niigem;
use Validator;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data){
$validator = Validator::make($data, [
'name' => 'required|string',
'email' => 'required|string|email|exists:niigems',
'password' => 'required|string',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
You need to return something when the user is created.
protected function create(array $data)
{
$isEmailExists = Niigem::where('email', $data['email'])->count();
if($isEmailExists){
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return ........ you need to return something here
} else {
return Redirect::back()->withErrors(['msg', 'The Message']);
}
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|max:255|unique:users|exists:niigems',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data)
{
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
I see your are trying to create a new user if they exist in the niigem table. To do it the Laravel way, you have to validate using Laravel's validation class. So, this should work:
protected function create(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|string',
'email' => 'required|string|email|exists:niigem',
'password' => 'required|string',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Another way is:
Niigem::firstOrCreate(['email' => 'dummy#domain.com'], ['name'=> $data['name'], 'password' => bcrypt($data['password'])]);
Please use this query you can use both condition as per your need:
$user = Niigem::where('email', '=', Input::get('email'))->first();
if ($user === null) {
// user doesn't exist
}
if ($user !== null)
{
// user doesn't exist
}
I'm trying to use the RegisterController in Laravel, but I can't get the Validator to work. I don't understand what the problem is, because it should just take an array and validate it.
When I try to send a JSON with the right fields to the register route, I get this error:
BadMethodCallException: Method validate does not exist. in file /home/deb85528n3/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php on line 96
Below is my code:
protected function validator(array $data)
{
$validator = Validator::make($data,
[
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer',
'lat' => 'required',
'lon' => 'required',
]);
echo $validator->errors();
if ($validator->fails())
{
return response()->json(['errors'=>$validator->errors()]);
}
if ($validator->passes())
{
$response = "validator passed";
return response()->json($response);
}
}
I also tried using the Validator in a different way:
public function validator(Request $request){
$validator = Validator::make($request->all(), [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer|min:4',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
]);
}
But then I get this error:
Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to App\Http\Controllers\Auth\RegisterController::validator() must be an instance of App\Http\Controllers\Auth\Request, array given, called in /home/deb85528n3/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 31 in file /home/deb85528n3/app/Http/Controllers/Auth/RegisterController.php on line 103
Edited to include the whole RegisterController:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
$validator = Validator::make($data, [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer|min:4',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
]);
echo $validator->errors();
if ($validator->fails())
{
return response()->json(['errors'=>$validator->errors()]);
}
if ($validator->passes())
{
$response = "validator passed";
return response()->json($response);
}
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
//maybe check if facebook login here?
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'birth_year' => $data['birth_year'],
'lat' => $data['lat'],
'lon' => $data['lon'],
]);
}
}
change your function name
use Validator;
add this in your controller,
remove this
public function validator(Request $request){
Write your validation rules in model like below
public static function rules()
{
return [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer|min:4',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
];
}
and call this rule in your controller
$validator = Validator::make($request->all(), "Your model Name"::rules());
if ($validator->fails()) {
//throw exception
}
The register method calls the validator method and passes through an array, it then expects that an instance of \Illuminate\Contracts\Validation\Validator to be returned.
Unless you're needed to override the default responses for registering a user, you should just be able to have:
public function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer|min:4',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
]);
}