How to use authentication for multiple tables in Laravel 5 - php

Sometimes, we'd like to separate users and admins in different 2 tables.
I think it is a good practice.
I am looking if that is possible in Laravel 5.

Before reading the following, you are supposed to have basic knowledge on ServiceProvider, Facade and IoC in Laravel 5. Here we go.
According to the doc of Laravel, you could find the Facade 'Auth' is refering to the Illuminate\Auth\AuthManager, which has a magic __call(). You could see the major function is not in AuthManager, but in Illuminate\Auth\Guard
Guard has a Provider. This provider has a $model property, according to which the EloquentUserProvider would create this model by "new $model". These are all we need to know. Here goes the code.
1.We need to create a AdminAuthServiceProvider.
public function register(){
Auth::extend('adminEloquent', function($app){
// you can use Config::get() to retrieve the model class name from config file
$myProvider = new EloquentUserProvider($app['hash'], '\App\AdminModel')
return new Guard($myProvider, $app['session.store']);
})
$app->singleton('auth.driver_admin', function($app){
return Auth::driver('adminEloquent');
});
}
2.Facade:
class AdminAuth extends Facade {
protected static function getFacadeAccessor() { return 'auth.driver_admin'; }
}
3. add the alias to Kernel:
'aliases' => [
//has to be beneath the 'Auth' alias
'AdminAuth' => '\App\Facades\AdminAuth'
]
Hope this could be helpful.

I have created a laravel package where you can handle multiple authentication.
Step 1 : Composer require
Firstly, composer require the multiauth package
composer require sarav/laravel-multiauth dev-master
Step 2 : Replacing default auth service provider
Replace
Illuminate\Auth\AuthServiceProvider::class
with
Sarav\Multiauth\MultiauthServiceProvider
in your config/app.php file
Step 3 : Modify auth.php
Modify your config/auth.php file to something like this
'multi' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
'table' => 'users'
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
'table' => 'admins'
]
],
Thats it! Now you can try multiple authentication by passing the user as first parameter. For example
\Auth::loginUsingId("user", 1); // Login user with id 1
\Auth::loginUsingId("admin", 1); // Login admin with id 1
// Attempts to login user with email id johndoe#gmail.com
\Auth::attempt("user", ['email' => 'johndoe#gmail.com', 'password' => 'password']);
// Attempts to login admin with email id johndoe#gmail.com
\Auth::attempt("admin", ['email' => 'johndoe#gmail.com', 'password' => 'password']);
For more detailed documentation
http://sarav.co/blog/multiple-authentication-in-laravel/
http://sarav.co/blog/multiple-authentication-in-laravel-continued/

Related

get user laravel api axios

