Laravel 5.6 Auth from two different tables - laravel

I would like to ask about logging in to Laravel but in a different way:
User information including the password but not the email will be stored in users table, while email_address will be stored in a email_addresses table with a boolean field called is_default.
So the user can have several emails, but he can log in with only one email that has the is_default is true. and he could change his default email through his profile.
So, how can I make the login process through the Auth::login() facade.
I was able to register the user by storing the information in a users table and store his email_address and is_default = true in the email_addresses table.

It can be a little tricky. Auth::login() just needs to fetch email and password, so if you can trick the model into thinking it has an email field accessible via $user->email, things will work out:
public function getEmailAttribute()
{
return DB::table('email_addresses')->whhere('default', 1)->where('user_id', $this->id)->first()->toArray()['email'];
}

Related

How to update a user using the 'Ion Auth' authentication library in CodeIgniter 4 framework

Below is my code snippet. I want to reduce the balance of a user in the database using Codeigniter 4?
dd($this->ionAuth->update($this->ionAuth->users()->row()->id, ['balance' => 1000]));
*I'm using Ion Auth for updating.
It's just a big burden when updating the database using modelling data.
It looks like you intend to use $this->ion_auth->user() instead of $this->ion_auth->users().
Also keep in mind that executing a dump and die on $this->ion_auth->update(...) returns a boolean not the updated user data.
References:
users() gets all users.
users()
Get the users.
Parameters
'Group IDs, group names, or group IDs and names' - array OPTIONAL. If an array of group ids, of group names, or of group ids and names are passed (or a single group id or name) this will return the users in those groups.
Usage
$users = $this->ion_auth->users()->result(); // get all users
user() gets a single user.
user()
Get a user.
Parameters
'Id' - integer OPTIONAL. If a user id is not passed the id of the currently logged in user will be used.
Usage
$user = $this->ion_auth->user()->row();
echo $user->email;
look here authentcate library for ci4
https://github.com/lonnieezell/myth-auth
go there download it
ionAuth is not compablitablke with ci4

How do i use 3/4 username in laravel 6.* or later version?

I stored data in user table like "id, name, username, phone_no, employee_no, email, password and many more columns.
I want to log in as Username & Password, email & Password, phone_no & Password, employee_no & Password.
what is the methodology need for this?
currently, I'm using a username & Password as laravel default.
Depending on your authentication method, you might need to override the function that looks for the user. But if you're asking for the query, typically you would do something like this:
User::where(function($query) {
$query->where('username', $accountIdentifier)
->orWhere('email', $accountIdentifier)
->orWhere('phone_number', $accountIdentifier)
->orWhere('employee_number', $accountIdentifier);
});
$accountIdentifier would correspond to the input of the user, which could be any of those parameters. It's also worth noting that I put it inside a sub-query because you might need to check for accounts that are active, or accounts that are verified.

Laravel authentication and authorisation

In my database, there is two table members and services. in that table, there are 5 column
members
Username, Rank, Name, Password, and more
services
username user_services
I don't want my default login from AUTH controller from users table but I want login from my members table and authorize table from another table services
if user logins authentication from members table and authorize from services table
Tried everywhere but couldn't get any valuable answer.
Thank You.
To change de auth table
Edit app/config/auth.php to change the table.
'table' => 'members',
If you need to change field in app/Http/Controllers/Auth/LoginController.php
public function username()
{
return 'yourField';
}
And you can continuos using AuthController from Laravel

Laravel authorization via email, password and additional field

Out of the box Laravel authorizes users by matching email (default - can be overridden) and password.
Is it possible to authorize user using 3 fields, e.g:
email
password
group
... where 'group' is some additional field from 'users' database.
In other words, user can belong to group:1 and can login to group:1 resources only, but not to group:2 using his group:1 credentials.
If user belongs to group:1 and group:2, then he needs different credentials to login to either group.
Additionally, user can use same email for both groups. In such case it is group number that would act as additional identifier. And of course passwords would be different.
I am thinking setting a database multiple column index on fields 'id' and 'group' would be a good start, but I fail to grasp (yet), what would be required to make Laravel authorization process sensitive to 3 fields, instead of 2.
I would appreciate some pointers.
This sounds like you're trying to achieve a form of tenancy on data belonging to certain groups only. Have a look at this package:
https://github.com/HipsterJazzbo/Landlord
In essence, you would add a group_id field to the tables where you wish to restrict access and then using middleware, apply an additional where('group_id', Auth::user()->group_id) clause to any database queries. This works very well for retrieving subsets of data belonging to specific users by their role, for example.

How to authenticated multiple table in laravel 5.2

I have a two tables in database
1. users(default user table)
2. suspend_users(only two column:- id, email)
now i want to first check in the "suspend_user" table user email exist or not if user email exist in the "suspend table" authentication failed message shows.
now i do not understand that how to check second table(suspend_users) first and how to check multiple table authenticate
I dont understand exactly what you mean, you can check first if the mail exists on the suspend_table and after check login:
example:
if (SuspendedUser::findBy(['email' => $user_email])) {
// go back to what else... throw expetion etc....
}
if (Auth::attempt(email, pass)) {} // check login
you can easily authenticate multiple table in laravel 5.1 and above.
http://blog.sarav.co/multiple-authentication-in-laravel/

Resources