0

In my laravel project , am implementing another login for agencies, which i want to take from agencies table.Agencies table have email and password fields. But when i try to login it not get logged in and redirecting to same page and prducing validation error.But i provided exact email and password from agencie table

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

<?php

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

Xavier Issac
  • 414
  • 1
  • 4
  • 25
  • I'm guessing this is because laravel doesn't know that the password is in the agency_password column. Unless you are sharing the database table with older code I'd suggest changing the names of your columns from agency_email, agency_password, agency_address, etc... to email, password, address, etc.. – kaan_a Apr 21 '20 at 06:37
  • I cahnged the coloumn name, now it is returning login validation error. I provided exact username and password from agency table – Xavier Issac Apr 21 '20 at 07:45
  • Most likely not of any help for your problem, but why not use Laravel's default user login and relate agencies to users? Spares you the hassle of writing another auth implementation. And you can use more than one login for an agency – brombeer Apr 21 '20 at 07:46
  • I am working on exixting project, they have such more logins, so i have to follow same waya – Xavier Issac Apr 21 '20 at 07:48
  • can you copy the exact error message and your login view? Perhaps you didn't change the name attributes of the input boxes in the views – kaan_a Apr 21 '20 at 07:51
  • @Kaan, Now when i tries to login, its returning validation error, username and email not found – Xavier Issac Apr 21 '20 at 08:29
  • Is there is any way to check am getting email and password in Logincontroller – Xavier Issac Apr 21 '20 at 08:30

0 Answers0