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);
    }

[Solved] Auth Check not working on Laravel 5.*

Problem :
I want to check if user has logged in or not, when I try
@unless (Auth::check())
    you are not logged in
@endundless

the Auth Check always return false.

Solution:
Use Auth::user() instead of Auth Check, so the code will be like :
@if (isset(Auth::user()->id))
      <h1>sudah login</h1>
@else
       <a href="{{ URL::to('auth/register') }}">Login</a> | <a href="{{ URL::to('auth/register') }}">Daftar</a>

 @endif