vkpaul123's avatar

Auth::guest() working only for 'users' and not for 'admins'

Hello, I am very new to Laravel. I am working on a school project which is to make Online Recruitment Website. I want to have 3 different logins for 'Admins', 'JobSeekers' and 'Employers'. I used the inbuilt Auth for 'JobSeekers', then i made copies of all 4 controllers (Login, Register, ForgotPassword, ResetPassword) for 'Employers' and 'Admins'. I made required Routes for Admin and Employer and Also AdminController and EmployerController, where i over-rode the show-login-page functions for both Employers and Admins. I made 'admin' and 'employer' guards and providers. The Employer and Admin Authentication is working fine. I wanted to check if the users ('Jobseeker', 'Admins' or 'Employers') have logged in using Auth::guest() function. But this is working only for JobSeekers which is using the inbuilt Auth system of Laravel, And Auth::guest() is NOT my Over-ridden systems for Admin and Employer.

Please Help.

0 likes
6 replies
Talky's avatar

You don't have to copy any controllers and login/register views for different ranks. Just make a new column named 'rank' in users table in database. Set it default to 0 (job seeker), you'll make number 1 for employer and number 2 for admin. Then make middleware let's say "RankMiddleware" and use parameter to define if a user is allowed to visit certain route. Your middleware check function should look like this (in Controller):

public function __construct() {
    $this->middleware('RankMiddleware:2'); // checks if a user is admin
}

and your middleware function could look like this:

    public function handle($request, Closure $next, $rank)
    {
        if (Auth::check()) {
            if (auth()->user()->rank >= $rank)
            return $next($request); //redirects as inteded
        }

        return back(); //redirects back if user is not logged in or has rank lower than required
    }

Useful link: https://laravel.com/docs/5.4/middleware#middleware-parameters

1 like
vkpaul123's avatar

@Talky Thank you so much for the reply.

I have 3 tables in the MySQL Database, namely 'users' (created by default Auth), 'admins' (created by my migrate) and 'employers' (created by my migrate, similar to admins).

The login is working for these three tables individually. But my aim is to check which type of user is logged in. i thought of using Auth::guest(), which works for 'users' (JobSeekers), and not for 'admins' and 'employers'

Talky's avatar

@vkpaul123 you picked much harder way than it is usually solved in Laravel. Basically what you do is you store all users in one table, just add one additional 'rank' column in the table. Then follow my instructions above. You'll be fine, believe me. Once you get along with middleware magic you'll be happy it exists!

If - still - you wish to stick with your method, you'll have to pull out Auth::guest() function out of vendor, copy it to your User Model and add functionality for 'admins' and 'employers'.

Hint: Never change things in vendors folder! Always pull out and override in your models. Changes made in vendors will wipe out when composer's updated.

2 likes
vkpaul123's avatar

@Talky Thank you so much. I'll try to make one table (users, default from Auth) with one column added to keep track of userType(rank).

I'll use your method.

Thank you so much. I'll keep you informed.

karthik_dev's avatar

I would suggest you to mention guard. Say

@if(Auth::guard('admin')->guest())
echo"guest user";  //for logged out admin
@endif

Please or to participate in this conversation.