我无法理解Sentry 2的登录实施。我的意思是在哨兵这是相当紧张的前进。提供从输入到Sentry::login()
方法的用户名/电子邮件和密码,但他们现在更改了它,这真的很混乱。
首先,他们删除了没用的用户名列。
其次,登录方法现在需要使用用户的id检索一个User对象,这再次没有意义,因为你不知道用户id,除非你做了另一个查询,所以它们真的很复杂。
我的代码:
public function login()
{
// Deny access to already logged-in user
if(!Sentry::check())
{
$rules = array(
'username' => 'required|unique:users',
'password' => 'required' );
$validator = Validator::make(Input::all(), $rules);
if($validator->fails())
{
Session::flash('error', $validator->errors());
return Redirect::to('/');
}
$fetch = User::where('username', '=', trim(Input::get('username')));
$user = Sentry::getUserProvider()->findById($fetch->id);
if(!Sentry::login($user, false))
{
Session::flash('error', 'Wrong Username or Password !');
}
return Redirect::to('/');
}
return Redirect::to('/');
}
我尝试使用这种方法,但它引发了一个异常:尽管id是表的一部分,但id是未知的,而User模型只是一个带有$ table ='users'的类声明;属性。
我在这里做错了什么或不理解。
答案 0 :(得分:17)
以下代码是我使用Sentry 2的登录方法。我基本上让Sentry为我做一切验证,找到用户,当然还有登录用户。消息是葡萄牙语,但如果您需要我翻译,请告诉。
public function login()
{
try
{
$credentials = array(
'email' => Input::has('email') ? Input::get('email') : null,
'password' => Input::has('password') ? Input::get('password') : null,
);
// Log the user in
$user = Sentry::authenticate($credentials, Input::has('remember_me') and Input::get('remember_me') == 'checked');
return View::make('site.common.message')
->with('title','Seja bem-vindo!')
->with('message','Você efetuou login com sucesso em nossa loja.');
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
return View::make('site.common.message')
->with('title','Erro')
->with('message','O campo do e-mail é necessário.');
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
return View::make('site.common.message')
->with('title','Erro')
->with('message','O campo do senha é necessário.');
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
$user = Sentry::getUserProvider()->findByLogin(Input::get('email'));
Email::queue($user, 'site.users.emailActivation', 'Ativação da sua conta na Vevey');
return View::make('site.common.message')
->with('title','Usuário não ativado')
->with('message',"O seu usuário ainda não foi ativado na nossa loja. Um novo e-mail de ativação foi enviado para $user->email, por favor verifique a sua caixa postal e clique no link que enviamos na mensagem. Verifique também se os nossos e-mails não estão indo direto para a sua caixa de SPAM.");
}
catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
{
return View::make('site.common.message')
->with('title','Erro')
->with('message','A senha fornecida para este e-mail é inválida.');
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
return View::make('site.common.message')
->with('title','Erro')
->with('message','Não existe usuário cadastrado com este e-mail em nossa loja.');
}
// Following is only needed if throttle is enabled
catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
$time = $throttle->getSuspensionTime();
return View::make('site.common.message')
->with('title','Erro')
->with('message',"Este usário está suspenso por [$time] minutes. Aguarde e tente novamente mais tarde.");
}
catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
{
return View::make('site.common.message')
->with('title','Erro')
->with('message',"Este usário está banido do nossa loja.");
}
}
答案 1 :(得分:2)
我想分享我对Sentry 2 Auth路线的看法。这就是我现在在所有项目中使用的内容。 “警报”课程来自我最近发现的this package。我用它将它传递给MessageBag,但我喜欢它有多干净。
class AuthController extends BaseController {
public function login()
{
try
{
// Set login credentials
$credentials = array(
'email' => Input::get('email') ?: null,
'password' => Input::get('password') ?: null
);
// Authenticate our user and log them in
$user = Sentry::authenticate($credentials, Input::get('remember_me') ?: false);
// Tell them what a great job they did logging in.
Alert::success(trans('success/authorize.login.successful'))->flash();
// Send them where they wanted to go
return Redirect::intended('/');
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
Alert::error(trans('errors/authorize.login.required'))->flash();
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
Alert::error(trans('errors/authorize.login.password.required'))->flash();
}
catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
{
Alert::error(trans('errors/authorize.login.password.wrong'))->flash();
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
Alert::error(trans('errors/authorize.login.user.found'))->flash();
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
Alert::error(trans('errors/authorize.login.user.activated'))->flash();
}
// The following is only required if throttle is enabled
catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
Alert::error(trans('errors/authorize.login.user.suspended'))->flash();
}
catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
{
Alert::error(trans('errors/authorize.login.user.banned'))->flash();
}
return Redirect::back()->withInput(Input::except('password'));
}
public function logout()
{
Sentry::logout();
Alert::success(trans('success/authorize.logout.successful'))->flash();
return Redirect::to('/');
}
}
答案 2 :(得分:0)
您需要调用父类构造函数来继承其功能。在这种情况下,不调用MainController构造函数,因此检查失败。