ruffsaint's avatar

How to set up Administrator and user authentication

Please friends am like 4weeks in laravel and am having big issue setting up an administrator and user authentication in an application i want to build now. I dont really know how to handle the auth since it only works for users. And i want my user and administrator to have different views. Please send ur helping hand

0 likes
5 replies
cbojer's avatar

In order to help you we will need more information.

How have you set up the models? Do you have a model for Administrators and a model for Users, or do you have a single User model with roles or a type of Administrator and User?

How is the code that does the authentication set up now?

This should allow us to get started helping you.

ruffsaint's avatar

@cbojer thanks for you concern.

i Havent done much, i only a user model and and a role column in a the users table in my database, i created an administrator model but i dont knw what to include. because i dont knw if am going to use the ready-made authentication provided by laravel 5.

mstnorris's avatar

@ruffsaint you asked this question yesterday https://laracasts.com/discuss/channels/laravel/user-admin-authentication

Also, you've asked a lot of basic questions, always one-liners and never following the Guidelines that have been given to you on multiple occasions. You asked your first question PLS I NEED YOU GUYS HELP 5 months ago (not 4 weeks), and since then you have continued in the same vain.

I suggest subscribing to the videos, watch the series, or, if that isn't an option for you, read through the Laravel Docs and when you get stuck, show us what you've done and then we can help you.

Also, please accept answers that have been given to you (if of course they have answered your question). Ive read through some of the answers you have been given to very vague questions you've asked and it is a long process but you say that they've helped. So mark it as such.

Regards, -M

cbojer's avatar

I'm currently using the following in an application, and it's working just fine:

Have a role/type column on your user model. E.g. for Administrators it might have the value "admin" and for regular users it might be "user".

Then when you are authenticating users, you can authenticate with extra conditions, e.g. check whether the user is an administrator for admin login:

if (Auth::attempt(['email' => $email, 'password' => $password, 'role' => 'admin']))
{
    // The user is an admin
}

Also, I would recommend making a few methods on the User model, that checks whether the user is an admin or a regular user:

public function isAdmin()
{
    return $this->role === 'admin';
}

With this, you can easily check in your controllers or views, whether the user is an admin or a regular user, and then act accordingly, e.g. by serving a different view for one of them.

Hope this helps you on your way.

Please or to participate in this conversation.