Why does Laravel authentication require the 'web' guard? - authentication

I have setup Laravel 5.5 and installed the default authentication scaffolding.
My application has two types of users - customers and staff - so I prefer to name the authentication guards that way, and the following configuration seems to work.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'customers' => [
'driver' => 'session',
'provider' => 'customer-users',
],
'staff' => [
'driver' => 'session',
'provider' => 'staff-users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
My providers, customer-users and staff-users, use the standard eloquent driver, however they each return a different user type.
The problem is that I would like to remove the 'web' guard, since it's just cluttering the config file. When I do however, I get an exception somewhere deep in the Laravel middleware.
I can live with the extra clutter of course, but it does bother me that Laravel is relying on a configuration item that I can't change. Is this a Laravel bug, perhaps?
BTW - I don't have 'web' set as the default guard when I get the error...

By default web guard given by laravel which used for web authentication based on session driver with the table users. Now you have created your own custom guards and you are using it. So your wish to keep that web guard. But if you remove, you might be facing some internal issue, so best you keep it and it's not going to be the performance issue too.

Related

DB switch between Production, Develop and Test in CakePHP3

I currently have two database settings in my config/app.php, one named "default" for production, another named "test" for development. I now want to rename the current "test" to "dev" and make a 3rd setting, naming it new "test" for testing purposes.
Switching between production database and development database is working fine, but how does my app know when to use the test database while I'm still in development environment (working on a development branch)? What changes should I make to what files?
I've been googling all day but all I can find is the way to switch between production and development. I need to know how to make the switch between development and test.
Below is something I have in mind but I'm pretty sure I have to modify some other files as well in order to make them work right.
# config/app.php
'Datasources' => [
// Production
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'MY PRODUCTION HOST',
'username' => 'MY PRODUCTION USERNAME',
'password' => 'MY PRODUCTION PASSWORD'
'database' => 'MY PRODUCTION DATABASE NAME'
'quoteIdentifiers' => false,
// 'encoding' and so on...
],
// Development
'dev' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'MY DEVELOPMENT HOST',
'username' => 'MY DEVELOPMENT USERNAME',
'password' => 'MY DEVELOPMENT PASSWORD'
'database' => 'dev'
'quoteIdentifiers' => false,
// 'encoding' and so on...
],
// Test
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'SAME AS MY DEVELOPMENT HOST',
'username' => 'SAME AS MY DEVELOPMENT USERNAME',
'password' => 'SAME AS MY DEVELOPMENT PASSWORD'
'database' => 'test'
'quoteIdentifiers' => false,
// 'encoding' and so on...
]
],
I'm totally new to CakePHP so any advices/suggestions would be appreciated. Thank you!
One method would be to set an environment variable in your development environment that's not set in production, for example:
DEV_ENVIRONMENT=true
In the application's startup script, in config/bootstrap.php you can look for that environment variable, and if it's set you can alias the default connection with dev:
In bootstrap.php:
// Look for this line:
ConnectionManager::setConfig(Configure::consume('Datasources'));
// Add your code someplace after that:
if(getenv('DEV_ENVIRONMENT')){
ConnectionManager::alias('dev', 'default');
}
The alias function will load the dev datasource any time the default connection is referenced in the app (which all Tables do unless overridden).
This generally the same method the PhpUnit tests use to substitute in the test datasource configs during test runs.

Yii2 Where to catch an exception thrown from a bootstrapper module?

In my config, I have a custom class bootstrapping to fetch data
'bootstrap' => [
[
'class' => 'app\components\DataProvider',
'campaignJsonUrl' => 'https://example.com/api.json',
],
],
And I would like to catch exceptions throwns for an unreachable url or invalid json for example. But I can't figure out where to catch these.
I made a custom error handler:
'errorHandler' => [
// use 'site/error' action to display errors
'class' => 'app\components\ErrorHandler',
'errorAction' => 'site/error',
],
In which the error seems to reach ErrorHandler.renderException() method if I override it, however it never reaches the SiteController.actionError() method, I guess because it happens before any controller action. How can I make the ErrorHandler run SiteController.actionError() so it will show the website's error page with the correct layout?
NOTE: My overridden renderException() method only sends the exception to a Slack API then returns parent::renderException($exception)

Integrating passport authentication in lumen

I am trying to integrate Passport authentication in Lumen (5.4.*) application using dusterio/lumen-passport https://github.com/dusterio/lumen-passport package.
I followed the steps till Installed routes, But when i try to access /oauth/token it throws 404 not found error
I am not sure what i am missing.
Can anyone help me out? Waiting for positive response.
be sure to register passport route and register auth config in bootstrap/app.php
add
Edit config/auth.php to suit your needs. A simple example:
return [
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => \Your\UserModel::class
]
]];
Load the config in bootstrap/app.php since Lumen doesn't load config files automatically:
$app->configure('auth');
and
Dusterio\LumenPassport\LumenPassport::routes($this->app);
Although it is not recommended to edit vendor file , if you want I have a solution editing vendor file. Edit the below file as given below:
vendor/dusterio/lumen-passport/src/LumenPassport.php
Edit line 83 from
$callback->group(...
to
$callback->router->group(...

Route not defined when using 'auth' flter

I've started playing around with Laravel. I want to create simple authentication logic using 'auth' filter for my route. The problem is when I set the route like this:
Route::get('/user', array('before' => 'auth', 'as' => 'user', function() {
return Redirect::action('UserController#index');
}));
Route::get('/login', 'UserController#login');
I get: [InvalidArgumentException] - Route [UserController#index] not defined
However, when I go with a basic route:
Route::get('/user', 'UserController#index');
the page renders successfully.
Can anyone see the problem?
You could try adjusting the route to use UserController#index
Route::get('/user', array(
'before' => 'auth',
'as' => 'user',
'uses' => 'UserController#index'
));

zf2 - zfcuser check auth for some module only

Im new to zf2.
zfcuser is setup as in the installation guide. Im able to register, login and logout.
I had created modules for frontend and backend. Im trying to check if user is login for the backend - admin and all the child modules.
I tried to include
$sm = $app->getServiceManager();
$auth = $sm->get('zfcuser_auth_service');
if (!$auth->hasIdentity()) {
//redirect to login page
}
in my admin/module.php function onBootStrap
it did check for login, BUT not only for the admin, is for the entire modules including the frontend.
I just need to check login for the admin modules, and all the child modules of admin.
Couldn't figure out how. Please help
In you module.config.php
<?php
namespace AdminModule;
use Zend\Authentication\AuthenticationService;
return array(
__NAMESPACE__ => array(
'params' => array(),
'service_manager' => array(
'invokables' => array(
'Zend\Authentication\AuthenticationService' => 'Zend\Authentication\AuthenticationService',
),
),
'services' => array(
// Keys are the service names
// Values are objects
'Auth' => new AuthenticationService(),
),
),
);
Example in your controller/Manager
$this->auth = new AuthenticationService();
if($this->auth->hasIdentity()){
$this->userid = $this->auth->getIdentity();
}
You can use a factory too.
Example in your controller
using tools from ZfcUser module
$this->zfcUserAuthentication()->hasIdentity()