Jump to content

Auth::guard('admin')->user() returns NULL


Recommended Posts

hi.

I created a guard with the name "admin".


'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
    ],


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ],

      ],

 

and login with method attempt:

        if(Auth::guard('admin')->attempt($credentials)) {
            $request->session()->regenerate();
            return redirect()->route('admin.index');
}

 

And I have assigned a Middlewareto the Route:

Route::prefix('dashboard/admin/brand')->middleware(['auth:admin'])->group(function () {
    Route::get('/view', [BrandController::class, 'BrandView'])->name('all.brand');
}

 

But when I want to access the properties of Guard Admin, it returns null:

class BrandController extends Controller
{


    public function __construct()
    {
dd (Auth::guard('admin')->user());   //return null
dd (Auth::guard('admin')->guest()); // return true
	}
{

 

What is the problem?

 

Link to comment
https://forums.phpfreaks.com/topic/315156-authguardadmin-user-returns-null/
Share on other sites

53 minutes ago, maxxd said:

 

<?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 Laravel\Sanctum\HasApiTokens;

class Admin extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use Notifiable;


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
//    protected $fillable = [
//        'name',
//        'email',
//        'password',
//    ];

    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];
}

 

When I create a guard, which middleware should I use? Have I chosen the right middleware?

 

After logging in, Returns the details of the admin table:

    if(Auth::guard('admin')->attempt($credentials)) {
            dd (Auth::guard('admin')->user()); //return true

 

But in the controller on which I applied middleware it returns null

 

Edited by ali_254
  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.