Justin Boxem
Justin Boxem

Reputation: 153

Laravel - Auth check on database email field for verification

I am working on a small personal project and I would like to ask if there's a possibility to run an auth check on the database value of "verified_employee". In my current setup, "verified_employee" is a boolean database field that is at default set at 0.

My question would be as follows: "Is it possible inside a blade view (i.e. "Home.blade.php") to run a check like "if auth::user()->verified_employee = '1'" then let the user continue. If not, adjust the view as such that a message is shown in the sense of "Your account is not activated by the administration team. Please await for them to do so".

User model

<?php

namespace App;

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

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password', 'admin', 'verified_employee'
];

public function Hours() {
    return $this->hasMany(Hour::class);
}

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}

View

@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">Dashboard</div>
            <!-- CHECK IF user->verfied_employee is true -->
            <!-- IF YES -->
            <div class="panel-body">
                Welkom, {{ Auth::user()->name }}
            </div>
            <!-- IF NO -->
            <div class="panel-body">
                Please wait till your account has been verified by the team.
            </div>
        </div>
    </div>
</div>
</div>
@endsection

Auth -> Login controller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}

Upvotes: 2

Views: 1449

Answers (3)

Tomasz Rup
Tomasz Rup

Reputation: 859

You can use ifs in blade, like that:

@if(Auth::user()->verified_employee)

<some-html></some-html>

@else

<other-html></other-html>

@endif

Assuming verified_employee is a boolean, ths should do the trick.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

If you don't want to show the view, you can do this in a controller method:

if (auth()->check() && auth()->user()->verified_employee) {
    return view(....);
} else {
    return redirect('/')->with('message', $message);
}

Upvotes: 0

EddyTheDove
EddyTheDove

Reputation: 13259

Yes, it's perfectly fine

@if( Auth::check() && Auth::user()->verified_employee )
    //do stuff
@endif

You field is boolean so it will return true or false.

Upvotes: 1

Related Questions