Login for two different users using laravel - laravel

I have made two login system. a) for Users and b) for Administrator.
But the problem is the app/config/auth.php in laravel has the default model=>'User' and
table=>'users' and I have two models and tables for different users.
How can I use the two different models and tables for login?

What you're doing is considered bad practice - you should take a look at role based permission systems or helpers. I have a few suggestions for you:
Sentry - Most popular, comes with permission system and roles
Entrust - Let's you add role based permissions
You should never repeat stuff for the same type of resource - that's like having a blog where you have a table for each category, it just doesn't really work and it's incredibly time consuming to keep up to date and in sync.
You should change your style right now, take the opportunity, it will save you time in the long run, believe me.

Related

Storing User Roles in Session Laravel

Hello there I am working on a project in Laravel in which i need to assign permission to each user so that i could verify on each blade file or controller function to check whether the current user has the permission to perform this. Moreover, the side nav links are also generated using these permissions dynamically.
I created two tables:
1: User => [ID, Name .....]
2: Permissions => [ID, Name, user_id(fk)]
To solve this problem, i have stored all the permissions of users in session at the time of login. So that i can verify all permissions on each page and generate links fetching from session.
Is that good approach or there is any better solution for this
It would be good if you had share more code but i can see what you are want to archive. Firstly you dont need to store in the Session because you have already a relation between user Object and Permission. Add to your User model this lines of code:
public function permissions() {
return $this->belongsTo(User::class);
}
Then you have access in your blade or controller to the permission. Small example in the controller:
$user = User::find(1);
dd($user->permissions);
// you can write a condition to check if user has Permission etc.
Yes you can store this is the session. But the more better option will be to get the permission through relation object like
user::find(1)->permissions()
Well if you're asking "better solution" ... but I Not sure if it's too late for this information since you're already developing the project. However, I would recommnend this package for your long term management (for both user and dev).
Spatie Laravel-permission package
It has Role based permission and Direct permission design (which is similar to your design). Once you installed the package then role and permission tables are created for you.
Once you created desired roles with permissions, it's easy for you to manage which page to allow for which role and which button show be shown.
You can check roles in your controller for those who can view this page.
In blade, you can check both roles and permission for which button to show or disable.
Hence, your don't need to worry about session settings or session expires. It's better for maintaining and development in future.
The Spatie package has simple syntax and easy to work with.
Installation:
composer require spatie/laravel-permission
Syntax:
Basic usage and syntax
There are plenty information or tutorials out there.

How to create two separate sites login/registration with single database users table

I have two different websites one in Cakephp 2.6 Framework and another in Laravel 8x Framework.
I have two different databases but i want to uses my Cakephp site's database users table for login/registration for both sites, how to integrate this solution in my frameworks.
Thanks in advance!
According to my understanding: It doesn't related to frameworks, instead it is a matter of business logic and can be handled with two steps.
step 1 => Add a "system_code/website_code" column in the users table.
step 2 => Add additional check for system_code along with login credentials in your code base of both websites.

Automatic Log In/Authentication Laravel from Other Laravel

I have two Laravel applications that use the same database and, therefore, have the same users and passwords.
Let's say the applications are called A and B.
If a user logs into A, what can I do so that they're automatically logged into B? So if they log into A, then when they go to B, they will not have to type in their login information again.
Both applications are on the same domain, but B is a subdomain and is not part of the same project as A.
Thank you in advance!
When you say Both applications are on the same domain, but B is a subdomain and is not part of the same project as A. can i safely assume that they are two separate installations of Laravel, but they share the same database? I that's the case, you could try this:
Switch the session driver to database in your .env file to "database", for reference you should look at the comments in config/session.php, near the 'driver' => env('SESSION_DRIVER', 'file'), option. Be sure to manage the tables accordingly (see https://laravel.com/docs/5.5/session#driver-prerequisites)
In your .env file, specify this row: SESSION_DOMAIN=.domain.ext (notice the dot near the equal sign).
Alternatively, if even the databases are separate you could try the same config as above using the cookie driver instead fo the database. let me know if this helped.
edit: more a suggestion than an answer, laravel is capable of handling multiple websites/domains natively (example: Route::domain('sub.domain.ext'). Pratically speaking, you organize the routes this way in routes/web.php. With a wise database management (and queries) you can easily mantain 2 separate websites within 1 Laravel application. This assuming you have control over your webserver, of course. If that's the case, i would suggest a refractoring. It's really worth the effort.

Different fields on different user types

I am creating an application where there are different types of users and and there has to be different fields. I have currently made different tables for different users and at registration the fields are saved to different tables. Is there another way to tackle the problem.
Basically there are two users, And I have made the tables
normal_user
-id
-username
-email
-profile_photo
another_user
-id
-username
-email
-profile_photo
-city
-phone_number
-first_name
-last_name
Is there an easier way or good way ?
Laravel has ACL ( Access Control List ) shipped right from version 5.1. You can make use of Roles and Permissions like how you'd normally see in WordPress. Jeffery Way has explained this excellently in laracasts website.
https://laracasts.com/series/whats-new-in-laravel-5-1
The last 4 videos explains Policies and Gate contracts to implement roles and permissions in Laravel. I believe the last video explains briefly about User Roles and Permissions.

Is it possible to set content related permissions on cartalyst/sentinel?

i'm new to laravel and cartalyst/sentinel, but for this project i'm facing out an authorization problem:
I have to set User CRUD permissions for the single content, and i'm facing out how to do id with cartalyst/sentinel.
(a lil' example: if i have a blog, i wanna set User CRUD permissions separately for each article).
Can anyone help me to find some documentation about something like this?
I have already implemented this kind of permissions with CakePHP, but is the first time i'm using laravel.
Thank You
The way I think you'd best go about this is by setting the users permission by using the article ID. You could give a user permissions like this: "articles.14.read":1,"articles.24.*":1. 14 & 24 being IDs of articles I made up.
At the last seems noone never has been capable to solve this problem, and i cant test the solution proposed. So i've falled back to the ACL stock support of Laravel 5.0.
Baybe i'll retry when i need the multisession system or other things from sentinel.

Resources