Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

costeeta's avatar

Multiple Authentication in Laravel 5 - phpCAS and Default Auth

Hello, Is it possible to have phpCAS authentication and the default laravel authentication in the same app? I'm building an app where students have to be authenticated by CAS and parents authenticated by an account their students create for them, which will use the default auth that ships with laravel5.

0 likes
28 replies
nfauchelle's avatar

I used the multiauth on a laravel 4.2 site and found it most excellent!

1 like
costeeta's avatar

Thanks so much guys. Unfortunately multiauth hasn't been updated to L5 :(

makos's avatar

I copied ollieread's multiauth solution and with few tweaks I was able to make it work with L5. It works flawlessly so far.

However I am very surprised that such a popular framework, doesn't contain built in option for multiple authentications.

makos's avatar

What I have is basically admin authentication for backend and also customer authetication for customer zone on the frontend. In my AdminServiceProvider.php

public function boot() {
        include __DIR__ . '/routes.php';

        // custom admin auth driver
    // not necessary needed if you use one of database or eloquent base drivers
        $this->app->auth->admin()->extend('geodeticca-admin', function($app){
            $userRepository = $app->make('Geodeticca\Admin\User\UserRepository');
            $accessRepository = $app->make('Geodeticca\Admin\Access\AccessRepository');

            return new Auth\Authenticator($userRepository, $accessRepository);
        });
    }

Auth controller looks like this

class AuthController extends Controller {

    protected $auth;

    public function __construct(){
    // retrieve admin auth provider
        $this->auth = \App::make('auth')->admin();
    }

    public function login(Request $request){
        if ($request->isMethod('post')) {
            if (!$this->auth->attempt($request->only('email', 'password'))){
                return \Redirect::route('admin.auth.index')->withErrors([
                    'error' => 'These credentials do not match our records.',
                ]);
            }
        }

        return \Redirect::route('admin.index');
    }
}

Config in config/auth.php

return [
        'multi' => [
            'customer' => [
                'driver' => 'geodeticca-eshop',
            ],
            'admin' => [
                'driver' => 'geodeticca-admin',
            ],
        ],
];

Rest of the code from github is 99% similar so no need to post it here. If you have any question please ask.

Dubz's avatar

@makos You should submit a pull request for this so other users can install it easily and use it. I tried to install it following the instructions but since I'm using laravel 5 I can't. I don't know how to manually install it to modify it either, so I'm kind of stuck waiting on it. My only other option is to downgrade to version 4.2 and install it, but I'd be more comfortable running the latest version, they tend to have less bugs and have more features.

makos's avatar

@Dubz, I will but Im affraid it wont be untill beginig of april.

Dubz's avatar

@makos Is it that complicated or are you just busy? I'd do it myself but laravel is still confusing to me in some areas so I wouldn't know where to start with it.

I was hoping to be able to use it on a new project I'm working on but I might be able to work around it for now and add it whenever its updated, I just don't want to start working on it now only to have it not work later.

makos's avatar

@Dubz I have to meet couple of deadlines now however in April I should be able to make something useful out of me :)

Dubz's avatar

@makos Understandable, I guess I'll try to work around it for now and work on organizing a good database setup and templates for the site. I can fill in random data for parts of it until I have logins and sessions working.

sahibalejandro's avatar

I'm trying to make multiauth work with L5 but I'm stuck...

@makos can you share your entire package code with me? Just take 5 minutes to zip it and send it to sahib.alejandro on gmail.

I have time to test, tune and make the pull request to ollieread repo, but I need your code.

I'm sure all people here will be grateful.

Dubz's avatar

@sahibalejandro That sounds great, I hope you can figure it out and be able to update it for the community! Like I said I'm new to laravel so I'm not sure exactly how the plugin even works (programmatically) much less how to utilize it. If I had more experience I might try it myself, but I have none so I can't do too much.

Even though I can understand @makos being busy, I don't see how sending the information zipped up is much different than sending a pull request on github, unless he doesn't have an account there or something. Either way I just hope it gets updated soon, looking forward to utilizing it.

ramonackermann's avatar

