I am stuck in creating custom auth for my custom laravel package

41 Views Asked by At

So I create A model for custom auth Store

namespace Stegback\RetailShop\Models;
use Illuminate\Foundation\Auth\User;
class Store extends User
{
    protected $guard = 'retail_shop';
    protected $fillable = [
        'name', 'email', 'password', 'role', 'storeName', 'active', 'address', 'pincode'
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
    protected $casts = [
        'password' => 'hashed',
    ];
}

then I tried to login with this model like

  public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator);
        }

        if (Auth::guard('retail_shop')->attempt(['email' => $request->email, 'password' => $request->password])) {
        $storeName = auth('retail_shop')->user()->storeName;
        $userId = base64_encode(auth('retail_shop')->user()->id);
        // dd(auth()->guard('retail_shop')->check());
        return redirect()->route('ReatilShop.store-dashboard', ['store' => $storeName, 'i' => $userId]);
    } else {
        return redirect()->back()->withInput()->withErrors(['errors' => 'Invalid email or      password']);
    }
  }

for this I adjust my auth.php in config file define as new guard like

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

    'retail_shop' => [
    'driver' => 'eloquent',
    'model' => Stegback\RetailShop\Models\Store::class,
    ],
],

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

i am getting Auth at login function but when i redirect to dashboard then auth is returning null value. this is my pacakge Service provider

namespace Stegback\RetailShop;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;

class RetailShopServiceProvider extends ServiceProvider
{
    public function boot()
    {
    if (File::exists(__DIR__ . '\Helpers\CommonHelper.php')) {
        require __DIR__ . '\Helpers\CommonHelper.php';
    }
    $this->loadRoutesFrom(__DIR__ . '/routes/retail-shop-routes.php');
    $this->loadMigrationsFrom(__DIR__ . '/database/migrations');
    $this->loadViewsFrom(resource_path('views'), 'laravel');
    $this->loadViewsFrom(__DIR__ . '/views/RetailShop', 'RetailShop');

    $this->publishes([
        __DIR__ . '/views' => resource_path('/views'),
    ], 'stegback-retail-shop-views');

    // $this->app['router']->aliasMiddleware('retail_shop_guard',   \Stegback\RetailShop\middleware\StoreGuardAuthMiddleware::class);
    }

    public function register()
    {
    //
    }
 }

and routes

 Route::middleware('auth:retail_shop')->prefix('/retail-shop')->name('ReatilShop.')->group(function () {
    Route::get('/logout', [StoreAuth::class, 'logout'])->name('logout');
    Route::get('/dashboard', [StoreDashboard::class, 'store_dashboard'])->name('store-dashboard');
     Route::get('/wallet', [StoreDashboard::class, 'wallet'])->name('store-wallet');
});

I am trying to get auth in cusotm laravel package using custom auth guard.

1

There are 1 best solutions below

0
Jeyhun Rashidov On

After making changes to the config/auth.php file or any other configuration files, it's necessary to clear the cache:

php artisan config:clear
php artisan route:clear
php artisan cache:clear

or you can simply use this command:

php artisan optimize:clear