I define a new guard "Admin" to have a multi Auth System User and admin in my project . web guard allows login.But admin guard does not allow login
when I try to login into Admin ,it gives
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'last_sign_in_at' in 'field list' (SQL: update `admins` set `updated_at` = 2020-09-27 12:49:24, `last_sign_in_at` = 2020-09-27 12:49:24, `current_sign_in_at` = 2020-09-27 12:49:24 where `id` = 1)
My users table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('user_type');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('status')->default(0);
$table->timestamp('last_sign_in_at')->nullable();
$table->timestamp('current_sign_in_at')->nullable();
$table->string('user_click');
$table->timestamp('user_click_time')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
My admin table
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('user_type');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('status');
$table->rememberToken();
$table->timestamps();
});
}
auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
//admin guard
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
],
My Middleware CheckRole
public function handle($request, Closure $next)
{
if (!Auth::guard('admin')->check()){
return redirect('admin/login');
}
return $next($request);
}
My Admin.php Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
//guard
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
//guard End
class Admin extends Authenticatable
{
use Notifiable;
protected $guard ='admin';
protected $hidden = [
'password', 'remember_token',
];
protected $guarded=[];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
My AdminController
public function adminLogin(Request $request){
if ($request->ismethod('post')) {
$data = $request->input();
if ( Auth::guard('admin')->attempt(['email' => $data['email'], 'password' => $data['password'],
'user_type'=>'admin', 'status' => '1'])){
return view('admin.dashboard');
}
else {
return back()->with('error',' Invalid UserName Or Password');
}
}
}
When I tried to login into Admin, It gives error. Any solution ps !
It seems like you have an event listener listening for Auth's LoginEvent and it is setting the last_sign_in_at field on the Model and saving it. Since you are using different models for Authentication it will end up trying to do this on what ever Model is in that event; in this case the Admin model.
You will need to add this field to your admin table, or you will have to check in the listener which Model the event is holding and decide whether to update this field depending on what type that model is.
Related
i'm try to using multi auth using Admin Guard and implement with Spatie, after login succes using the Admin Guard, then access the Group Middleware but i got an error 403 USER IS NOT LOGGED IN.
this is my code :
Admin Model :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
class Administration extends Authenticable
{
use HasFactory, Notifiable, HasRoles;
protected $guard_name = 'admins';
protected $fillable = [
'name', 'email', 'username', 'password', 'photo'
];
protected $hidden = ['password'];
}
LoginController :
public function __construct()
{
$this->middleware('guest:admins')->except('logout');
}
public function authenticated(Request $request, $user)
{
if ($user->hasRole('admin')) {
return redirect()->route('bank.master-bank.index');
} else if ($user->hasRole('finance')) {
return redirect()->route('bank.master-bank.index');
} else if ($user->hasRole('supervisor')) {
return redirect()->route('bank.master-bank.index');
}
return redirect('login');
}
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required'
]);
if (auth()->guard('admins')->attempt($request->only('email', 'password'))) {
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return redirect()->intended('/bank/master-bank');
} else {
$this->incrementLoginAttempts($request);
return redirect()
->back()
->withInput()
->withErrors(["Incorrect user login details!"]);
}
}
Auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'admins' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Administration::class,
],
],
Route Web.php
Route::group(['middleware' => 'auth:admins'], function () {
Route::group(['middleware' => ['role:admin']], function () {
Route::group(['prefix' => 'user', 'as' => 'user.'], function () {
Route::get('/', [UserAdminController::class, 'user_panel'])->name('user_panel');
Route::get('/role-user', [UserAdminController::class, 'role_panel'])
Route::get('/detail_user/{id}', [UserAdminController::class, 'detail_user'])
Route::resource('/verif-user', VerifUserController::class);
});
});
});
when access the Route::group(['middleware' => ['role:admin']], function () { i got error 403
USER IS NOT LOGGED IN.
Check you haven't define your middleware role and you are using it here.
I have multi-authentication set up in my laravel app. I am trying to create multiple authentication using default authentication laravel 5.8. I have two tables one is users and other is admins. I have configured the guards for admin. User login works fine, no issues but when I try to login the admin, it doesn't work even if I login with correct credentials. Password field validation works if I use less then 6 character. Please help me to solve this problem.enter code here
My Admin model is
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable {
use Notifiable;
protected $guard = 'admin';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password', 'title',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Guard setting is
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
],
AdminLoginController is
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;
class AdminLoginController extends Controller {
public function __construct() {
$this->middleware('guest:admin')->except('logout');
}
public function showLoginForm() {
return view('auth.admin-login');
}
protected function guard() {
return Auth::guard('admin');
}
public function login(Request $request) {
//validate the form
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6',
]);
//attemp to login
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
//Authentication passed...
return 'success';
//return redirect()
//->intended(route('admin.dashboardsdsdsd'));
//if login success then redirect to page
// if not success then redirect to back
}
return redirect()->back()->withInput($request->only('email', 'remember'));
}
}
Route is
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('admin', 'AdminController#index')->name('admin.dashboard');
Route::get('admin/login', 'Auth\AdminLoginController#showLoginForm')->name('admin.login');
Route::post('admin/login', 'Auth\AdminLoginController#login')->name('admin.login.submit');
Please help me to resolve this issue, so that admin can login.
There are two main reasons:
First one: your hash password is not correct, so open this website ( which is MD5 Hash Generator) then put any number that you like, take it and create a new admin account directly from database and paste the password then try
Second one: Clear your cache and view :
php artisan view:clear
php artisan cache:clear
As previously asked I want to use different table(clients) for auth.
I have allready edited some codes, but still I am not able to use auth method.
I've tried too many variations but, still can't login with auth it after user register.
config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'client' => [
'driver' => 'session',
'provider' => 'clients',
]
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Client::class,
],
client modal file.
class Client extends Authenticatable
{
protected $guard = 'client';
public $timestamps = true;
protected $fillable = [
'email',
'password',
'fullname',
];
protected $hidden = [
'password', 'remember_token',
];
}
clients migration file.
Schema::create('clients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('email')->unique();
$table->string('password');
$table->string('fullname');
$table->rememberToken();
$table->timestamps();
});
Controller
public function showRegister()
{
return view('pages.register');
}
public function doRegister(ClientRequest $request)
{
$validated = $request->validated();
$request->merge(['created_at' => Carbon::now()]);
$request->merge(['password' => Hash::make($request->password) ]);
$add = Client::create($validated);
auth('client')->attempt($add);
return redirect('my_profile')->with('success', 'success');
}
After submit register form I get this error.
Symfony\Component\Debug\Exception\FatalThrowableError
Argument 1 passed to Illuminate\Auth\SessionGuard::attempt() must be of the type array, object given, called in C:\wamp64\www\laravel\app\Http\Controllers\HomepageController.php on line 117
when I change my attempt code like this, It returns null.
auth('client')->attempt([
'email'=> $request->email,
'password'=> $request->password
]);
If user is getting created successfully try this line.
auth('client')->login($add);
Remove this line
auth('client')->attempt($add);
My Laravel project uses the following user types: students, parents, trainers. Now I would like to use Laravel Nova for the backend to manage the different resources.
Nova uses the users table, and model as default, however, I would like to use the admins table and model for the login.
I already created a custom admins table and model and updated the config/auth.php.
database/migrations/create_admins_table.php
...
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 60);
$table->string('email', 60)->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
config/auth.php
'guards' => [
...
'admins' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
...
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
What changes do I have to make to use the admins' table/guard for the Nova login?
In your /config folder, you'll find the file nova.php. Inside it, change the following line of code to specify your guard. For example:
'guard' => 'admins',
i need to create three authentication: user, admin, restUser.
I managed to create multiple login for user and admin but when try to add login for restUser it returns user form...
this is my code:
app/Teretaneusers.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Teretaneusers extends Authenticatable
{
use Notifiable;
protected $guard = 'teretaneuser';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
and I create table in MySQL database teretaneusers with column: name, email, password
config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
'teretaneuser' => [
'driver' => 'session',
'provider' => 'teretaneusers',
],
'teretaneuser-api' => [
'driver' => 'token',
'provider' => 'teretaneusers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admins::class,
],
'teretaneusers' => [
'driver' => 'eloquent',
'model' => App\Teretaneusers::class,
],
],
Controllers/UserGymController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserGymController extends Controller
{
public function __construct()
{
$this->middleware('auth:teretaneuser');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('teretaneuser');
}
}
Controllers\Auth\UserGymLoginController.php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
class UserGymLoginController extends Controller
{
public function __construct()
{
$this->middleware('guest:teretaneuser');
}
public function showLoginForm(){
return view('auth.teretaneuser-login');
}
public function login(Request $request){
//validate the form data
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]
);
//attempt to log user in
if(Auth::guard('teretaneuser')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
return redirect()->intended(route('userGym.dashboard'));
}
return redirect()->back()->withInput($request->only('email','remember'));
}
}
auth/teretaneuser-login.blade.php
form class="form-horizontal" method="POST" action="{{
route('userGym.login.submit') }}"
and web.php
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::prefix('admin')->group( function() {
Route::get('/login', 'Auth\AdminLoginController#showLoginForm')->name('admin.login');
Route::post('/login', 'Auth\AdminLoginController#login')->name('admin.login.submit');
Route::get('/', 'AdminController#index')->name('admin.dashboard');
});
Route::prefix('userGym')->group( function() {
Route::get('/login', 'Auth\UserGymLoginController#showLoginForm')->name('userGym.login');
Route::post('/login', 'Auth\UserGymLoginController#login')->name('userGym.login.submit');
Route::get('/', 'UserGymController#index')->name('userGym.dashboard');
});
Can somebody tell me where I'm wrong? When I try login from adress http://localhost/logovanje/public/userGym/login
it redirest me to http://localhost/logovanje/public/home
I use Laravel 5.4
I did the same for the admin and it worked.
Most likely you still have a valid session and you got a middleware (possibly RedirectIfAuthenticated) that is coming into play.
I think you could use Sentinel for this as it has an authentication package called roles and permissions
Here's a link for its documentation.