I want to have an authentication for Admin and for Non-admin. I'm having the admin infromation in 'users' db table, and non-admin information in 'customers' table.
How to use Laravel's Auth class to create authentication for this two different user roles?
for starter you should merge the two tables and then define a new column named "role_id" and then a new table called "roles" then as you attempt like this:
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
Session::put('role', Auth::user()->role_id);
return Redirect::intended('dashboard');
}
then via Session you can get the role whenever it's needed and decide to whatever yo want to do with it...
note: also edit your User model so role_id can be accessible
happy coding
Related
I want two users to use same mobile number and password, but they may have different role_id, which is a column (i added in users table), this role_id may have different value according to role of a user.
lets assume 4 for manager, 5 for supervisor.
I want manager and supervisor to use same mobile number and password while login, is there a way i can pass a third parameter (role_id along with mobile_no and password) and laravel login attempt to checks that value and try to login that credentials from database.
I know we can add active (boolen) columns where we can pass a value that account should be active.
But here role_id can have any value starting from 1.
So is there a way i can modify attempt function or check a where clause along with mobile number, password and role_id so that laravel try to login that credentials associated with that role_id.
What i tried, i tried to create two users with same mobile number and password, laravel is not logging either one of them, because it is not knowing which user to login, also different password is also not working for same mobile number
i am using laravel 5.8
You can code your own auth logic using the Auth facade. Give a look to the documentation: Manually authenticating users
So you would need to retrieve the roleid from somewhere to know what user it should attempt to login to, and in the controller you would have something like this:
// This is assuming that you're getting all the info from the form
// You can create your own array and set the information how you want.
$credentials = $request->only('mobile_number', 'password', 'role_id');
if (Auth::attempt($credentials)) {
// Authentication passed
// do what you want when the
// user gives valid credentials
}
Note that we are also giving the role_id to the attempt method, meaning that it will try to find a match in the database for the mobile_number, the password and the role_id and retrieve that user.
I am using Laravel built-in Authentication for users which I run from the terminal php artisan make:auth
It has one table in the database, users table and has an isAdmin column, so I am assuming it has two roles an admin and not_amin account.
Now, what I want is to modify this built-in functionality of Laravel make:auth to be able to add some roles like isEmployee and/or isCustomer.
How can I achieve this using the available option I have?
I hope, I somewhat explained my case. Thanks.
Having a column is_admin is targeted to only one role. This tells you if user is an admin or not.
I would suggest you to change depending on any of below cases :
1. If user can have just 1 role :
Modify users table and change is_admin to role. Inside role column, you can store the role of that particular user like admin, employee, 'customer` etc.
2. If user has multiple roles(many-to-many) :
Remove column is_admin from users table and create a new table role_user which will have user_id and role_id. You will need to have another table called roles to store all available roles in your system.
I am new to Laravel and i am creating a new SMS (Student Management System) for my first Laravel Project. So, i need 3 tables.
Admins
Teachers
Students
Admins will be me, teachers will be the actual teachers and students will be students. I will have more tables in the futures but i like to keep all the different types of users in their own table. This was simple before laravel. I am in 5.6 and i am using this to login a user:
public function auth()
{
if (Auth::attempt(['username' => request(['username']), 'password' => request(['password'])])) {
return redirect('/admin');
}
}
I want to know how i can get the Auth::attempt to check the "admins" table when trying to auth someone in the admins controller. And how do i do this so that way when i am working on the teacher/student authentication, it will check their specific table. I am so confused.
I am using Auth::check('user', $this->request), where 'user' is the name of my Auth::config configuration, to successfully authenticate and login users against a Users table. Upon successful user login, the user information is stored in the session and can be accessed via Auth::check('user').
I have a relational design in place where the Users table has a many-to-one relationship with a table named Sites. When Auth::check queries the Users table to authenticate a user logging in, it only returns information from the Users table. I need Auth:check to return the User with its related data from the Sites table. Both the User and Site models have the appropriate hasOne and hasMany relationships defined. How can I ensure that the user is queried and returned with its corresponding related data via Auth::check()?
While it's generally inadvisable to store more session information than is absolutely necessary, it's possible to do this through the 'query' configuration key of the Form auth adapter. Just create a wrapper method in your Users model that can take a query configuration array, and return a user object with associated relationships. Probably something like this:
public static function authenticate (array $query) {
return static::all($query + array('with' => 'Sites'));
}
Then, add 'query' => 'authenticate' to your 'user' auth configuration.
How do we build a profile page that outputs the user's data? and this page can only be viewed by the user who logged in. It's something like when we go to our profile page and view our own username, password, email, address..etc. Then we may edit it by ourselves. It, of course can't be edited by other users.
I'm confused with the need of a profile table, now I think we would not need it? we can just populate the data using some PHP logic on a page we create as profile.ctp ?
This is confusing, I followed this http://book.cakephp.org/#!/view/1041/hasOne and created a profile table with some fields my users table has, and then with a foreign key called user_id. I checked on User and Profile model both are correctly defined in the relationship. I have this in Profile model:
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
and this in User model:
var $hasOne = 'Profile';
As I browse to my profile/index there are field names without any records. It's empty set. I thought it was supposed to retrieve data from the users' table ??
What's the best way to create a profile page for the existing users.. and the upcoming registrations ?
You do not need a profile table (if you already have a users table with their info).
One way of having this done is, after user validation, compare his id with id of the user which profile he wants to visualize. If those match, then it's a user who is viewing his own profile, and you can let him view his info.
Ofcourse there's a great deal of security issues you would have to take into account if you are thinking to make this a public accessible web site.