Check out my fork of the project: https://github.com/sboo/multiauth

It's working, but you have to change the auth middlewares, the Guard injection doesn't work. Here's my working example for Middleware\Authenticate:

<?php namespace App\Http\Middleware;

use Auth;
use Closure;

class Authenticate {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::user()->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('auth/login');
            }
        }

        return $next($request);
    }

Change RedirectIfAuthenticated accordingly.

Also, the default app.blade.php will also cause an error, change:

Auth::guest() to Auth::user()->guest()

Auth::user()->name to Auth::user()->get()->name

Hope that helps.

ramonackermann's avatar

@sahibalejandro I did make a pull request. I'll be working on it next week a bit more. The problem with danielcoimbra's solution is that you can't have 2 different concurrent sessions of different types. That would be an issue for me.

Hopefully Olliread can check and merge it somehow. Otherwise I'll put the fork on packagist.

sahibalejandro's avatar

@ramonackermann Ok, I will not make a pull request to Olliread, and I also ended up with the same conclusion about danielcoimbra's approach.

I'm trying with your code right now.

omar-dev's avatar

I'm using "hacklee/laravel5-multi-auth" package, it is working perfectly, but the password reset does not work ): @sahibalejandro ndro if you are willing to pull request to Olliread you should consider password reset. thanks guys for the help.

samsoft's avatar

Thanks @ramonackermann i was unable to use multi auth in my first project, i'm integrating it with my new project but i'm confused somewhere, i need assistance on this.. in my project, i've Admin and Partner to be Authenticated from different route, my question is, do i need to create Authentication class for each? AdminAuthentication and PartnerAuthentication? @sahibalejandro how do you achieve this?

    'multi' => [
        'admin' => [
            'driver' => 'eloquent',
            'model' => 'App\Admin',
        ],
        'client' => [
            'driver' => 'database',
            'table' => 'partner',
            'email' => 'partner.emails.password',
        ]
    ],

asasmoyo's avatar

hi @ramonackermann, i've installed https://github.com/sboo/multiauth and it works perfectly with database and eloquent driver. but when i use my own auth driver it shows an error,

Driver [my_auth] not supported.

how can i use my own auth driver with this package? thanks

--edited--

somehow i managed to use my own auth driver with https://github.com/sboo/multiauth. all i need to do are extending the MultiManager and AuthManager class then i implemented a method named "createMy_authDriver" on my AuthManager that returns a Guard object which uses my own implementation of UserProvider. thanks :D

sarav_m2h's avatar

Step 1 : Require Composer package

Open your terminal and navigate to your laravel folder. Now run the following command

 composer require sarav/laravel-multiauth dev-master

Step 2 : Replacing default auth service provider

Replace

Illuminate\Auth\AuthServiceProvider::class

with

Sarav\Multiauth\MultiauthServiceProvider

Step 3 : Modify auth.php

Modify auth.php file from the config directory to something like this

'multi' => [
        'user' => [
               'driver' => 'eloquent',
               'model'  => App\User::class,
               'table'  => 'users'
        ],
        'admin' => [
               'driver' => 'eloquent',
               'model'  => App\Admin::class,
               'table'  => 'admins'
         ]
   ],

Note : I have set second user as admin here. Feel free to change yours but don't forget to add its respective driver, model and table.

We are done! Now you can simply login user/admin like the following code

\Auth::loginUsingId("user", 1); // Login user with id 1

\Auth::loginUsingId("admin", 1); // Login user with id 1

// Attempts to login user with email id johndoe@gmail.com 
\Auth::attempt("user", ['email' => 'johndoe@gmail.com', 'password' => 'password']);

// Attempts to login admin with email id johndoe@gmail.com
\Auth::attempt("admin", ['email' => 'johndoe@gmail.com', 'password' => 'password']); 

Simply pass the first parameter as key which you have configured in auth.php to perform authentication for either user or admin.

For more detailed documentation

http://sarav.co/blog/multiple-authentication-in-laravel/

http://sarav.co/blog/multiple-authentication-in-laravel-continued/

Please or to participate in this conversation.