Skip to content

Commit 535da06

Browse files
committed
Fix problems with expired auth_token
1 parent 4f6fdf3 commit 535da06

File tree

4 files changed

+70
-27
lines changed

4 files changed

+70
-27
lines changed

assets/css/wpcable.css

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ img.round {
4343
font-size: 12px;
4444
}
4545

46+
.button.danger:before {
47+
font-family: dashicons;
48+
content: '\f534 ';
49+
font-size: 20px;
50+
float: left;
51+
margin-right: 6px;
52+
}
53+
4654
/**************/
4755
/* Main boxes */
4856
/**************/
@@ -65,7 +73,6 @@ img.round {
6573
z-index: -1;
6674
bottom: 0;
6775
content: "";
68-
/*box-shadow: 0 8px 30px rgba(0,0,0,.3)*/
6976
}
7077

7178
.whitebox {
@@ -302,16 +309,6 @@ img.round {
302309
.date-filters .section {
303310
flex-basis: 23%;
304311
}
305-
306-
.date-filters label,
307-
.date-filters input[type="text"] {
308-
309-
}
310-
311-
.date-filters button {
312-
313-
}
314-
315312
}
316313

317314
.compareto {

classes/api_calls.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ private function get_curl( $url, $args = [], $method = 'post', $headers = '' ) {
255255
);
256256
}
257257

258+
if ( is_array( $res ) && ! empty( $res['errors'] ) ) {
259+
if ( false !== array_search( 'Invalid login credentials', $res['errors'], true ) ) {
260+
// The auth_token expired or login failed: Clear the token!
261+
// Next time the user visits the settings page, they need to login again.
262+
codeable_api_logout();
263+
return false;
264+
}
265+
}
266+
258267
return $res;
259268
}
260269

functions/admin-settings.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,22 @@ function wpcable_options() {
4141
* @return void
4242
*/
4343
function codeable_load_settings_page() {
44-
$nonce = false;
44+
$nonce = false;
45+
$action = false;
4546

46-
if ( ! empty( $_REQUEST['_wpnonce'] ) ) {
47-
$nonce = wp_unslash( $_REQUEST['_wpnonce'] );
47+
if ( empty( $_REQUEST['_wpnonce'] ) ) {
48+
return;
4849
}
50+
if ( empty( $_REQUEST['action'] ) ) {
51+
return;
52+
}
53+
54+
$nonce = wp_unslash( $_REQUEST['_wpnonce'] );
55+
$action = wp_unslash( $_REQUEST['action'] );
4956

50-
if ( $nonce && wp_verify_nonce( $nonce, 'logout' ) ) {
57+
if ( 'logout' === $action && wp_verify_nonce( $nonce, $action ) ) {
58+
codeable_api_logout();
59+
} elseif ( 'flush_data' === $action && wp_verify_nonce( $nonce, $action ) ) {
5160
codeable_flush_all_data();
5261
}
5362
}
@@ -111,16 +120,21 @@ function codeable_settings_callback() {
111120
add_query_arg( 'action', 'logout' ),
112121
'logout'
113122
);
114-
$logout_warning = __( 'When logging out, all your data (including task notes or color flags) is deleted from the DB. This cannot be undone.\n\nDo you want to clear your data and log out?', 'wpcable' );
123+
124+
$flush_data_url = wp_nonce_url(
125+
add_query_arg( 'action', 'flush_data' ),
126+
'flush_data'
127+
);
128+
129+
$flush_data_warning = __( 'All your data is deleted from the DB, including your private task notes or color flags. This cannot be undone.\n\nDo you want to clear your data and log out?', 'wpcable' );
130+
131+
// Hacky way to save settings without a second redirect...
132+
$_SERVER['REQUEST_URI'] = remove_query_arg( [ 'action', '_wpnonce', 'success', 'error' ] );
133+
115134
?>
116135
<div class="wrap wpcable_wrap">
117136
<form method="post" action="options.php">
118-
<?php settings_fields( 'wpcable_group', '_wpnonce', false ); ?>
119-
<input
120-
type="hidden"
121-
name="_wp_http_referer"
122-
value="<?php echo remove_query_arg( ['success', 'error' ] ); ?>"
123-
/>
137+
<?php settings_fields( 'wpcable_group' ); ?>
124138
<?php do_settings_sections( 'wpcable_group' ); ?>
125139

126140
<h2><?php esc_html_e( 'Task list', 'wpcable' ); ?></h2>
@@ -237,9 +251,11 @@ function codeable_settings_callback() {
237251
'<b>' . $wpcable_email . '</b>'
238252
);
239253
?>
254+
<input type="hidden" name="wpcable_email" value="<?php echo esc_attr( $wpcable_email ); ?>" />
240255
</p>
241256
<p>
242-
<a href="<?php echo esc_url( $logout_url ); ?>" class="button" onclick="return confirm('<?php echo esc_attr( $logout_warning ); ?>')"><?php esc_html_e( 'Log out and clear all data', 'wpcable' ); ?></a>
257+
<a href="<?php echo esc_url( $logout_url ); ?>" class="button" ><?php esc_html_e( 'Log out', 'wpcable' ); ?></a>
258+
<a href="<?php echo esc_url( $flush_data_url ); ?>" class="button danger" onclick="return confirm('<?php echo esc_attr( $flush_data_warning ); ?>')"><?php esc_html_e( 'Clear all data', 'wpcable' ); ?></a>
243259
</p>
244260
</td>
245261
</tr>

functions/helpers.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ function codeable_page_requires_login( $page_title ) {
211211
exit;
212212
}
213213

214+
/**
215+
* Simply expires the current auth_token, so the API sync does not work anymore.
216+
* The user will see the login form again when he visits the settings page the next
217+
* time.
218+
*
219+
* @return void
220+
*/
221+
function codeable_api_logout() {
222+
delete_option( 'wpcable_auth_token' );
223+
224+
// Flush object cache.
225+
wpcable_cache::flush();
226+
}
227+
214228
/**
215229
* Flushes all locally stored data and forgets the authentication token.
216230
*
@@ -250,14 +264,18 @@ function codeable_flush_all_data() {
250264
}
251265
}
252266

253-
delete_option( 'wpcable_auth_token' );
267+
// Flush object cache.
268+
wpcable_cache::flush();
269+
254270
delete_option( 'wpcable_email' );
255271
delete_option( 'wpcable_average' );
256272
delete_option( 'wpcable_balance' );
257273
delete_option( 'wpcable_revenue' );
258274
delete_option( 'wpcable_last_fetch' );
259275
delete_option( 'wpcable_account_details' );
260276

277+
codeable_api_logout();
278+
261279
$redirect_to = codeable_add_message_param(
262280
'success',
263281
sprintf(
@@ -267,9 +285,6 @@ function codeable_flush_all_data() {
267285
$redirect_to
268286
);
269287

270-
// Flush object cache.
271-
wpcable_cache::flush();
272-
273288
wp_safe_redirect( $redirect_to );
274289
exit;
275290
}
@@ -339,6 +354,12 @@ function codeable_last_fetch_info() {
339354
* @return bool
340355
*/
341356
function codeable_api_logged_in() {
357+
$token = get_option( 'wpcable_auth_token', false );
358+
359+
if ( ! $token ) {
360+
return false;
361+
}
362+
342363
$api = wpcable_api_calls::inst();
343364

344365
return $api->auth_token_known();

0 commit comments

Comments
 (0)