i created laravel cms using vue and axios.
i want get current user that sending post requests
so i followed laravel api documentation and take this structure
// bootstrap.js
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
window.axios.defaults.headers.common['Authorization'] = `Bearer ${window.api_token}`;
// vue component file
axios.post('/api/v1/person', this.person)
.then(data => {
this.$emit('added', data.data);
this.person.id = data.data.data.id
});
// Route in api.php
Route::prefix('v1')->name('api.')->group(function () {
/** Person Routes */
Route::prefix('person')->namespace('Person')->name('person.')->group(function(){
Route::post('/', 'PersonController#index');
});
});
//in laravel controller i retrun
return response()->json(auth('api')->user());
but i get this result
even i checked console headers and Authorization header set properly
i can get all post data but laravel don`t pass me the user
also i made a repository of this project in github
if you want can follow this link
https://github.com/mohammadZx/crm
In the documentation it states that you have to set the correct guard for Passport to work. Update auth.php config.
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
by default, laravel trying to find user by hashing token and for this reason laravel return null because your user api tokens not hashed. so if you change auth.php setting to don't searching hashes api_tokens, this problem will be fixed
'guards' => [
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false
],
],

Why is a login via `auth guard A` redirected different than one from `auth guard B` when the same behaviour is expected?

First of all, i'm not a pro in PHP development or Laravel but will try to explain my question as well as possible for me. I also try to give enough information but when something is missing, please let me know whats missing and i will add it!
Some important context about my project:
Laravel 6.18 (will update soon if my first goal is reached)
I use Hyn/Multi-tenant to make my application mutli tenant.
I use a Vue front end, i give a bearer token to Vue via the app.js
The application should be a multi tenant application where each tenant has its own user table. I first built everything as "single tenant". When the basic functionality was implemented and worked fine, i added Hyn/MT to make it multi tenant, moved the tenant specific tables to the tenant db folder and updated the models. Now i know it was better to start with Hyn/MT before building all the functionality but so far i got everything working fine.
After i added the multi tenant support to my project and fixed the broken functionality i decided to add an admin specific area to manage the tenants. To achieve this i added a SystemU ser table to the master database which contains the admin users. After that i update my web.php so it gives the subdomain to the LoginController.guard() function. My code:
// web.php
Route::group(array('domain' => '{subdomain}.festipay.xlan'), function () {
Route::post('login', 'Auth\LoginController#login');
});
// LoginController.php
protected function guard()
{
if (Request::route("subdomain") == "admin") {
return Auth::guard('admin_web');
} else {
return Auth::guard('web');
}
}
I also updated my config/auth.php, it looks like this now:
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin_web' => [
'driver' => 'session',
'provider' => 'admin_users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin_users' => [
'driver' => 'eloquent',
'model' => App\SystemUser::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
Except for the mentoined changes i did not implement any admin specific logic yet. So i expect that the admin users are handeled exactly the same except for the way they are authenticated.
The tenant users who log in to e.g. tenant_x.domain.com are redirected to /dashboard when they login and are redirected back to /login when they log out. The admin users who log in to admin.domain.com are not redirected to /dashboard when the login is successfull but are redirected back to /login again. Offcourse this is not the expected behaviour as it should be (currenly) the same as the tenant users (so a redirect to /dasboard when the login is succesfull)
I think that the authentication them selve works fine as the LoginController.attemptLogin() returns true when i use valid admin credentials and false (and view shows wrong credetials) when i use invalid credentials.
I found in this post that is may be a session issue and i tried to apply the solution mentoined in that post. Unfortunately did adding protected $primaryKey = 'id'; to the SystemUser class not solve the issue. I also compared the tenant User class with the SystemUser class but they are almost identical exccept for unimportant fields i removed from the SystemUser like address.
I have no idea how i can find out where the issue occurs or how to solve this. The goal is that an admin which logged in succesfully is redirect to another page as the /dashboard. Can someone help me find out what goes wrong? i'm already happy when someone can help me to get the same behaviour for the admin's as the tenants currently have.
Thanks in advance!
Update 1 #David Barker
When its about the session, i think this is important to know as well:
- I use a Vue front end, i give a bearer tokento Vue via theapp.js``
My session config:
<?php
use Illuminate\Support\Str;
return [
'driver' => env('SESSION_DRIVER', 'file'),
'lifetime' => env('SESSION_LIFETIME', 120),
'expire_on_close' => false,
'encrypt' => false,
'files' => storage_path('framework/sessions'),
'connection' => env('SESSION_CONNECTION', null),
'table' => 'sessions',
'store' => env('SESSION_STORE', null),
'lottery' => [2, 100],
'cookie' => env(
'SESSION_COOKIE',
Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
),
'path' => '/',
'domain' => env('SESSION_DOMAIN', null),
'secure' => env('SESSION_SECURE_COOKIE', false),
'http_only' => true,
'same_site' => null,
];
I did a dd($request->session();) in the LoginController->attemptLogin(); function, see the result bellow. The result is the same for both users except for the id and _token. (i cleaned the cookies before the login attempt in both cases)
Illuminate\Session\Store {#490 ▼
#id: "7WI7JUWPnS4pg3EHvaxk5TOKaM9l9UXJi1zJNKuG"
#name: "festipay_session"
#attributes: array:1 [▼
"_token" => "mtMWanYGMUxFHivOqAaEmVQnHDE0hvwKkHMgCswg"
]
#handler: Illuminate\Session\FileSessionHandler {#489 ▼
#files: Illuminate\Filesystem\Filesystem {#198}
#path: "/var/www/domain.com/storage/framework/sessions"
#minutes: "120"
}
#started: true
}
Maybe this is also interesting infomation. Left are the responses for the admin (after i clicked the login button) and right the working tenant login.
I finally found the issue. It was very easy to solve. I did not specify the auth guard for the Route::group.
It was like this:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () { return redirect('/dashboard'); });
Route::get('/dashboard', function () { return view('dashboard'); })->name('dashboard');
Route::get('/logout', 'Auth\LoginController#logout')->name
I changed it to this to make it work:
Route::group(['middleware' => 'auth:system_user_web,web'], function () {
Route::get('/', function () { return redirect('/dashboard'); });
Route::get('/dashboard', function () { return view('dashboard'); })->name('dashboard');
Route::get('/logout', 'Auth\LoginController#logout')->name

How to make Laravel 6 authorization that checks for 4 different usertype

I have tried to edit this tutorial with no avail. This works if you only have a user and an admin. What I need to have is to make it check for 4 possible usertypes.
https://www.itsolutionstuff.com/post/laravel-6-multi-auth-authentication-tutorialexample.html
Based on your comment on Ruben Danielyan's answer, it seems like you're trying to use is_admin as your role identifier by using 1 and 2, however if you used the tutorial's migration this will not work since is_admin is a boolean. So you would have to change in the migration file to $table->integer('is_admin')->nullable();
Edit:
As per porloscerros said, you should also perhaps rename is_admin to a more appropriate name like role_id or role_category since fields starting with is_ are generally used for boolean value to represent whether a record is or is not in one specific state or category (like is_active, is_hidden, or is_discounted)
You can use roles and permissions package to assign roles to your registered users,spatie/laravel-permission is my best choice. Then you can redirect user depending on his role
The code from your tutorial
public function login(Request $request)
{
...
$user = auth()->user();
if ($user->hasRole('admin')) {
return redirect()->route('admin.home');
}else if ($user->hasRole('modertor')){
return redirect()->route('moderator.home');
}else if ($user->hasRole('translator')){
return redirect()->route('translator.home');
}else{
return redirect()->route('home');
}
}
It is simple.
All you have to do is to add a guard.
Open app/Providers/AuthServiceProvider.php and change your boot method
public function boot(){
$this->registerPolicies();
Auth::viaRequest('admin', function ($request) {
return Admin::where('token', $request->token)->first();
});
}
After that attach this guard to your model in config/auth.php.
...
'guards' => [
'web' => ['driver' => 'session','provider' => 'users',],
'admin' => ['driver' => 'session','provider' => 'admins',],
],
...
'providers' => [
'users' => ['driver' => 'eloquent','model' => App\User::class,],
'admins' => ['driver' => 'eloquent','model' => App\Admin::class,],
],
...
Now you may use auth:admin middleware on a route to check if admin is logged in before reaching that route.

Correct approach to use JWT in laravel

i've reviewed some articles and forums to figure out how to implement JWT authentication in my backend.
the problem is that i've seen different approaches and idk which one is correct.
consider that i've already installed JWT package.
after that some do this:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
...
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
and then in Controllers constructor using
$this->middleware('auth:api');
to validate user. they say setting 'driver' => 'jwt' will make default guard jwt. also they use
$token = auth()->attempt($credentials)
to validate token. this was first group approach.
second ones never change anything in config/auth.php (no driver change - no gaurd change ...).
they use a middleware in app/Http/Kernel.php (routeMiddleware):
'auth.jwt' => \Tymon\JWTAuth\Http\Middleware\Authenticate::class,
and when making routes they apply it using a group, like:
Route::group(['middleware' => 'auth.jwt'], function () {
Route::get('logout', 'ApiController#logout');
Route::get('user', 'ApiController#getAuthUser');
Route::get('products', 'ProductController#index');
Route::get('products/{id}', 'ProductController#show');
Route::post('products', 'ProductController#store');
Route::put('products/{id}', 'ProductController#update');
Route::delete('products/{id}', 'ProductController#destroy');
});
also they use $jwt_token = JWTAuth::attempt($input) or $this->user = JWTAuth::parseToken()->authenticate(); to get and validate user.
im'm confused with these approaches, should i use jwt as default guard? or i just add it to my packages and use it explicitly?

What are role providers?

How do I use zfc rbac role providers?
I understand guards prevent users from accessing routes but it seems like I need the role providers as well. Are these database permissions? In the example below is 'article' a controller and the part after the '.' the permission granted on that controller? How can I test these once in place? Many thanks.
return [
'zfc_rbac' => [
'role_provider' => [
'ZfcRbac\Role\InMemoryRoleProvider' => [
'admin' => [
'permissions' => [
'article.delete',
'article.edit',
'article.archive',
'article.read'
]
],
'member' => [
'permissions' => [
'article.edit',
'article.archive',
'article.read'
]
],
'guest' => [
'permissions' => ['article.read']
]
]
]
]
];
Here you can read about role providers
In ZF-Rbac one identity can have different roles with different permissions/privileges. To collect the roles for the authorizationService you need role providers. They will include a RoleProvicerInterface (link) with the getRoles method which is supposed to return the roles that the authorization service has to work with.
Each Identity has an IdentityInterface (link) which has also a getRoles method. This will return an array of roleNames which will be mapped to the roles from the RolesProvider to find out about permissions/privileges.
You can then find out what the current user (identity) is allowed to do.

Categories