Saturday, June 25, 2016

AsgardCMS check authentification on frontend / blade

1. Declare Authentification on Controller

use Modules\Core\Contracts\Authentication;

2. Call Authentification and set it into variable

public function __construct(Authentication $auth)
{
       $this->auth = $auth;
}

3. Passing it into function

public function index()
{
$auth = $this->auth;
return view('user::main.dashboard', compact('auth'));
}

4. Do check authentification
@if ( $auth->check() )
     <a href="{{ URL::to('auth/logout') }}">Sign out</a>
@else
     <a href="{{ URL::to('auth/register') }}">Login</a> | <a href="{{ URL::to('auth/register') }}">Daftar</a>
@endif 


Done,

If you want to use Authentification in many controller you can use it in serviceProvider

This is my sample code

<?php namespace Modules\Profile\Providers;

use Illuminate\Support\ServiceProvider;
use Modules\Profile\Entities\Profile;
use Modules\Profile\Repositories\Eloquent\EloquentProfileRepository;
use Modules\Profile\Repositories\ProfileRepository;
use Modules\Core\Contracts\Authentication;

class ProfileServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;


    private $auth;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerBindings();
    }

    public function boot(Authentication $auth2)
    {
        view()->share('auth2', $auth2);
    }

No comments:

Post a Comment