binshops / laravel-blog

Laravel Blog Package/ Laravel CMS. Easiest way to add a blogging system to your Laravel website. Laravel Blog.
https://www.binshops.com
MIT License
428 stars 134 forks source link

"Auth::check()" 401 error while logged in #76

Closed dappstatus closed 2 years ago

dappstatus commented 2 years ago

I try to get laravel-blog running on an already existing laravel project.

Everything seems fine except of the auth part. I'm logged in as admin but "\Auth::check()" in "UserCanManageBlogPosts" results in "error evaluating code" when debugging.

I changed '_usermodel' to \App\Admin::class which is my user/admin model. This is how my model looks like:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    use Notifiable;
    // The authentication guard for admin
    protected $guard = 'admin';
     /**
      * The attributes that are mass assignable.
      *
      * @var array
      */
    protected $fillable = [
        'username', 'password',
    ];
     /**
      * The attributes that should be hidden for arrays.
      *
      * @var array
      */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Enter your own logic (e.g. if ($this->id === 1) to
     *   enable this user to be able to add/edit blog posts
     *
     * @return bool - true = they can edit / manage blog posts,
     *        false = they have no access to the blog admin panel
     */
    public function canManageBinshopsBlogPosts()
    {
        // Enter the logic needed for your app.
        // Maybe you can just hardcode in a user id that you
        //   know is always an admin ID?

        if($this->username === "testadmin")
        {
            // return true so this user CAN edit/post/delete
            // blog posts (and post any HTML/JS)
            return true;
        }
        // otherwise return false, so they have no access
        // to the admin panel (but can still view posts)
        return false;
    }
}

I tried to add "use Illuminate\Support\Facades\Auth;" to "UserCanManageBlogPosts" and changed "\Auth::check()" to "Auth::check()" which doesn't result in an error anymore, but that for in a false, although I'm logged in as admin.

I'm not sure how to get the auth running correctly here, what could be the problem and what could solve it?

dappstatus commented 2 years ago

Found the solution in a old Issue thread: "Auth::guard('admin')->user()" shows the logged in user.