Tuesday, March 14, 2017

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, 
            </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']);
}
}



via Justin Boxem

Advertisement