| | |
| | | */ |
| | | public function sendMagicLink(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $data = $request->get_json_params(); |
| | | |
| | | // Verify Turnstile |
| | | if (!$this->verifyTurnstile($data['cf-turnstile-response'] ?? '')) { |
| | | return $this->error('Security verification failed', 'turnstile_failed', 403); |
| | | } |
| | | $email = sanitize_email($request->get_param('email')??$request->get_param('user_email')??''); |
| | | $type = sanitize_text_field($request->get_param('type')); |
| | | $type = sanitize_text_field($request->get_param('type')) ?? MagicLinkManager::TYPE_LOGIN; |
| | | $context = $request->get_param('context') ?? []; |
| | | |
| | | error_log('SendMagicLink request: '.print_r($email, true)); |
| | |
| | | |
| | | // Check if email exists |
| | | $exists = email_exists($email); |
| | | if (!$exists) { |
| | | |
| | | if ($type === MagicLinkManager::TYPE_LOGIN && !$exists) { |
| | | return new WP_REST_Response([ |
| | | 'success' => false, |
| | | 'message' => 'User account not found' |
| | | ], 400); |
| | | 'success' => true, |
| | | 'message' => 'Invalid email address' |
| | | ]); |
| | | } |
| | | |
| | | if ($type === MagicLinkManager::TYPE_SIGNUP && $exists) { |
| | | // Redirect to login instead |
| | | $type = MagicLinkManager::TYPE_LOGIN; |
| | | } |
| | | |
| | | // Send the magic link |