Laravel: Auth guard [sanctum] is not defined - php

I'm trying to implement auth in Laravel via Sanctum. I did all steps from documentation. Generation of Token works fine but when I try to use auth:sanctum middleware it returns the error Auth guard [sanctum] is not defined.
Here are my files:
/routes/api.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Dashboard;
Route::middleware('auth:sanctum')->get('/dashboard/get_current_client/', [Dashboard::class, 'get_current_client']);
Route::get('/dashboard/client_data/', [Dashboard::class, 'client_data']);
/config/auth.php
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Client::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
/app/Models/Client.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Client extends Authenticatable
{
use HasFactory, HasApiTokens, Notifiable;
protected $table = 'client';
protected $primaryKey = 'client_id';
public $timestamps = false;
}
/app/Http/Controllers/Dashboard.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Client;
class Dashboard extends Controller
{
public function client_data () {
$user = Client::where('client_id', 1)->first();
return $user->client_id;
}
public function get_current_client(Request $request) {
var_dump($request->user());
}
}

I found the problem. For some reason cache in /bootstrap/cache has not updated. After manual removing it, everything started working.

to fix it add the below code to file "/bootstrap/cache/packages.php"
'laravel/sanctum' => array (
'providers' =>
array (
0 => 'Laravel\\Sanctum\\SanctumServiceProvider',
), ),

Related

Laravel 8 : Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given

