I have read a lot of threads about multi-auth in Laravel, most of the configurations I see are somehow complicated, I have also seen a multi-auth package but it does not support Laravel Socialite.
I am aware that this question is asked multiple times, but if someone can give a better answer. It would be much appreciated!
Things I have tried
I am familiar with Laravel make:auth
I am also familiar with Laravel socialite Facebook , Twitter , Google plus.
Give this a try. But you still need some basic knowledge about Laravel's new multitenancy.
In config/auth.php add something like this to guards array:
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
Than in the same file add this to providers array:
'customers' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
Than create Migration for customers DB table (you can use Laravel's out of the box migration for users table)
Next is Eloquent model App\Customer with these included:
use App\Scopes\AuthorizedScope;
use Illuminate\Foundation\Auth\User as Authenticatable;
These should let you use Laravel's Auth facade in your app with these most used methods:
Auth::guard('customer')->attempt()
Auth::guard('customer')->check()
Auth::guard('customer')->logout()
Auth::guard('customer')->user()
Or use auth middleware like this:
Route::get('customer/dashboard', function () {
// Only authenticated users may enter...
})->middleware('auth:customer');
Also checkout these:
https://laravel.com/docs/5.3/authentication#authenticating-users
Related
So I just started a Laravel Project with Breeze, and I wanted to change the default table users , the problem is it didn't work, I did my research for days and I didn't get any successful result
I will try to explain what is the problem and what have I tried so far.
First, I created a new table called users_data, and this table, is completely different than the users table.
The fields that users_data has, are for example: name_value, password_value, age_value, email_value, etc. (I have to mention too that for the table users_data, it doesn't use a migration, because I already have an sql file, and added it directly to the db (I already have tables created, with primary keys, and foreign key, so i couldn't do the migration because it would take me a lot of time), and without the migration I can still get the data, so I don't think it could be this the problem).
Actually I'am using Breeze, however, I used Auth scaffolding (PHP artisan make: Auth) too
What have I tried:
After several days of search, first I have created a new Model, called UsersModel, the content of this is the same as User Model however what I change is:
protected $table = 'users_data';
protected $fillable = [
*name_value*,
*password_value*,
];
and an extra function to override the default password of breeze or Auth (I guess):
public function getAuthPassword()
{
return $this->password_value;
}
next I went to conf/auth.php
there I specified the Model:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\UsersModel::class,
],
and the table to use:
/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/
'table' => 'users_data',
after this I went to the view login.blade.php, and changed only the email input (for what I read on different pages, changing the password input will cause different problems, because I would need to make a lot of changes to make it work so the best idea is to override it with getAuthPassword, specified in the model):
new name input:
x-input id="email" class="block mt-1 w-full" type="text" name="name_value" :value="old('name_value')" required autofocus />
After all this I went to LoginRequest (the validation for the login), where I replaced email for name_value
I tried to debug this:
dd(Auth::attempt($this->only('name_value', 'password'), $this->boolean('remember')));
and returns false
I noticed that there's a function in vendor/laravel/ui/auth-back/AuthenticatesUsers
called username(), that returns 'email'
when I saw that I remembered a page that said that this function could override too, so I changed the return value to name_value, and it doesn't do nothing
last, just to clarify,
I don't need the Register site I only need the login page, so for that in the $fillable I didn't add all the columns of the database, just the ones that I need to log in (name_value, password_value)
If anyone could help me and guide me it will be great, because I'am running out of ideas (I could do it with PHP alone, however, I need the ->middleware ['Auth], is there a way to activate the middlware if the user exists?)
So you might have a model named users_data.php. Go inside it and change the code to something like that.
STEP:1
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\users_data as Authenticatable; //Add this line for Auth.
class users_data extends Authenticatable //Extends child from Authenticatable parent class obj.
{
use HasFactory;
protected $fillable = ['column1', 'column2', 'column3', .....];
}
STEP:2
Go to config/auth.php. You may found something like this below.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
Change the 'model' => App\Models\User::class, to 'model' => App\Models\users_data::class,
This is the main character in this drama that putting your application to users table by default.
STEP:3
Comment off the User.php so that no future conflict create.
Now your application has been diverted to your desired table and ready to login.
I don't think it's good practice to be editing vendor files. We don't push them to version control so other developers won't have your changes. Also, Laravel already has a way to override the username value without editing the vendor files.
Just use the trait in your auth controller like this:
public class MyLoginController {
use AuthenticatesUsers; // or you can also use ThrottlesLogins trait
// then override the username function here
public function username() {
return 'name_value';
}
}
To override the password you can define this on your User model:
public function getPasswordAttribute() {
return $this->attributes['password_value'];
}
public function getAuthPassword() {
return $this->password_value;
}
I haven't tested this but based on the docs this is how you should do it. Also make sure to read this Laravel doc.
I'm working on a social website and when I login, I need some information about the user to use in the front-end such as name, image link, username, id.
So which is better to use, a session to store all data I need, or just using
auth()->user()->property?
The auth() helper function or the facade for that matter is already using the session as a driver for the web guard by default...
config/auth.php
'guards' => [
'web' => [
'driver' => 'session', // <-- Here
'provider' => 'users',
],
]
So what you're really asking is if the session or the session is better...
you can use auth() helpler :
auth()->user()
Or Auth Facade :
Auth::user()
both the same and get authenticate user details from database
You can use auth helper for that and it's optimal enough cause its in global bootstraped $app variable witch loads in the beginning of laravel kernel loading.
Also its a good way to load other everytime-needed attributes from user in the auth() helper like auth()->id.
You can use marco to add such data to auth helper this way:
Add this to AppServiceProvider boot method or any custom provider you have to add extra field to auth helper through SessionGuard which is macroable:
SessionGuard::macro('mobile', function () {
return auth()->check() ? auth()->user()->mobile : null;
});
Now everywhere you need simply use
auth()->mobile();
I'm setting up a REST API using Laravel 5.7. To validate authentication I JWT-auth and for permissions and roles I use Spatie.
My problem: when trying to link a role to a user I get the following error
Spatie \ Permission \ Exceptions \ RoleDoesNotExist
There is no role named admin.
The role do exist in the database:
This is how I'm trying to assign a role to the user:
$user = User::findOrFail(1);
$user->assignRole('admin');
As I'm new to Laravel I'm not sure if it's relevant, but setting the JWT I had to change the driver of the guard in the config/auth.php to jwt
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
I can't see what I'm doing wrong. I added the roles and then tried to add the role to a user.
Check your app namespace. If you updated it from App, be sure to update it in config/auth.php.
On the other hand, if you didn't update the App namespace, try clearing your cache and re-seed the database tables.
php artisan config:cache
php artisan cache:clear
Also check the user model if you have protected $guard_name = 'api'; in there.
Hope this helps. Cheers!
I'm assuming you've manually added the roles to the database. The roles are cached and it causes issues if you don't use the built in create methods.
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit articles']);
From the GitHub docs
You will need to manually clear the cache with php artisan cache:clear.
The better way to do this is either to use a seeder (for permanent roles) or Tinker to run the code to create roles and permissions, which will trigger the cache to be cleared.
Guard has been changed from the web to api but still try to find web from the database
assign a role like this.
$roleToAssign = Role:: findByName('administrator', 'api');
$user->assignRole($roleToAssign);
#Link
If none of other answers solved your problem
Check that you are using queue
if so
restart the queue
I am in charge of creating a new laravel project where the authentication is made by our LDAP. I am pretty new to laravel so excuse me for my lack of comprehension
I am trying to use this library : Adldap2-Laravel
I followed this documentation to build the authentification from scratch.
So my config/app.php have those lines added
//In providers array
Adldap\Laravel\AdldapServiceProvider::class,
Adldap\Laravel\AdldapAuthServiceProvider::class,
//In aliases
'Adldap' => Adldap\Laravel\Facades\Adldap::class,
And my config/auth.php (I also tried to only use the adldap driver, without success)
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'users' => [
'driver' => 'adldap',
'model' => App\User::class,
],
],
Everything looks right. But I can not auth, I have this error : These credentials do not match our records.
When I use this code
use Adldap\Laravel\Facades\Adldap;
$username = "myUser";
$password = "myPassword";
if (Adldap::auth()->attempt($username, $password)) {
//Queries here
}
else
{
echo "Auth failed";
}
I CAN perform queries, and the authentication works with all my users. But I can not use properly the login form behind the "Login" button.
Did I miss something ?
I do not know if the authentication should work directly without importing anything but I also tried to run this command
php artisan adldap:import
All my users are found and inserted in the laravel DB. But the authentication still does not work.
I deleted the ADLDAP_ACCOUNT_SUFFIX= from my .env file
And added the suffix directly in ADLDAP_ADMIN_USERNAME=Administrator#mycompany.something
Now I'm able to connect via the email address.
I do not know if it is the best solution but at least it works.
I let this post open for a few days to see if someone have a better idea
I created a simple login and registration in my page and I also added some new columns in the default users table. Now my problem is I have another table named as admin and the default Laravel's Auth table is users. How can I implement the same functionality in my table?
In the Users model it has the declaration for the table name
protected $table = 'users';
Can you give me an example how can I use the default laravel's auth class?
Thats all thanks. :-)
Laravel takes default users table for an application. For a change of laravel authentication different table relevant table name, we need to make a small change in authentication file of config.
Go to
config/auth.php
'providers' => [
// 'users' => [
// 'driver' => 'eloquent',
// 'model' => App\User::class,
// ],
'users' => [
'driver' => 'database',
'table' => 'user',
],
],
Do you hear about Multiauth in laravel. in this library there are two or more type user can login in one laravel application. In our case there are two type user Admin and Public that means User right.
Both forgot password and reset password functionality works separately in one application.
After install this library have have one step like below.
'multi' => [ 'admin' => [ 'driver' => 'database', 'table' => 'admin', 'email' => 'client.emails.password' ], 'users' => [ 'driver' => 'database', 'table' => 'users', 'email' => 'client.emails.password', ] ],
change your Auth.php file code with this one.
installation
Firstly you want to include this package in your composer.json file.
"require": {
"sboo/multiauth" : "4.0.*"
}
Now you'll want to update or install via composer.
composer update
Usage
Everything is done the exact same way as the original library, the one exception being that all method calls are prefixed with the key (account or user in the above examples) as a method itself.
Auth::admin()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::client()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::admin()->check();
Auth::client()->check();
Here is your library
I don't think the best way is to duplicate your table. I would extend users table with a role field that indicates if the user is a standard one or an admin. This way, you can keep the same code and add the ADMIN functionality that you are looking for.
If you NEED to do that and you are using Laravel 4, maybe you can use this plugin:
https://github.com/ollieread/multiauth/
Also in this thread you have code that implements Auth in different tables:
https://gist.github.com/danielcoimbra/64b779b4d9e522bc3373
But I strongly suggest to integrate both tables in one with an Admin flag/field