简体   繁体   中英

How to configure web routes and api routes with multiple authentication in laravel?

I am using laravel 5.8 and preparing an application with multiple interfaces.

For Example: I have three types of interfaces like.

1) User Interface (end users)

2) Agencies (inter-mediator user between system and end user)

3) Admin panel

all three having different users and authentication.

Database tables: 1) User Interface: users 2) Agencies: agency_users 3) Admin panel: admins

and different models accordingly.

Now, here I should have web interface for all three. and Rest API for User Interface and Agencies where both should have different API authentication.

I had done lot more search for proper fundamentals of API in laravel but not getting similar kind of information.

Please suggest me if anyone have resolved similar type of scenario. Will provide more information if required.

Thanks for your help in advance.

This is one of my own middlewares, it looks if the authenticated user is an admin.

NOTE: be sute to also add your middlware to the HTTP/Kernel.php

class IsAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        if (Auth::user() &&  Auth::user()->admin == 1) {
            return $next($request);
        }

        return redirect('/');
    }
}

After that you can use a RouteGroup in your routes and add your middleware as the middleware of that group

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM