Skip to content

Commit

Permalink
Implement redirect.onAlreadyLoggedIn service (fixes #680)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweissman committed Aug 20, 2017
1 parent 2b174f6 commit 5d6af4a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## v4.1.10-alpha
- Add support for PHP7 runtime errors to be handled in the same way as Exceptions
- Implement NotFoundExceptionHandler and pass through all NotFoundExceptions to this handler.
- Implement `redirect.onAlreadyLoggedIn` service (fixes #680)
- Deprecate `determineRedirectOnLogin` and replace with `redirect.onLogin` service
- Fix some PSR-2 compliance issues

## v4.1.9-alpha
Expand Down
27 changes: 12 additions & 15 deletions app/sprinkles/account/src/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,9 @@ public function login($request, $response, $args)
$ms->addMessageTranslated('success', 'WELCOME', $currentUser->export());

// Set redirect, if relevant
$determineRedirectOnLogin = $this->ci->determineRedirectOnLogin;
$response = $determineRedirectOnLogin($response);
$redirectOnLogin = $this->ci->get('redirect.onLogin');

return $response->withStatus(200);
return $redirectOnLogin($request, $response, $args);
}

/**
Expand Down Expand Up @@ -424,10 +423,11 @@ public function pageRegister($request, $response, $args)
/** @var UserFrosting\Sprinkle\Account\Authenticate\Authenticator $authenticator */
$authenticator = $this->ci->authenticator;

// Forward to dashboard if user is already logged in
// TODO: forward to user's landing page or last visited page
// Redirect if user is already logged in
if ($authenticator->check()) {
return $response->withRedirect($this->ci->router->pathFor('dashboard'), 302);
$redirect = $this->ci->get('redirect.onAlreadyLoggedIn');

return $redirect($request, $response, $args);
}

// Load validation rules
Expand Down Expand Up @@ -577,10 +577,11 @@ public function pageSignIn($request, $response, $args)
/** @var UserFrosting\Sprinkle\Account\Authenticate\Authenticator $authenticator */
$authenticator = $this->ci->authenticator;

// Forward to dashboard if user is already logged in
// TODO: forward to user's landing page or last visited page
// Redirect if user is already logged in
if ($authenticator->check()) {
return $response->withRedirect($this->ci->router->pathFor('dashboard'), 302);
$redirect = $this->ci->get('redirect.onAlreadyLoggedIn');

return $redirect($request, $response, $args);
}

// Load validation rules
Expand Down Expand Up @@ -814,7 +815,7 @@ public function register(Request $request, Response $response, $args)
Capsule::transaction( function() use ($classMapper, $data, $ms, $config, $throttler) {
// Log throttleable event
$throttler->logEvent('registration_attempt');

// Create the user
$user = $classMapper->createInstance('user', $data);

Expand Down Expand Up @@ -1003,14 +1004,10 @@ public function setPassword(Request $request, Response $response, $args)

$ms->addMessageTranslated('success', 'PASSWORD.UPDATED');

// Log out any existing user, and create a new session

/** @var UserFrosting\Sprinkle\Account\Database\Models\User $currentUser */
$currentUser = $this->ci->currentUser;

/** @var UserFrosting\Sprinkle\Account\Authenticate\Authenticator $authenticator */
$authenticator = $this->ci->authenticator;

// Log out any existing user, and create a new session
if ($authenticator->check()) {
$authenticator->logout();
}
Expand Down
43 changes: 40 additions & 3 deletions app/sprinkles/account/src/ServicesProvider/ServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator;
use UserFrosting\Sprinkle\Account\Authenticate\AuthGuard;
use UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager;
Expand Down Expand Up @@ -331,12 +333,47 @@ public function register($container)
return $authenticator->user();
};

/**
* Returns a callback that forwards to dashboard if user is already logged in.
*/
$container['redirect.onAlreadyLoggedIn'] = function ($c) {
/**
* This method is invoked when a user attempts to perform certain public actions when they are already logged in.
*
* @todo Forward to user's landing page or last visited page
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param array $args
* @return \Psr\Http\Message\ResponseInterface
*/
return function (Request $request, Response $response, array $args) use ($c) {
$redirect = $c->router->pathFor('dashboard');

return $response->withRedirect($redirect, 302);
};
};

/**
* Returns a callback that handles setting the `UF-Redirect` header after a successful login.
*/
$container['determineRedirectOnLogin'] = function ($c) {
return function ($response) use ($c)
{
$container['redirect.onLogin'] = function ($c) {
/**
* This method is invoked when a user completes the login process.
*
* Returns a callback that handles setting the `UF-Redirect` header after a successful login.
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param array $args
* @return \Psr\Http\Message\ResponseInterface
*/
return function (Request $request, Response $response, array $args) use ($c) {
// Backwards compatibility for the deprecated determineRedirectOnLogin service
if ($c->has('determineRedirectOnLogin')) {
$determineRedirectOnLogin = $c->determineRedirectOnLogin;

return $determineRedirectOnLogin($response)->withStatus(200);
}

/** @var UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $c->authorizer;

Expand Down
25 changes: 22 additions & 3 deletions app/sprinkles/admin/src/ServicesProvider/ServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
namespace UserFrosting\Sprinkle\Admin\ServicesProvider;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator;
use UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager;
use UserFrosting\Sprinkle\Core\Facades\Debug;
Expand Down Expand Up @@ -43,10 +45,27 @@ public function register($container)

/**
* Returns a callback that handles setting the `UF-Redirect` header after a successful login.
*
* Overrides the service definition in the account Sprinkle.
*/
$container['determineRedirectOnLogin'] = function ($c) {
return function ($response) use ($c)
{
$container['redirect.onLogin'] = function ($c) {
/**
* This method is invoked when a user completes the login process.
*
* Returns a callback that handles setting the `UF-Redirect` header after a successful login.
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param array $args
* @return \Psr\Http\Message\ResponseInterface
*/
return function (Request $request, Response $response, array $args) use ($c) {
// Backwards compatibility for the deprecated determineRedirectOnLogin service
if ($c->has('determineRedirectOnLogin')) {
$determineRedirectOnLogin = $c->determineRedirectOnLogin;

return $determineRedirectOnLogin($response)->withStatus(200);
}

/** @var UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $c->authorizer;

Expand Down

0 comments on commit 5d6af4a

Please sign in to comment.