Skip to content

Commit

Permalink
Support new success_url and error_url shortcode params
Browse files Browse the repository at this point in the history
  • Loading branch information
kodinkat committed Oct 9, 2024
1 parent acc664e commit 00b8d89
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
14 changes: 5 additions & 9 deletions dt-login/login-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,25 @@ public function login( WP_REST_Request $request ) {
}
//phpcs:enable

$error_response = null;
$user_manager = new DT_Login_User_Manager( $payload );

try {
$response = $user_manager->login();
} catch ( \Throwable $th ) {
$error_response = new WP_Error( $th->getCode(), $th->getMessage(), [ 'status' => 401 ] );
return new WP_Error( $th->getCode(), $th->getMessage(), [ 'status' => 401 ] );
}

if ( is_wp_error( $response ) ) {
$error_response = $response;
return $response;
}
if ( !$response ) {
$error_response = new WP_Error( 'login_error', 'Something went wrong with the login', [ 'status' => 401 ] );
return new WP_Error( 'login_error', 'Something went wrong with the login', [ 'status' => 401 ] );
}

// Provide opportunity to update response packet.
$rest_response = apply_filters( 'dt_sso_login_response', [
return new WP_REST_Response( [
'status' => 200,
'body' => $response,
], $payload, $error_response );

return !is_null( $error_response ) ? $error_response : new WP_REST_Response( $rest_response );
] );
}

/**
Expand Down
15 changes: 11 additions & 4 deletions dt-login/login-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ function dt_firebase_login_ui( $atts ) {
$atts = shortcode_atts( [
'lang_code' => $default_lang,
'redirect_to' => $default_redirect_to,
'success_url' => ( !empty( $atts['success_url'] ) ? $atts['success_url'] : null ),
'error_url' => ( !empty( $atts['error_url'] ) ? $atts['error_url'] : null )
], $atts );

$lang_code = $atts['lang_code'];
$redirect_to = $atts['redirect_to'];

$firebase_langs = dt_login_firebase_supported_languages();


if ( !in_array( $lang_code, $firebase_langs ) ) {
if ( strpos( $lang_code, '_' ) !== false ) {
$lang_code = explode( '_', $lang_code )[0];
Expand All @@ -47,6 +48,8 @@ function dt_firebase_login_ui( $atts ) {
$config['tos_url'] = DT_Login_Fields::get( 'tos_url' ) ? DT_Login_Fields::get( 'tos_url' ) : '';
$config['privacy_url'] = DT_Login_Fields::get( 'privacy_url' ) ? DT_Login_Fields::get( 'privacy_url' ) : '';
$config['disable_sign_up_status'] = !dt_can_users_register();
$config['success_url'] = ( !empty( $atts['success_url'] ) ? $atts['success_url'] : null );
$config['error_url'] = ( !empty( $atts['error_url'] ) ? $atts['error_url'] : null );

$sign_in_options = [];
$sign_in_options['google'] = DT_Login_Fields::get( 'identity_providers_google' ) === 'on' ? true : false;
Expand Down Expand Up @@ -173,7 +176,7 @@ function signInSuccessWithAuthResult(authResult, redirectUrl) {
const response = JSON.parse(json)

if ( response.status === 200 ) {
const { login_method, jwt, redirect_url } = response.body
const { login_method, jwt } = response.body

if ( login_method === 'mobile' ) {
if ( !Object.prototype.hasOwnProperty.call( jwt, 'token' ) ) {
Expand All @@ -186,8 +189,12 @@ function signInSuccessWithAuthResult(authResult, redirectUrl) {
localStorage.setItem( 'login_method', 'mobile' )
}

// Support last-ditch custom url redirects.
window.location = ( ( redirect_url ) ? redirect_url : config.redirect_url );
// Redirect based on specified settings or override urls.
if ( config['success_url'] ) {
window.location = config['success_url'];
} else {
window.location = config.redirect_url;
}
} else {
showLoader(false)
showErrorMessage(response.message)
Expand Down

0 comments on commit 00b8d89

Please sign in to comment.