Laravel authentication and authorisation - laravel

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

Related

Laravel: Check if the field was filled in the route, using Trait and Scope

I have an api that I am implementing multi-tenant based on an additional column in the tables.
For this I created a trait that implements a
static::addGlobalScope() and static::creating().
I have a user_idXtenant_id table. In this table I store the accesses that the user has.
My Route looks like this:
Route::get('{tenant_id}/products', function ($tenant_id) {
return App\Products::where(['tenant_id' => $tenant_id])->get();
})->middleware('role_or_permission:admin|seller|get-products');
Route::get('products', function () {
return App\Products::get();
})->middleware('role_or_permission:admin|seller|get-products');
The idea is that in the first route it is verified if the user has access to tenant_id and if he has access, return only the products of that tenant_id (here Polices can be useful and solve the problem).
The second route, on the other hand, must return all products of all tenant_id that the user has access to. For this, I use static :: addGlobalScope() to set the tenants that the user has access to in this way:
$builder->whereIn('tenant_id', $tenants);
The question is how to check in GlobalScope if tenant_id was filled in the model. So, if it is present, I only check the permission and if it is not present I include the tenant_id that the user has access to.
I hope I have been able to adequately exemplify the problem.
Any help is most welcome.
Thank you!

Laravel Multi table auth

I have three tables
Admins for admin,
Teachers for teacher,
students for student.
Each of these users have their own dashboard (AdminDash, TeacherDash, StudentDash). Now all I want to do is, each user are sent to their respective dashboard after login. Is it even possible to do so.
Also can this be done using single login form or do I need to create different login form for each case and different controllers to authenticate.
Edit:
Actually, here teachers and students fill different registration forms with different value so I don't want to store them in same table. And admins is for admin and superadmin who run the site. Also the dashboard they use after login are different.
I published a custom laravel for manage multi auth in one laravel project. In example branch I commit step by step for describe everything you most know. larasin
Also can this be done using single login form or do I need to create different login form for each case and different controllers to authenticate.
You can create a login form component an use it every where you want.
First extend your 3 tables with Authenticable like:
class User extends Authenticatable
Then in your config/auth, set up the providers like:
'teachers' => [
'driver' => 'eloquent',
'model' => App\Models\Teacher::class,
],
Then you should be able to auth::attempt your model instance.
Do let me know if this doesn't work, I am pretty new in Laravel also :) cheers!
p/s: I do think it is better to just use a user table with a "role" column, however I believe you probably have different columns needed for each of these user groups. But you can still use new tables such as "teacher_profiles","user_profiles" for those columns, and you can refer to different tables according to your role.
For example, if "teacher" role_id is 3, then if user role==3, you will refer to "teacher_profiles" table.

Relational table data read on Vue with Laravel

I have USER & ROLE Table
I want to show the role of that particular user on his datatable which is in Vue.
All the related codes are given on those links.
Thanks in Advance.
PLEASE MENTION IF YOU KNOW ABOUT ANY VIDEOS/TUTORIALS ON THIS TOPIC
Vue Code link: https://notepad.pw/code/nqxo2q43
Roles Table: https://notepad.pw/code/n3zw1m9jh
Users Table: https://notepad.pw/code/5yrleots
Role Model: https://notepad.pw/code/o853sl6fv
User Model: https://notepad.pw/code/f00vu7m1
Role Controller: https://notepad.pw/code/w773twp2p
User Controller: https://notepad.pw/code/4vzarmyk

Laravel 5.6 Auth from two different tables

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'];
}

Where to add Auth session variables when the user is logging in

I’d like to add session variable to Auth when the user logs in.
Where should that code be exactly?
Here’s my structure:
Group table (parent table):
Id
Currency
Client_type
User table (child table):
Id
Group_Id (FK)
Name
I’d like for the values of Currency and Client_type to be available globally in Auth.
Thanks, using Laravel 5.2
Auth::user() will have the logged in user object from which you should be able to access all its properties.
edit: if the currency is a property of a related model then you should have your relations set up, maybe something like this in the User model:
public function group()
{
$this->belongsTo(Group::class,'Group_Id');
}
and you access it then like so
Auth::user()->group->Currency;
BTW I'm not sure how the tables are related.

Resources