I am trying to create multiple guard authentication for Users and Admins for my application. Before you mark it as duplicate or something, please read it and let someone help me. I have tried all the possible solutions from same issues solutions but nothing worked till now.
Here is my code:
auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'providers' => 'admins'
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\AdminUser::class,
],
],
AdminUser.php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
class AdminUser extends Authenticatable
{
use HasFactory, Notifiable;
protected $table = 'admin_users';
protected $guard = 'admin';
protected $fillable = [
'first_name',
'middle_name',
'last_name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
AdminAuthController.php
class AdminAuthController extends Controller {
public function __construct()
{
$this->middleware('auth:admin');
}
public function getLogin()
{
return view('login');
}
}
web.php
Route::get('/login', 'LoginController#index')->name('login');
Route::get('/admin/login','AdminAuthController#getLogin');
When I go to my-application/login it ask for username and password and let me in once authenticated. But when I go to my-application/admin/login, it gives me this error:
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in /var/www/vhosts/my-application/httpdocs/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 124
I tried solutions with the same issue, but nothing helped.Laravel documentation is bit confusing and so I am here to seek your help. Please help me out and I appreciate your help in advance. I am using Laravel 8.*
I faced this issue just yesterday. I am relatively new to web development. It turned out the issue arose from my cofig/auth.php. In setting the "guards" section, I set the provider to reference the table name ('provider' => 'admins_user') rather than the "providers" section below it ('provider' => 'admins').
Solution: When I pointed guards to the "providers" section, it was resolved. I know it's a silly mistake on my side but I hope this helps someone out there with similar circumstances.

Attempt() method is undefined in Laravel 6

I am trying to authenticate admin in Laravel project. my auth.php as shown below:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
AdminController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function adminLogin(){
return view('auth.adminLogin');
}
public function cheackAdminLogin(Request $request){
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('admin')->attempt(['email' => $request->email,
'password' => $request->password])) {
return redirect()->intended('/admin');
}
return back()->withInput($request->only('email')); */
}
}
AdminDashboardController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminDasboardController extends Controller
{
public function adminPanel()
{
return view('adminPanel');
}
}
The problem that I encountered that when I want to check the admin login, the attempt() function when I wrote it, I get an error message in the editor "Undefined Method".
I tried to using auth()->guard('admin')->attempt() instead of Auth::guard('admin')->attempt but the problem is same.
please help
It may be a problem of type hint
use Illuminate\Auth\SessionGuard;
/** #var SessionGuard $adminGuard */
$adminGuard = Auth::guard("admin");
if ($adminGuard->attempt($credentials)) {
...

Laravel 5.6 multi auth keeps on authenticating on default model

I have a two table login: examinee and company. company is my default auth,
every time i try to attempt auth in examinee it keeps on using the default table which is the company.
I've seen several problems that are similar to mine but it doesn't seem to work for me or I might have overlooked something that maybe some of you might be able to see.
What am I doing wrong?
Auth.php:
'defaults' => [
'guard' => 'web',
'passwords' => 'companies',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'companies',
],
'api' => [
'driver' => 'token',
'provider' => 'companies',
],
'examinees' => [
'driver' => 'session',
'provider' => 'examinees',
],
],
'providers' => [
'companies' => [
'driver' => 'eloquent',
'model' => App\Company::class,
],
'examinees' => [
'driver' => 'eloquent',
'model' => App\Examinee::class,
],
],
'passwords' => [
'companies' => [
'provider' => 'companies',
'table' => 'password_resets',
'expire' => 60,
],
'examinees' => [
'provider' => 'examinees',
'table' => 'password_resets',
'expire' => 60,
],
],
LoginController:
public function showLoginForm()
{
return view('examinee.auth.login');
}
protected function guard()
{
return Auth::guard('examinees');
}
public function login(Request $request) {
$user = Examinee::where('email', $request->get('email'))->first();
if (Auth::attempt(['id' => $user->id, 'password' => $request->get('password')])) {
// prints data from Company table instead of Examinee...
echo "AUTH USER:<pre>";
print_r(Auth::user());
echo "</pre>";
// return redirect('/home');
}
}
Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Examinee extends Authenticatable
{
use Notifiable;
protected $table = 'examinees';
protected $fillable = ['email', 'password'];
protected $hidden = ['password', 'remember_token'];
public $timestamps = false;
}
How ive done this in the past is to add the guard to your auth
Change
Auth::attempt(['id' => $user->id, 'password' => $request->get('password')
to
Auth::guard('examinees')->attempt(['id' => $user->id, 'password' => $request->get('password')

Laravel guard not working

I'm trying to implement admin/users login with laravel 5.5. I've created the Admin model, exactly the same as the user one, and set the guards for the admin. But during login, the guards are not being passed:
This is my AdminController:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class AdminController extends Controller{
use AuthenticatesUsers;
protected $guard = 'admin';
public function showLoginForm(){
return view('admin')->with(['title' => 'Panel de Administrador - Iniciar Sesión', 'bodyClass' => 'admin-view']);
}
}
I tried to debug, and went into the vendor folder, to AuthenticatesUsers class, and debugged the request:
As you can see, it's using the User model and the web guard. Here's on the config file for the guards:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
]
],
and then I have the provider:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class
]
],
Nevermind I figured it out. Guard is not a property of the class, it's a function so I have to replace the inherited guard function, and use the Auth facade, like this:
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller{
use AuthenticatesUsers;
public function guard(){
return Auth::guard('admin');
}
public function showLoginForm(){
return view('admin')->with(['title' => 'Panel de Administrador - Iniciar Sesión', 'bodyClass' => 'admin-view']);
}
}

Laravel 5.2 Error App\User cannot use Illuminate\Foundation\Auth\User - it is not a trait

I am pretty new to laravel 5.2. Right now I am trying to create a custom login. I created the login html. And on submit, the function shows error
App\User cannot use Illuminate\Foundation\Auth\User - it is not a trait
I couldn't find what is the error. The codes are below
Routes.php
Route::post('/', 'Admin\LoginController#postLogin');
LoginController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Auth;
use Request;
class LoginController extends Controller {
public function index() {
return view('admin.login');
}
public function register() {
return view('admin.register');
}
public function postLogin() {
$email = Request::input('email');
$password = Request::input('password');
if (Auth::attempt(['email' => $email, 'password' => $password])) {
//echo "success";
return redirect('admin/dashboard');
} else {
return redirect('/');
}
}
}
User.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable,
CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['keyword', 'remember_token'];
}
Auth.php
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'users' => [
'driver' => 'eloquent',
'model' => 'App\User',
'table' => 'users',
'password' => [
'email' => 'emails.password',
'expire' => 60,
],
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
];
I am following the tutorial from here
Please help me. I am stuck here from last few hours
You should use this namespace:
use Illuminate\Auth\Authenticatable;
It's a trait you should use. Now, you're trying to use a class as a trait:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User {
use Authenticatable ...

Categories