I'm using laravel 5.4 and added a new guard named website to my application and I did all things that are required for that.
But a problem that I have is that some views are shared between both default and website guard. for example when I want to show current logged in user I used this :
<div class="pull-left info">
@if(Auth::guard('website')->check())
<p>{{Auth::guard('website')->user()->name}} Website</p>
@elseif (Auth::check())
<p>{{Auth::user()->name}}</p>
`@endif
<a href="#"><i class="fa fa-circle text-success"></i> Online</a>
</div>
And this is just one place that I must to check which guard is logged in and it is Naturally difficult,time consuming and dirty way.
On the other hand some operations is same for both guards. for example there is a bannerController store method that can save some banner model for them like this (Of course this example is written just for website guard as you can see ):
public function store($website,StoreBannerAds $request)
{
$allData = $request->all();
$website = Auth::guard('website')->user();
$ads = $website->banner_ads()->create($allData);
$result = ['success' => true, 'generated_id' => $ads->banner_ads_id];
return $result;
}
In this case if I want to store some banners for default user logged in User I should use an if () or completely another store method:
public function store($website,StoreBannerAds $request)
{
$allData = $request->all();
if (Auth::guard('website')->check()) {
$model = Auth::guard('website')->user();
} else if (Auth::check()){
$model = Auth::user();
}
$ads = $model->banner_ads()->create($allData);
$result = ['success' => true, 'generated_id' => $ads->banner_ads_id];
return $result;
}
I want know are there any clean and appropriate ways to do that Or I must to use this approach still؟
Update:
What I changed before in auth.php configuration file are these:
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'website' => [
'driver' => 'session',
'provider' => 'websites',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'websites' => [
'driver' => 'eloquent',
'model' => App\Website::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],