I have a seeder so the users that can use the app can just run the seeder at the begining and have that administrator configured. The app only has one administrator. However, Im not undersanding why the login is not working properly it shows always that the credentials are invalid. Do you know what can be the issue?
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
AdministratorSeeder::class,
]);
}
}
class AdmininistratorSeeder extends Seeder
{ public function run()
{
User::create(['name' => 'administrator', 'email' => config('services.administrator.key'), 'password' => bcrypt('password')]);
}
}
Related
How to set email and password to admin login using guards??
If I have to login for the 1st time in admin login portal what email and password is it going to verify with.
I tried adding record to database directly and logging in but that doesn't work.
when i try to login with email and password in the database, i get the following error
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Models\Admin given, called in C:\xampp\htdocs\Alumni datatable - Copy (2) - Copy\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line 434
AdminAuthController
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class AdminAuthController extends Controller
{
public function getLogin(){
return view('admin.auth.login');
}
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if(auth()->guard('admin')->attempt(['email' => $request->input('email'), 'password' => $request->input('password')])){
$user = auth()->guard('admin')->user();
if($user->is_admin == 1){
return redirect()->route('adminDashboard')->with('success','You are Logged in sucessfully.');
}
}else {
return back()->with('error','Whoops! invalid email and password.');
}
}
public function adminLogout(Request $request)
{
auth()->guard('admin')->logout();
Session::flush();
Session::put('success', 'You are logout sucessfully');
return redirect(route('adminLogin'));
}
}
you have to extends Authenticatable in your Admin Model
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
}
SuperAdminSeeder.php
public function run()
{
#Super Admin Seeder
$role = Role::where('name', 'super_admin')->first();
$user = User::create([
'name' => 'Jhon Cena',
'email' => 'jhon#gmail.com',
'password' => Hash::make('jhoncena'),
'remember_token' => Str::random(60),
]);
$user->role()->attach($role->id);
}
This is my SuperAdmin seeder which task is to make a user with role super_admin
DatabaseSeeder.php
public function run()
{
$this->call(RegionSeeder::class);
$this->call(CountrySeeder::class);
$this->call(LocationSeeder::class);
$this->call(ProductSeeder::class);
$this->call(SliderSeeder::class);
$this->call(BannerSeeder::class);
$this->call(SuperAdminSeeder::class); //this is the Super Admin seeder
$this->call(RoleSeeder::class);
$this->call(UserSeeder::class);
$this->call(ShopSeeder::class);
}
This is the database seeder
The error I am getting [php artisan migrate:fresh --seed]
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Trying to get property 'id' of non-object", "E:\github\LARAVEL\Deal-Ocean\database\seeds\SuperAdminSeeder.php", [Object(App\User)])
I am new in laravel
It's caused no super_admin role when you run superadmin seeder. You should add SuperAdminSeeder after RoleSeeder.
public function run()
{
$this->call(RegionSeeder::class);
$this->call(CountrySeeder::class);
$this->call(LocationSeeder::class);
$this->call(ProductSeeder::class);
$this->call(SliderSeeder::class);
$this->call(BannerSeeder::class);
$this->call(RoleSeeder::class); //this seeder must have superadmin role
$this->call(SuperAdminSeeder::class); //this is the Super Admin seeder
$this->call(UserSeeder::class);
$this->call(ShopSeeder::class);
}
public function run()
{
$this->call(RegionSeeder::class);
$this->call(CountrySeeder::class);
$this->call(LocationSeeder::class);
$this->call(ProductSeeder::class);
$this->call(SliderSeeder::class);
$this->call(BannerSeeder::class);
$this->call(RoleSeeder::class);
$this->call(SuperAdminSeeder::class); //this is the Super Admin seeder
$this->call(UserSeeder::class);
$this->call(ShopSeeder::class);
}
Just put the RoleSeeder above the SuperAdminSeeder.
Because roles table might have a column value named super_admin. SO you must call the RoleSeeder Because there is a foreign key constriant.. And then you can insert the super admin using SuperAdminSeeder
I'm migrating from Laravel 4 to 5.7 and having trouble with my custom auth provider. I've followed various walkthroughs (e.g. 1, 2, 3) as well as quite a bit of googling.
I've attempted to get this working by the following:
Set the guards and providers and link to my target model.
'defaults' => [
'guard' => 'custom_auth_guard',
'passwords' => 'users',
],
'guards' => [
'custom_auth_guard' => [
'driver' => 'session',
'provider' => 'custom_auth_provider',
],
],
'providers' => [
'custom_auth_provider' => [
'driver' => 'custom',
'model' => App\UserAccount::class,
],
],
Register the driver defined in the above provider. I'm piggybacking off AuthServiceProvider for ease
...
public function boot()
{
$this->registerPolicies();
\Auth::provider('custom',function() {
return new App\Auth\CustomUserProvider;
});
}
...
Created my custom provider which has my retrieveByCredentials, etc. I've replaced the logic with some die() to validate if it is making it here. In Laravel 4, it used to go to validateCredentials().
class CustomUserProvider implements UserProviderInterface {
public function __construct()
{
die('__construct');
}
public function retrieveByID($identifier)
{
die('retrieveByID');
}
public function retrieveByCredentials(array $credentials)
{
die('retrieveByCredentials');
}
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
die('validateCredentials');
}
For reference, App/UserAccount looks like so
class UserAccount extends Authenticatable
{
use Notifiable;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'public.user_account';
// no updated_at, created_at
public $timestamps = false;
private $_roles = [];
private $_permissions = [];
}
Finally, I am calling it via my controller.
if(\Auth::attempt($credentials){
return \Redirect::intended('/dashboard');
}
I have also tried to call the guard direct
if(\Auth::guard('custom_auth_guard')->attempt($credentials){
return \Redirect::intended('/dashboard');
}
This results in the following error: "Auth guard [custom_auth_guard] is not defined."
I've tried a few other commands to make sure there is no cache issue:
composer update
php artisan cache:clear
The results: when I call Auth::attempt($credentials) Laravel is trying to run a query on the users table. the expected result is that it would hit one of the die()'s in CustomUserProvider... or at lease try and query public.user_account as defined in the model.
I've been messing with this for some time and I must be missing something simple... hopefully someone with a bit more experience in Laravel 5 can see what I am doing wrong.
Thanks in advance!!
Managed to work it out. Couple little problems but the main one was that I was trying to piggyback on AuthServiceProvider as opposed to my own provider. Below is what I did to get a custom auth provider working in Laravel 5.7
Set the provider in config.auth.php.
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => \UserAccount::class,
],
],
Create a new provider in app/providers/ . This links the listed provider above with the correct User Provider Code.
namespace App\Providers;
use Auth;
use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;
class CustomAuthProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
Auth::provider('eloquent',function()
{
return new CustomUserProvider(new \UserAccount());
});
}
}
Created my custom provider in app/auth/. This is the logic for validating the user and replaces the laravel functions for auth. I had an issue here where it was validating but not populating the user object. I originally had a test to see if the object was null and if it was, populate... however it was always populated with an empty object. removing the test allowed me to call Auth::user() functions.
namespace App\Auth;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Auth\EloquentUserProvider;
class CustomUserProvider implements EloquentUserProvider{
public function __construct()
{
$this->user = $user;
}
public function retrieveByID($identifier)
{
$this->user = \UserAccount::find($identifier);
return $this->user;
}
public function retrieveByCredentials(array $credentials)
{
// find user by username
$user = \UserAccount::where('name', $credentials['username'])->first();
// validate
return $user;
}
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
//logic to validate user
}
Updated App/Models/UserAccount looks like so
use Illuminate\Foundation\Auth\User as Authenticatable;
class UserAccount extends Authenticatable
{
protected $table = 'public.user_account';
// no updated_at, created_at
public $timestamps = false;
private $_roles = [];
private $_permissions = [];
}
That's it. I can now validate via the below call
if(\Auth::attempt($credentials){
return \Redirect::intended('/dashboard');
}
I want to build in an expiry of passwords onto my website. I came across a very helpful document which is very good. However on my local PC this does not work, the reset of password does not appear.
But when I transfer all to the website, I get the error
Class app\Http\Requests\PasswordExpiredRequest does not exist
The class has been created and is in the correct path. Please assist, where have I missed something
Ensure the contents of app/Http/Requests/PasswordExpiredRequest.php are:
<?php
namespace App/Http/Requests;
class PasswordExpiredRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'current_password' => 'required',
'password' => 'required|confirmed|min:6',
];
}
}
(the document forgot to mention adding the namespace to the file)
UPDATE: I am going to include my full file replacing the partial view I had. The seeder for the User table works, but the one for the Groups table does not. I do have those tables produced by Sentry but I only created a Model for Groups that has nothing in it other than the declaration of the class. Don't know what else to include.
<?php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
//User::create(array('email' => 'foo#bar.com'));
// $this->call('UserTableSeeder');
$this->command->info('User table seeded!');
}
}
class UserTableSeeder extends Seeder {
public function run()
{
User::create(array(
'username' => 'alvaro',
'permissions' =>'{"user":1}'
));
$this->command->info('User table seeded!');
}
}
class GroupTableSeeder extends Seeder {
public function run()
{
Group::create(array(
'name' => 'usuario',
'permissions' =>'{"user":1}'
));
$this->command->info('Group table seeded!');
}
}
But actually, the one I want is the Groups tables (I am on Sentry). Yes, I have created the Model for Group, as Group.php but I don't know how to define its contents. Sometimes I have seen on other occasions that it suffices with just defining the class, but here I dont know, it doesn't work that easily.
Just doing something like
class GroupTableSeeder extends Seeder
will not work as it says that such class does not exist.
The only thing I needed to do was to create a separate file having that name GroupTableSeeder.php
and include the code in there. For some reason, while UserTableSeeder can be inside a file called DatabaseSeeder and it works, it does not work for other tables.
class GroupTableSeeder extends Seeder {
public function run()
{
Group::create(array(
'name' => 'basicuser',
'permissions' =>'{"user.create" :-1,"user.delete" :-1,"user.view":1,"user.update":-1,"post.create":1}'
));
$this->command->info('Group table seeded!');
}
}