How can i change the default Auth Password field name - laravel

i want to change the default password field name of Laravel Auth, this because im using ORACLE, How can i change ? i tried this but i cant login
User.php
protected $table = 'SEG_USUARIOS1';
public function getAuthPassword()
{
return $this->contrasenha;
}
RegisterController.php
protected function validator(array $data)
{
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:SEG_USUARIOS1',
'contrasenha' => 'required|string|min:6|confirmed',
]);
}
protected function create(array $data)
{
$user = User::create([
'nombre' => $data['name'],
'email' => $data['email'],
'contrasenha' => bcrypt($data['password']),
'verifytoken' => Str::random(40),
]);
$thisUser = User::findOrFail($user->usuario_id);
$this->sendEmail($thisUser);
return $user;
}
Im Using laravel 5.4

My code was ok, the only problem was that I'am using another primary key,
so you have to use it in User.php
protected $primaryKey = 'your_id';

Related

Laravel Livewire testing

I am looking forward to learn Livewire and its test as well. Now I have created component for registering users and it is working fine, but when I try to do a test I get this error:
Failed asserting that an array has the key 'redirect'.
Here are the parts of the code:
RegisterTest
class RegisterTest extends TestCase
{
use RefreshDatabase;
/** #test */
public function can_register()
{
Livewire::test('auth.register')
->set('name', 'user')
->set('email', 'user#outlook.com')
->set('password', 'secret')
->set('password_confirmation', 'secret')
->call('register')
->assertRedirect('/');
}
}
Component
public $name = '';
public $email = '';
public $password = '';
public $password_confirmation = '';
public function register()
{
$data = $this->validate([
'name' => 'required|string',
'email' => 'required|email',
'password' => 'required|confirmed|min:8|string'
]);
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
return redirect('/');
}
This is pretty easy.
Add the following file:
->assertHasNoErrors(['name', 'email', 'password']);
before
->assertRedirect('/');
line and you will see it will fail. This is because your validation tells password should be minimum 8 characters and in your test it is 6 (you used secret as password)

I have added avatar column to the generated users table, how to make default value to the avatar column

I am using Laravel Framework, I have generated all Register and Login through "$php artisan make:auth" command, now I have added a new column called "avatar" in the users table, and I want to set it to "noimage.jpg", so each time I register by default "noimage.jpg" will be added.
RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'avatar' => 'noimage.jpg' //How it suppose to be?
]);
}
You also have to add avatar to the $fillable property of your model. Otherwise you cannot assign it with create. See docs on Mass Assignment.
Instead you could manually assign the avatar:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->avatar = 'noimage.jpg';
return $user;
}
Another way to set a default value for your model is to use Laravel lifecycle:
const DEFAULT_AVATAR = 'noimage.jpg'
protected static function boot()
{
parent::boot();
static::creating(function (User $user) {
if (!$user->avatar) {
$user->avatar = self::DEFAULT_AVATAR;
}
});
}
See: https://laravel.com/docs/5.8/eloquent#events

how to logout from laravel 5.6

I want to log out after register. how do I do that?
This is my register controller:
protected function create(array $data)
{
$user = User::create([
'firstname' => $data['firstname'],
'secondname' => $data['secondname'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'address' => $data['address'],
'mobileno' => $data['mobileno'],
'type' => $data['type'],
]);
$verifyUser = VerifyUser::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
Mail::to($user->email)->send(new VerifyMail($user));
return $user;
return redirect('/login')->with('status', 'We sent you an activation code. Check your email and click on the link to verify.');
}
You can change the url :-
protected $redirectTo = '/where/you/want/to/redirect';
after registration in app/Http/Controller/Auth/RegisterController.php
and for logout:-
public function __construct()
{
$this->middleware('guest')->except('logout');
}
Add this two line in your controller and check
public function __construct()
{
$this->middleware('auth');
}

laravel auth register insert data into two tables

i have my default registration for auth controller. i want to register also the emp_id created from users table to employee table. once registered.
my RegisterController
use App\User;
use App\Employee
public function count_users(){
$count = User::count();
return date('y').'-'.sprintf('%04d',$count);
}
protected function create(array $data)
{
return User::create([
'emp_id' => $this->count_users(),
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return Employee::create([
'emp_id' => $this->count_users()
]);
}
Please check following line in your code:
return User::create([ .....
Above line creates the user and returns the created user. Any code below "return" is not being called.
Please try following code:
use App\User;
use App\Employee
public function count_users(){
$count = User::count();
return date('y').'-'.sprintf('%04d',$count);
}
protected function create(array $data)
{
$emp_id = $this->count_users();
$user = User::create([
'emp_id' => $emp_id,
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Employee::create([
'emp_id' => $emp_id
]);
return $user;
}

How do I modify my Laravel 5.2 registration to assign new users a role in Laravel 5.3?

I'm new to Laravel, and have been trying to build a user-role authentication system. As 5,3 is very new, there are not many resources that are beginner-friendly. I am using the authentication system created by php artisan make:auth, however I am unsure on how to associate my newly created users with a my roles table.
Initially I had been following this video series on YouTube, but the Authentication controllers differ greatly between 5.2/5.3
I'm hoping that someone can help me figure out what must be done to add in an equivalent what is done in the video to the new controller.
Laravel 5.2 Role Association
public function postSignUp(Request $request)
{
$user = new User();
. . .
$user->save();
$user->roles()->attach(Role::where('name', 'User')->first();
}
From Laravel 5.3 RegisterController.php
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
Thanks for reading.
You can modify the auto-generated Controllers:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user->roles()->attach(Role::where('name', 'User')->first());
return $user;
}
To attach a role to a user by inserting a record in the intermediate table that joins the models right after creating an account, use the attach method before returning the new User, like this:
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
//attach default user role to user
$user->roles()->attach(Role::where('name', 'User')->first());
return $user;
}

Resources