Skip to content

Commit

Permalink
Render user forms on the front end in headless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Dec 22, 2024
1 parent 818fea8 commit 0a58fd1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
16 changes: 0 additions & 16 deletions src/config/GeneralConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -1690,8 +1690,6 @@ class GeneralConfig extends BaseConfig
*
* This can be set to `false` to disable front-end login.
*
* Note that this config setting is ignored when <config5:headlessMode> is enabled.
*
* See [[ConfigHelper::localizedValue()]] for a list of supported value types.
*
* ::: code
Expand All @@ -1713,8 +1711,6 @@ class GeneralConfig extends BaseConfig
*
* This can be set to `false` to disable front-end logout.
*
* Note that this config setting is ignored when <config5:headlessMode> is enabled.
*
* See [[ConfigHelper::localizedValue()]] for a list of supported value types.
*
* ::: code
Expand Down Expand Up @@ -2745,8 +2741,6 @@ class GeneralConfig extends BaseConfig
/**
* @var mixed The URI or URL that Craft should use for Set Password forms on the front end.
*
* This setting is ignored when <config5:headlessMode> is enabled, unless it’s set to an absolute URL.
*
* See [[ConfigHelper::localizedValue()]] for a list of supported value types.
*
* ::: tip
Expand Down Expand Up @@ -3311,8 +3305,6 @@ class GeneralConfig extends BaseConfig
/**
* @var mixed The URI or URL that Craft should use for email verification links on the front end.
*
* This setting is ignored when <config5:headlessMode> is enabled, unless it’s set to an absolute URL.
*
* See [[ConfigHelper::localizedValue()]] for a list of supported value types.
*
* ::: code
Expand Down Expand Up @@ -5167,8 +5159,6 @@ public function localeAliases(array $value): self
*
* This can be set to `false` to disable front-end login.
*
* Note that this config setting is ignored when <config5:headlessMode> is enabled.
*
* See [[ConfigHelper::localizedValue()]] for a list of supported value types.
*
* ```php
Expand All @@ -5192,8 +5182,6 @@ public function loginPath(mixed $value): self
*
* This can be set to `false` to disable front-end logout.
*
* Note that this config setting is ignored when <config5:headlessMode> is enabled.
*
* See [[ConfigHelper::localizedValue()]] for a list of supported value types.
*
* ```php
Expand Down Expand Up @@ -6374,8 +6362,6 @@ public function sendPoweredByHeader(bool $value = true): self
/**
* The URI or URL that Craft should use for Set Password forms on the front end.
*
* This setting is ignored when <config5:headlessMode> is enabled, unless it’s set to an absolute URL.
*
* See [[ConfigHelper::localizedValue()]] for a list of supported value types.
*
* ::: tip
Expand Down Expand Up @@ -7018,8 +7004,6 @@ public function verificationCodeDuration(mixed $value): self
/**
* The URI or URL that Craft should use for email verification links on the front end.
*
* This setting is ignored when <config5:headlessMode> is enabled, unless it’s set to an absolute URL.
*
* See [[ConfigHelper::localizedValue()]] for a list of supported value types.
*
* ```php
Expand Down
20 changes: 20 additions & 0 deletions src/controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ public function beforeAction($action): bool
*/
public function actionLogin(): ?Response
{
// Set the default response format to HTML, in case it was set to JSON for headless mode
if (!$this->request->getAcceptsJson()) {
$this->response->format = Response::FORMAT_HTML;
}

if ($this->request->getIsGet()) {
// see if they're already logged in
$user = static::currentUser();
Expand Down Expand Up @@ -577,6 +582,11 @@ public function actionGetElevatedSessionTimeout(): Response
*/
public function actionLogout(): Response
{
// Set the default response format to HTML, in case it was set to JSON for headless mode
if (!$this->request->getAcceptsJson()) {
$this->response->format = Response::FORMAT_HTML;
}

// Passing false here for reasons.
Craft::$app->getUser()->logout(false);

Expand Down Expand Up @@ -791,6 +801,11 @@ public function actionRemovePasswordResetRequirement(): ?Response
*/
public function actionSetPassword(): Response
{
// Set the default response format to HTML, in case it was set to JSON for headless mode
if (!$this->request->getAcceptsJson()) {
$this->response->format = Response::FORMAT_HTML;
}

// Have they just submitted a password, or are we just displaying the page?
if (!$this->request->getIsPost()) {
if (!is_array($info = $this->_processTokenRequest())) {
Expand Down Expand Up @@ -886,6 +901,11 @@ public function actionSetPassword(): Response
*/
public function actionVerifyEmail(): Response
{
// Set the default response format to HTML, in case it was set to JSON for headless mode
if (!$this->request->getAcceptsJson()) {
$this->response->format = Response::FORMAT_HTML;
}

if (!is_array($info = $this->_processTokenRequest())) {
return $info;
}
Expand Down
5 changes: 1 addition & 4 deletions src/web/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1741,10 +1741,7 @@ private function _checkIfActionRequestInternal(bool $checkSpecialPaths): bool
}

// Special path?
if (
$checkSpecialPaths &&
($this->_isCpRequest || !$this->generalConfig->headlessMode)
) {
if ($checkSpecialPaths) {
$specialPaths = [
[
$this->_isCpRequest ? self::CP_PATH_LOGIN : $this->generalConfig->getLoginPath(),
Expand Down

0 comments on commit 0a58fd1

Please sign in to comment.