Custom Registration process to onboard other users in Laravel - laravel

I am building a Laravel webapp, where I need to implement a custom registration process. Now I am not sure, whether I can/should use the built-in Authentication features from Laravel or not.
I have three types of users:
Admin users: My colleagues in my company who talk to clients, onboard them, etc.
Clients: Users who create certain tasks, that need to be done. They could more or less use a "standard registration" process
Service Providers: Users who fulfill tasks for clients. They need to be onboarded by our company. So our admin users would create their user profile and send them an invitation link.
Especially the Service Providers are giving me a headache. Is this kind of custom registration flow possible in Laravel or do I have to build my own custom athentication to accomplish this?

Thank you for posting your question. I have implemented Laravel Authentication with custom features before, I would recommend you to take the following steps:
Install Laravel Authentication.
Update users table with a column name type that will distinguish between users.
Create middleware for each user. Middleware helps you which user can access or perform which tasks.
If you have different elements for each user then you can also implement policies or gates to show specific elements on your blade template according to user type.
I would not recommend creating custom Authentication for this scenario, as you have to implement proper session handling throttling, forgot password, verify the process along with emails and notifications. which is built-in by default in Laravel.
If you want to edit the register and login function you can do that. ;)

Related

Sanctum authentication with roles

I'm trying to implement Sanctum SPA authentication with roles (user/admin). The thing is that I want to use separate admin table.
Read all of the laravel documentation related to that subject but with no result.
Tried to make a new guard and provider inside of config/auth.php but didn't succeed at conjunction it with Sanctum.
Any additional resources and ides how to do it will be appreciated! :)
Sanctum (SPA auth) only deals with authentication (who you are) - what you are asking/looking for is how to authorize users (what you can do) to perform/access specific resources.
If what you need is only to separate users between regular users and admins, you can get away with a boolean column on the users table: is_admin and then use that to check wether a user is an admin or not.
If you need more granular control, you could probably make use of Spatie's laravel-permissions package.

Authentication (and Authorization) in an app that's not fully a SPA - Laravel Passport

Hi I'm building an app with Larval and vue.js. This app is not a fully single page application but is a combination of vue and blade.
For example, the dashboard or login page and some other pages are SPA-based and are fully implemented with vue. But the landing page and some other pages have been created by blades that may have used vue components in some of them.
My question is about authentication in such cases. Is laravel passport appropriate for such cases? Or should I use jwt or something else? Because I need authentication on both types of pages (Vue-based, Blade-based). And it's done on almost every page.
On the other hand, I used laravel-permission - Spatie to control permissions and roles. Is it possible for passports to define roles and permissions and control based on them? I mean displaying or hiding elements and links, accessing certain sections, or doing some work with permissions checking.
What resources do you offer to start learning how to develop authentication and authorization in such apps?
Edit:
Maybe there was some ambiguity in my question, so I'll explain a little more. Suppose our website has three parts:
The first part that includes landing page, display of a post, display of all posts and more. This section is implemented by blade and may also use vue components.
The second part is the user profile page of the site where users can edit their own information, view their posts and edit them, view registered comments and more. This section is SPA and is fully implemented with Vue and only logged in users can see it.
The third part is the admin dashboard page, which is needed to manage different sections, apply settings and more. This section is also SPA and fully implemented with Vue. In this section, only users with the Administrator role are allowed, and each one can perform certain tasks based on the permissions given to them. This means, it must be checked whether the user is allowed to see a section or perform a specific task, or not.
Routing is also performed by vue on the user profile and admin dashboard pages, and actions are performed by axios and sending requests to apis written in laravel. And all three parts are in the same application.
So the question is, how should Authentication and Authorization be done in this scenario? Is laravel-passport appropriate for this task? Is it possible to do both in laravel-passport? For example, Authorization is going to be done in the front-end section by vue, how should this be done? Is it possible to combine laravel-passport and laravel-permission - Spatie? Or do we need it at all? Perhaps the more general question: Is this scenario reasonable?
You can use the intermediate approach, blade login/register and the Laravel passport middleware https://laravel.com/docs/6.x/passport#consuming-your-api-with-javascript.
This Passport middleware will attach a laravel_token cookie to your outgoing responses. This cookie contains an encrypted JWT that Passport will use to authenticate API requests from your JavaScript application. Now, you may make requests to your application's API without explicitly passing an access token
Spatie permission is great for managing permissions and roles. You can protect your api routes with middleware or checks in your controller.
You making it too complicated. if you are calling your vue components in blade file you don't need passport at all but if your front end is separate from back end then you need to use passport because you don't have access to session...
On the other hand, I used laravel-permission - Spatie to control permissions and roles. Is it possible for passports to define roles and permissions and control based on them?
Passport doesn't care what kinda permission and role system you want to use you can use anything that you want

How to authenticate users with an integer such as account number as the username

I have a problem authenticating users in my banking application. I want super users to be created and authenticated using email. However, I want that normal users be registered and assigned account numbers. The normal users would then use the account numbers to login. How do you achieve authentication of these different kinds of users with different username fields in one django project.
I have tried overriding the USERNAME_FIELD to account numbers so that normal users would use that to login. It doesnt help to login normal users. For superusers, it prompts for account number when I run
python manage.py createsuperuser
Kindly assist in understanding how to handle this kind of custom authentication.
Django is ready to support your requirement. AUTHENTICATION_BACKENDS support multiple backend as list. For normal user you have to write custom backend and Add that in AUTHENTICATION_BACKENDS.
This link will help you.

Laravel 5.1: Eloquent Authentication Using Two Distinct User Models/Tables

My app needs to support two types of users:
regular users , these are those who are subscribers (restaurants
that use my app for managing their business). For these users, I
have the out of box authentication (Laravel 5.1) set up. email and
password are the fields I authenticate on. I maintain information
about such users in my users table.
guests, these are people who
visit the restaurants above, register to earn loyalty points, check
their score, leave feedback, etc. I maintain information about such
users in my guests table. Authentication, in this case, is simple.
I just use a mobile_number to authenticate them into the app.
I get that I can implement guest's authentication in a subdomain of my app, with different Controllers and Views.
What I don't get is, how can I use the eloquent database driver with the two distinct models? I see that we specify the model eloquent would be using through config.auth.model. So, I'm assuming that we can only have one single model implementing authentication.
Is, what I trying to achieve, possible without implementing a custom driver?
Short answer: No.
You need a custom driver for this. But that shouldn't be too hard to implement, as you can easily get inspiration from/or extend the current EloquentUserProvider. You can also check out the answer to this other question:
Custom user authentication base on the response of an API call
The context is different than yours, but it may help getting a better grasp on the implementation approach (that is if you haven't done this before).

Headstart needed for re-using the register functionality in Laravel 5.0

I am building a Laravel 5.0 project. With this the admin section is only accessible for registered users. I have added for example a user type to the User model and want to create and edit users using an admin module instead of the standard registration form in /auth/register.
But I cannot seem to find how to disable /auth/register for the outside world, but also cannot create my own CRUD for managing users.
So my main two questions are:
How can I reuse the password encryption in my own create and update functionality
How can I disable /auth/register for the not logged in users?
Thanx in advance

Resources