Processing Login with Sentry 2

I find it difficult to understand the Sentry 2 implementation for login. I mean, in Sentry, it was pretty complicated. Provide the username / email address and password from the input method to the Sentry::login() method, however they changed it now and it is really confusing.

First of all, they deleted the user column, which does not make sense.
Secondly, the login method now accepts the User object that you need to get using the user ID, which again does not make sense, since you do not know the user ID unless you make another request, so that they really complicate everything.

My code is:

 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('/'); } 

I tried to use this approach, but it throws an exception: this identifier is unknown, despite the fact that the identifier is part of the table, and the User model is nothing more than a class declaration using $ table = 'users'; attribute.

What am I doing wrong here or do not understand.

+4
source share
3 answers

The code below is my login method using Sentry 2. I basically let Sentry do everything to verify, find the user, and, of course, log in to the user. Messages are in Portuguese, but if you need to translate, just let me know.

 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."); } } 
+17
source

I would like to share my take on the Sentry 2 Auth routes. This is what I use now in all my projects. The "Alert" class is this package I recently found. I use to pass it to MessageBag, but I like how clean it is.

 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
source

You need to call the constructor of the parent class to inherit its functionality. In this case, the MainController constructor is not called, and therefore, the check is not performed.

0
source

Source: https://habr.com/ru/post/1483722/


All Articles