在Laravel 5.2

时间:2017-04-05 06:16:37

标签: php laravel laravel-5 login-attempts

当我尝试登录时,以下操作不起作用:

Auth::guard('pia')->attempt(['email' => $_POST['email'],
                             'password'=>$_POST['password']])

如果我删除guard('pia')然后它正在运行,但有时它redirects默认为Laravel login page,尽管用户已登录,我不想要。

提供route的代码,auth.phpAuthController.php

我为不同类型的用户使用不同的AuthControllers,他们的数据库表也是分开的,例如piapia,管理员表管理员等。

routes.php

    Route::group(['prefix' => 'pia'], function() {

        //Login Routes...
        Route::get('login','Pia\AuthController@showLoginForm');
        Route::post('login','Pia\AuthController@login');
        Route::get('logout','Pia\AuthController@logout');

        // Registration Routes...
        Route::get('register', 'Pia\AuthController@showRegistrationForm');
        Route::post('register', 'Pia\AuthController@register');
    });

    config/auth.php

    'pia' => [ 
               'driver' => 'session',
                'provider' => 'pia',
             ],
  <?php

namespace App\Http\Controllers\Pia;

use App\Pia;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use DB;
use Mail;
use Auth;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */

    protected $guard = 'pia';
    protected $redirectTo = '/pia/dashboard';
    protected $redirectAfterLogout = '/pia/login';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    public function showLoginForm(){

        if(view()->exists('auth.authenticate')){
            return view('auth.authenticate');
        }
        return view('pia.auth.login');
    }

    public function showRegistrationForm(){

        return view('pia.auth.register');
    }

    public function login(Request $request)
    {   

        if (Auth::guard('pia')->attempt(['email' => $_POST['email'],'password'=>$_POST['password']])) {
            return redirect('/pia/dashboard');
        }else{
             return redirect('/pia/login')->with('warning', 'Problem, try again!'); //
        }
    }
...

}

0 个答案:

没有答案