Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peterpan26's avatar

blade show user email logged in

hello im logging in with ldap on php laravel, when i have a successfull bind i do the following:"

 $emailExists = DB::table('user_roles')->where('email', $email)->exists();
                
                    // Log the SQL query and email existence
                    Log::info('SQL query: ' . DB::table('user_roles')->where('email', $email)->toSql());
                    Log::info('Email Exists: ' . ($emailExists ? 'true' : 'false'));
                
                    // If the email does not exist, return an error
                    if (!$emailExists) {
                        return back()->with('error', 'You are not authorized to login with this email');
                    }
                
                    // Retrieve roles associated with the email
                    $userRolesData = DB::table('user_roles')->where('email', $email)->get();
//to validate the roles on navbar i do like this:          
@php
    $userRoles = $userRolesData->pluck('role')->toArray();
@endphp
@if(in_array('Administrator', $userRoles))

my question is im not being able to show current user logged in email on the blade

0 likes
6 replies
enoch91's avatar

@peterpan26 if you're using LDAP for authentication, after a successful LDAP bind, you can log the user in using Laravel's authentication system

public function login(Request $request)
{
    $email = $request->input('email');
    
    $emailExists = DB::table('user_roles')->where('email', $email)->exists();

    if (!$emailExists) {
        return back()->with('error', 'You are not authorized to login with this email');
    }

    // If authentication is successful, create or retrieve the user and log them in
    $user = User::where('email', $email)->first();

    Auth::login($user);

    return redirect()->intended('dashboard'); // Redirect to the intended page
}

Now, in your Blade template, you can access the authenticated user's email like this

@if(Auth::check())
        <p>Logged in as: {{ Auth::user()->email }}</p>
    @endif

    @php
        $userRoles = Auth::user()->roles->pluck('role')->toArray();
    @endphp

    @if(in_array('Administrator', $userRoles))
        <p>Welcome, Administrator!</p>
    @endif
peterpan26's avatar

@enoch91 I need to match the emails with the ones i have on userRoles table on my database, because the user's database it's not mine and i have no access to it...

enoch91's avatar
enoch91
Best Answer
Level 2

@peterpan26 Since you do not have access to the user's db, you should update the controller's login method to be like

public function login(Request $request)
{
    $email = $request->input('email');

    $emailExists = DB::table('user_roles')->where('email', $email)->exists();

    if (!$emailExists) {
        return back()->with('error', 'You are not authorized to login with this email');
    }

    // Store user email in session
    session(['user_email' => $email]);

    $userRolesData = DB::table('user_roles')->where('email', $email)->get();

    return redirect()->route('home');
}

Then on the blade, you can get the email like so

@if (session('user_email'))
        <p>Logged in as: {{ session('user_email') }}</p>
    @else
        <p>No user logged in.</p>
    @endif
achatzi's avatar

@peterpan26 You can store the email and the user roles in the session when the user is logged in succesufully and delete them when they log out.

//in your code after you get the roles
$userRolesData = DB::table('user_roles')->where('email', $email)->get();

session()->put('logged-user-email', $email);
session()->put('logged-user-roles', $userRolesData->pluck('role')->toArray());

And in your blade template you can change the if directive to this

@if (in_array('Administrator', session('logged-user-roles', [])))

and show the email like this

{{ session('logged-user-email') }}
Snapey's avatar

I would create a users table and populate it from the emails in the roles table.

You can then log the user in through that users table and maintain the session between requests without having to keep referring to the ldap service. I don't see any point in trying to invent your own solution?

peterpan26's avatar

now to fix everything i just need how to do it on routes to check if the user email logged in matches the roles and make restrictions based on that, and on blade for example on navbar if the user_role is administrator show x menus and if not show only others

Please or to participate in this conversation.