| @@ -69,13 +69,34 @@ | ||
| 69 | 69 | */ |
| 70 | 70 | public function google_auth( $code = '' ) { |
| 71 | 71 | $client = timetics_get_google_client(); |
| 72 | 72 | |
| 73 | - $client->add_scope( Calendar::scope() ); | |
| 74 | - $data = $client->fetch_access_token_with_auth_code( $code ); | |
| 75 | - $data['code'] = $code; | |
| 73 | + // If no code is provided, redirect to Google's authorization page | |
| 74 | + if ( empty($code) ) { | |
| 75 | + $client->add_scope( Calendar::scope() ); | |
| 76 | + $auth_url = $client->get_auth_url(); | |
| 77 | + wp_redirect($auth_url); | |
| 78 | + exit; | |
| 79 | + } | |
| 76 | 80 | |
| 77 | - timetics_update_google_auth( get_current_user_id(), $data ); | |
| 81 | + try { | |
| 82 | + $client->add_scope( Calendar::scope() ); | |
| 83 | + $data = $client->fetch_access_token_with_auth_code( $code ); | |
| 84 | + $data['code'] = $code; | |
| 85 | + | |
| 86 | + timetics_update_google_auth( get_current_user_id(), $data ); | |
| 87 | + | |
| 88 | + wp_redirect(admin_url('admin.php?page=timetics#/settings')); | |
| 89 | + exit; | |
| 90 | + } catch (\Exception $e) { | |
| 91 | + $error_message = sprintf( | |
| 92 | + '<p>%s</p><p><a href="%s" class="button button-primary">Go Back</a></p>', | |
| 93 | + esc_html( $e->getMessage() ), | |
| 94 | + esc_url( admin_url('admin.php?page=timetics#/settings') ) | |
| 95 | + ); | |
| 96 | + | |
| 97 | + wp_die($error_message, esc_html__('Google Auth Error', 'timetics'), array('back_link' => true)); | |
| 98 | + } | |
| 78 | 99 | } |
| 79 | 100 | |
| 80 | 101 | /** |
| 81 | 102 | * Authentication for zoom |