|
| 1 | +/** |
| 2 | + * AuthKit |
| 3 | + * ----------------------------------------------------------------------------- |
| 4 | + * File: js/modules/forms/redirect.js |
| 5 | + * Author: Michael Erastus |
| 6 | + * Package: AuthKit |
| 7 | + * |
| 8 | + * Description: |
| 9 | + * ----------------------------------------------------------------------------- |
| 10 | + * Redirect handling utilities for the AuthKit AJAX forms module. |
| 11 | + * |
| 12 | + * This file is responsible for resolving redirect behavior from the AuthKit |
| 13 | + * runtime configuration and executing browser navigation when a successful AJAX |
| 14 | + * submission carries redirect intent. |
| 15 | + * |
| 16 | + * Responsibilities: |
| 17 | + * - Resolve the configured post-success redirect behavior. |
| 18 | + * - Resolve the configured fallback redirect URL. |
| 19 | + * - Support both camelCase and snake_case runtime config keys. |
| 20 | + * - Execute browser redirects safely. |
| 21 | + * - Apply redirect intent from normalized successful form results. |
| 22 | + * |
| 23 | + * Design notes: |
| 24 | + * - This file does not perform HTTP requests. |
| 25 | + * - This file does not render UI feedback. |
| 26 | + * - This file does not decide whether a submission succeeded; it only responds |
| 27 | + * to already-normalized success results. |
| 28 | + * - Redirect behavior is intentionally configuration-driven so future UI flows |
| 29 | + * may choose between redirect, none, or custom event-based handling. |
| 30 | + * |
| 31 | + * Supported configuration sources: |
| 32 | + * - context.config.forms.successBehavior |
| 33 | + * - context.config.forms.success_behavior |
| 34 | + * - context.config.forms.ajax.successBehavior |
| 35 | + * - context.config.forms.ajax.success_behavior |
| 36 | + * - context.config.forms.fallbackRedirect |
| 37 | + * - context.config.forms.fallback_redirect |
| 38 | + * - context.config.forms.ajax.fallbackRedirect |
| 39 | + * - context.config.forms.ajax.fallback_redirect |
| 40 | + * |
| 41 | + * Expected normalized success result shape: |
| 42 | + * - { |
| 43 | + * ok: true, |
| 44 | + * redirectUrl: 'https://example.com/dashboard' | null |
| 45 | + * } |
| 46 | + * |
| 47 | + * @package AuthKit |
| 48 | + * @author Michael Erastus |
| 49 | + * @license MIT |
| 50 | + */ |
| 51 | + |
| 52 | +import { dataGet, isString, normalizeString } from '../../core/helpers.js'; |
| 53 | + |
| 54 | + |
| 55 | +/** |
| 56 | + * Resolve the configured success redirect behavior. |
| 57 | + * |
| 58 | + * Resolution order: |
| 59 | + * - context.config.forms.successBehavior |
| 60 | + * - context.config.forms.success_behavior |
| 61 | + * - context.config.forms.ajax.successBehavior |
| 62 | + * - context.config.forms.ajax.success_behavior |
| 63 | + * - redirect fallback |
| 64 | + * |
| 65 | + * Supported values: |
| 66 | + * - redirect |
| 67 | + * - none |
| 68 | + * |
| 69 | + * @param {Object|null} context |
| 70 | + * @returns {string} |
| 71 | + */ |
| 72 | +export function resolveRedirectBehavior(context) { |
| 73 | + return normalizeString( |
| 74 | + dataGet( |
| 75 | + context, |
| 76 | + 'config.forms.successBehavior', |
| 77 | + dataGet( |
| 78 | + context, |
| 79 | + 'config.forms.success_behavior', |
| 80 | + dataGet( |
| 81 | + context, |
| 82 | + 'config.forms.ajax.successBehavior', |
| 83 | + dataGet(context, 'config.forms.ajax.success_behavior', 'redirect') |
| 84 | + ) |
| 85 | + ) |
| 86 | + ), |
| 87 | + 'redirect' |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +/** |
| 93 | + * Resolve the configured fallback redirect URL. |
| 94 | + * |
| 95 | + * Resolution order: |
| 96 | + * - context.config.forms.fallbackRedirect |
| 97 | + * - context.config.forms.fallback_redirect |
| 98 | + * - context.config.forms.ajax.fallbackRedirect |
| 99 | + * - context.config.forms.ajax.fallback_redirect |
| 100 | + * - empty-string fallback |
| 101 | + * |
| 102 | + * @param {Object|null} context |
| 103 | + * @returns {string} |
| 104 | + */ |
| 105 | +export function resolveFallbackRedirect(context) { |
| 106 | + return normalizeString( |
| 107 | + dataGet( |
| 108 | + context, |
| 109 | + 'config.forms.fallbackRedirect', |
| 110 | + dataGet( |
| 111 | + context, |
| 112 | + 'config.forms.fallback_redirect', |
| 113 | + dataGet( |
| 114 | + context, |
| 115 | + 'config.forms.ajax.fallbackRedirect', |
| 116 | + dataGet(context, 'config.forms.ajax.fallback_redirect', '') |
| 117 | + ) |
| 118 | + ) |
| 119 | + ), |
| 120 | + '' |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +/** |
| 126 | + * Perform a browser redirect safely. |
| 127 | + * |
| 128 | + * Returns false when the provided URL is invalid or empty. |
| 129 | + * |
| 130 | + * @param {*} url |
| 131 | + * @returns {boolean} |
| 132 | + */ |
| 133 | +export function performRedirect(url) { |
| 134 | + const normalizedUrl = normalizeString(url, ''); |
| 135 | + |
| 136 | + if (normalizedUrl === '') { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + if (!window || !window.location || typeof window.location.assign !== 'function') { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + window.location.assign(normalizedUrl); |
| 145 | + |
| 146 | + return true; |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +/** |
| 151 | + * Apply redirect behavior for a normalized successful submission result. |
| 152 | + * |
| 153 | + * Rules: |
| 154 | + * - when configured behavior is not "redirect", do nothing |
| 155 | + * - prefer normalizedResult.redirectUrl when present |
| 156 | + * - otherwise fall back to configured fallback redirect |
| 157 | + * |
| 158 | + * @param {Object|null} context |
| 159 | + * @param {Object|null} normalizedResult |
| 160 | + * @returns {boolean} |
| 161 | + */ |
| 162 | +export function handleRedirect(context, normalizedResult) { |
| 163 | + const behavior = resolveRedirectBehavior(context); |
| 164 | + |
| 165 | + if (behavior !== 'redirect') { |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + const redirectUrl = isString(normalizedResult?.redirectUrl) |
| 170 | + ? normalizeString(normalizedResult.redirectUrl, '') |
| 171 | + : ''; |
| 172 | + |
| 173 | + if (redirectUrl !== '') { |
| 174 | + return performRedirect(redirectUrl); |
| 175 | + } |
| 176 | + |
| 177 | + const fallbackRedirect = resolveFallbackRedirect(context); |
| 178 | + |
| 179 | + if (fallbackRedirect !== '') { |
| 180 | + return performRedirect(fallbackRedirect); |
| 181 | + } |
| 182 | + |
| 183 | + return false; |
| 184 | +} |
0 commit comments