-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redirect the user when logged and to 'r' querystring parameter of login #19
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Zizaco
added a commit
that referenced
this pull request
Feb 19, 2013
Redirect the user when logged and to 'r' querystring parameter of login
Another option instead of adding the redirect field to the form would be to use the Session. public function getIndex()
{
list($user,$redirect) = $this->checkAuthAndRedirect('user');
if($redirect){return $redirect;}
// Show the page
return View::make('site/user/index', compact('user'));
}
public function postLogin()
{
$input = array(
'email' => Input::get( 'email' ), // May be the username too
'password' => Input::get( 'password' ),
'remamber' => Input::get( 'remember' ),
);
// If you wish to only allow login from confirmed users, call logAttempt
// with the second parameter as true.
// logAttempt will check if the 'email' perhaps is the username.
if ( Confide::logAttempt( $input ) )
{
$r = Session::get('loginRedirect');
if (!empty($r))
{
Session::forget('loginRedirect');
return Redirect::to($r);
}
return Redirect::to('user');
}
else
{
// Check if there was too many login attempts
if( Confide::isThrottled( $input ) )
{
$err_msg = Lang::get('confide::confide.alerts.too_many_attempts');
}
else
{
$err_msg = Lang::get('confide::confide.alerts.wrong_credentials');
}
return Redirect::to('user/login')
->withInput(Input::except('password'))
->with( 'overrideNotifications', true )
->with( 'error', $err_msg );
}
}
protected function checkAuthAndRedirect($redirect, $ifValid=false)
{
// Get the user information
$user = Auth::user();
$redirectTo = false;
if(empty($user->id) && ! $ifValid) // Not logged in redirect, set session.
{
Session::put('loginRedirect', $redirect);
$redirectTo = Redirect::to('user/login')
->with( 'notice', Lang::get('user/user.login_first') );
}
elseif(!empty($user->id) && $ifValid) // Valid user, we want to redirect.
{
$redirectTo = Redirect::to($redirect);
}
return array($user, $redirectTo);
} |
@andrew13. That's a better idea! I'm gonna change this soon. |
Also, In my own project I moved checkAuthAndRedirect to the User model so the references are now: class UserController extends BaseController {
public function getIndex()
{
list($user,$redirect) = User::checkAuthAndRedirect('user');
if($redirect){return $redirect;}
// Show the page
return View::make('site/user/index', compact('user'));
}
[...]
public function postLogin()
{
$input = array(
'email' => Input::get( 'email' ), // May be the username too
'password' => Input::get( 'password' ),
'remamber' => Input::get( 'remember' ),
);
// If you wish to only allow login from confirmed users, call logAttempt
// with the second parameter as true.
// logAttempt will check if the 'email' perhaps is the username.
if ( Confide::logAttempt( $input ) )
{
$r = Session::get('loginRedirect');
if (!empty($r))
{
Session::forget('loginRedirect');
return Redirect::to($r);
}
return Redirect::to('user');
}
else
{
// Check if there was too many login attempts
if( Confide::isThrottled( $input ) )
{
$err_msg = Lang::get('confide::confide.alerts.too_many_attempts');
}
else
{
$err_msg = Lang::get('confide::confide.alerts.wrong_credentials');
}
return Redirect::to('user/login')
->withInput(Input::except('password'))
->with( 'overrideNotifications', true )
->with( 'error', $err_msg );
}
}
[...]
}
use Zizaco\Confide\ConfideUser;
use Zizaco\Entrust\HasRole;
use BigElephant\Presenter\PresentableInterface;
class User extends ConfideUser {
use HasRole;
public static function checkAuthAndRedirect($redirect, $ifValid=false)
{
// Get the user information
$user = Auth::user();
$redirectTo = false;
if(empty($user->id) && ! $ifValid) // Not logged in redirect, set session.
{
Session::put('loginRedirect', $redirect);
$redirectTo = Redirect::to('user/login')
->with( 'notice', Lang::get('user/user.login_first') );
}
elseif(!empty($user->id) && $ifValid) // Valid user, we want to redirect.
{
$redirectTo = Redirect::to($redirect);
}
return array($user, $redirectTo);
}
[...]
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See #18 and #16