Git Product home page Git Product logo

Comments (8)

ankurk91 avatar ankurk91 commented on July 27, 2024 2

@han-huang

Add these lines to your unauthenticated function in app/Exceptions/Handler.php

// This is a workaround
        if ($request->is('admin/*')) {
            return redirect()->guest('/admin/login');
        }

Above lines should come before return redirect()->guest('/login');

from multi-auth.

han-huang avatar han-huang commented on July 27, 2024 1

Difference between 'middleware' => ['web', 'admin', 'auth:admin'] and 'middleware' => ['web', 'admin']

Add Exception to Debug

  • app/Exceptions/Handler.php
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

+        throw new \Exception("forDebug", 1);
        return redirect()->guest('login');
    }
  • app/Http/Middleware/RedirectIfNotAdmin.php
    public function handle($request, Closure $next, $guard = 'admin')
    {
        if (!Auth::guard($guard)->check()) {
+            throw new \Exception("forDebug", 1); 
            return redirect('admin/login');
        }

        return $next($request);
    }

Compare information of debug, find out the division of runtime

  • 'middleware' => ['web', 'admin', 'auth:admin'] :

    • at Pipeline->handleException(object(Request), object(AuthenticationException)) in Pipeline.php line 35
  • 'middleware' => ['web', 'admin'] :

    • at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in Pipeline.php line 33
  • vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php
    protected function getSlice()
    {
        return function ($stack, $pipe) {
            return function ($passable) use ($stack, $pipe) {
                try {
                    $slice = parent::getSlice();
                    $callable = $slice($stack, $pipe);

                    return $callable($passable);  //line 33
                } catch (Exception $e) {
                    return $this->handleException($passable, $e);  //line 35
                } catch (Throwable $e) {
                    return $this->handleException($passable, new FatalThrowableError($e));
                }
            };
        };
    }

from multi-auth.

han-huang avatar han-huang commented on July 27, 2024 1

@ankurk91

I think it is ok.
https://laravel.com/docs/5.3/helpers#method-auth

Or you can use Auth::guard('admin')->user()->email .

from multi-auth.

chamamme avatar chamamme commented on July 27, 2024 1

In other not for your changes to interfere with other packages such as Bouncer its will be better to edit you your Authenticate class in app/Http/Middleware to this

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        if ($request->is('admin/*')) {
            return route('admin.login');
        }
        return route('login');
    }
}

Thanks you !

from multi-auth.

 avatar commented on July 27, 2024

I'm using both the native Laravel auth and hesto's multi-auth. Would this work for me?

from multi-auth.

han-huang avatar han-huang commented on July 27, 2024

@ankurk91

I want to explain my opinion .
It should execute redirect('admin/login') in app/Http/Middleware/RedirectIfNotAdmin.php, but it doesn't.
I think it would be better to fix in multi-auth:install command , not to add workround.
Thanks.

app/Http/Middleware/RedirectIfNotAdmin.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfNotAdmin
{
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @param  string|null  $guard
         * @return mixed
         */
        public function handle($request, Closure $next, $guard = 'admin')
        {
            if (!Auth::guard($guard)->check()) {
                return redirect('admin/login');
            }

            return $next($request);
        }
}

from multi-auth.

ankurk91 avatar ankurk91 commented on July 27, 2024

@han-huang
I manually applied your patch into my application and it worked.
No workaround required.
Now i can access admin user like this -

# get logged-in user email
auth('admin')->user()->email

# check if admin use is logged-in
auth('admin')->check()

Don't know if this is correct way.

@Hesto
Is it possible to remove auth:admin from middle-ware array?
This change was introduced in v1.0.6.
Is there any caveats ?

PS: This issue is related to #29

from multi-auth.

AdmiinX avatar AdmiinX commented on July 27, 2024

this fix worked for me
removed auth:admin from middleware array
app\Providers\RouteServiceProvider.php

Route::group([
    'middleware' => ['web', 'admin'],
    'prefix' => 'admin',
    'as' => 'admin.',
    'namespace' => $this->namespace,
], function ($router) {
    require base_path('routes/admin.php');
});

changed the middleware admin to handle authentication and redirect
app/Http/Middleware/RedirectIfNotAdmin.php

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Closure;

class RedirectIfNotAdmin extends Middleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string[]  ...$guards
     * @return mixed
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    public function handle($request, Closure $next, ...$guards)
    {
        $this->authenticate($request, ['admin']);
        return $next($request);
    }

   /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        if (!$request->expectsJson()) {
            return route('admin.login');
        }
    }
}

so any route use the middleware admin will be authenticate with the admin guard and if failed will be redirect to route('admin.login')
for more info check Authenticate class

from multi-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.