vendredi 28 octobre 2016

Why is this protected route refusing logged in users? laravel 5.3

I have a custom guard setup and it's been logging me in, or it was until I protected one of the routes, and now the route is rejecting all users out of hand.

Route::get('agencydash', 'AgencyRegisterController@getDashboard')->name('agencydash')->middleware('auth:agency');

This is the controller part related to login. It has the same response when registering and so on though. I tried emptying the browser cache, and even instituted a logout button:



Goes to

Route::get('logout', 'AgencyRegisterController@logout')->name('logout');

Which runs

public function logout() {
        Auth::guard('agency')->logout();
        return redirect()->route('start');
    }

But no matter what. No cigar. The login gets rejected back to the front page.

public function postSignIn(AgencyFormRequest $request){

    $credentials = array('Username'=>Input::get('Username'),
                     'password'=> Input::get('Password'));
    if (Auth::guard('agency')->attempt($credentials)) {
        return redirect()->route('agencydash');
    }
    return redirect()->back();
}

I know the logging in function works as it does redirect to that route, which promptly triggers the auth route and goes, "Nope buddy."

I added an extra line to the Handler file to send me back to the front page:

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

        return redirect()->guest('/')->withMessage('Gotta be logged in for that mate.');
    }

This is the guard:

'agency' => [
            'driver' => 'session',
            'provider' => 'agencies',
        ],

This is the provider:

'agencies' => [
            'driver' => 'eloquent',
            'model' => App\AgencyLogin::class,
        ],

This is the model:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class AgencyLogin extends Authenticatable
{
    use Notifiable;

    protected $table='ministry';

    protected $fillable = [
        'Username', 'Password',
    ];


    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'Password', 'remember_token',
    ];
}

The correct namespaces have been imported into the controller (it'd break otherwise). Auth is complicated so I'm really making an effort to understand it as this app has a lot of layers of Auth.

I have confirmed that the normal Auth functionality does work. Of course this gets weirder as if I allow the generic middleware Auth lock, it redirects from agencydash to the user home dashboard. Which I'm assuming is part of the Auth middlewhere functionality.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire