mardi 21 avril 2020

Laravel multi table authentication is not logging in

In my laravel project , am implementing another login for agencies, which i want to take from agencies table.Agencies table have agency_email and agency_password fields. But when i try to login it not get logged in and redirecting to same page.

Following is my code in model

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Kyslik\ColumnSortable\Sortable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Agencie extends Authenticatable
{
    use Notifiable;
    use Sortable;

    protected $guard = 'agencie';
    protected $table = 'agencies';
    protected $primaryKey = 'agency_id';    

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'agency_id', 'agency_name','agency_user_name' ,'agency_password', 'agency_city', 'agency_state', 'agency_zip', 'agency_email','status','created_at','updated_at'
    ];

}

Following i have addeed to config/auth.php

'guards' => [
        'agencie' => [
            'driver' => 'session',
            'provider' => 'agencies',
        ],   
    ],
'providers' => [
        'agencies' => [
            'driver' => 'eloquent',
            'model' => App\Agencie::class,
        ],
    ],

Following is my code in LoginController

<?php

namespace App\Http\Controllers\Agency\AgencyAuth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Hesto\MultiAuth\Traits\LogsoutGuard;
use JsValidator;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers, LogsoutGuard {
        LogsoutGuard::logout insteadof AuthenticatesUsers;
    }


    protected $validationRules = [
                                        'email' => 'required|email',
                                        'password' => 'required'
                                    ];

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    // public $redirectTo = '/user/home';
    public $redirectTo = '/user/dashboard-graph';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

        $this->middleware('guest:agencie', ['except' => 'logout']);

       // $this->middleware('guest:agency')->except('logout');
    }

    public function username()
    {
    return 'agency_email';
    }

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        $validator = JsValidator::make($this->validationRules,[],[],'#loginform');
        return view('agency.auth.login')->with('validator', $validator);
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('agencie');
    }
    /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */   
}

I have added following codes to app/Expecations/Handler.php

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

        $middleware = request()->route()->gatherMiddleware();
        $guard = config('auth.defaults.guard');
        foreach($middleware as $m) {
            if(preg_match("/auth:/",$m)) {
                list($mid, $guard) = explode(":",$m);
            }
        }

        switch($guard) {
            case 'agencie':
                $login = 'agency/login';
                break;
            default:
                $login = 'user/login';
                break;
        }

        return redirect()->guest($login);
    }

I have also added additonal 2 files in middleware named RedirectifAgency.php & Redirectifnotagency.php

Following is the code

namespace App\Http\Middleware;

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

class RedirectIfAgency
{
    /**
     * 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 = 'agencie')
    {
        if (Auth::guard($guard)->check()) {
            return redirect('agency/home');
        }

        return $next($request);
    }
}

What is the problem here.Please help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire