| @@ -10,144 +10,91 @@ | ||
| 10 | 10 | class OsAuthController extends OsController { |
| 11 | 11 | |
| 12 | 12 | function __construct() { |
| 13 | 13 | parent::__construct(); |
| 14 | - $this->action_access['public'] = array_merge( | |
| 15 | - $this->action_access['public'], | |
| 16 | - [ | |
| 17 | - 'logout_customer', | |
| 18 | - 'login_customer', | |
| 19 | - 'login_customer_using_social_data', | |
| 20 | - 'login_customer_using_google_token', | |
| 21 | - 'login_customer_using_facebook_token', | |
| 22 | - 'request_otp', | |
| 23 | - 'verify_otp', | |
| 24 | - 'resend_otp', | |
| 25 | - ] | |
| 26 | - ); | |
| 14 | + $this->action_access['public'] = array_merge( $this->action_access['public'], [ | |
| 15 | + 'logout_customer', | |
| 16 | + 'login_customer', | |
| 17 | + 'login_customer_using_social_data', | |
| 18 | + 'login_customer_using_google_token', | |
| 19 | + 'login_customer_using_facebook_token', | |
| 20 | + 'request_otp', | |
| 21 | + 'verify_otp' | |
| 22 | + ] ); | |
| 27 | 23 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'auth/'; |
| 28 | 24 | } |
| 29 | 25 | |
| 30 | - public function verify_otp() { | |
| 26 | + public function verify_otp(){ | |
| 31 | 27 | $this->check_nonce( 'otp_verify_otp_nonce', $this->params['otp']['verify_nonce'] ); |
| 32 | 28 | $otp_verification_params = $this->params_for_otp_verification(); |
| 33 | - $otp_code = $otp_verification_params['otp_code']; | |
| 34 | - $contact_type = $otp_verification_params['contact_type']; | |
| 35 | - $contact_value = $otp_verification_params['contact_value']; | |
| 36 | - $delivery_method = $otp_verification_params['delivery_method']; | |
| 29 | + $otp_code = $otp_verification_params['otp_code']; | |
| 30 | + $contact_type = $otp_verification_params['contact_type']; | |
| 31 | + $contact_value = $otp_verification_params['contact_value']; | |
| 37 | 32 | |
| 38 | - $result = OsOTPHelper::verifyOTP( $otp_code, $contact_value, $contact_type, $delivery_method ); | |
| 33 | + $result = OsOTPHelper::verifyOTP($otp_code, $contact_value, $contact_type, 'email'); | |
| 39 | 34 | |
| 40 | - $message = __( 'Invalid Code', 'latepoint' ); | |
| 41 | - $status = LATEPOINT_STATUS_ERROR; | |
| 35 | + $message = __('Invalid Code', 'latepoint'); | |
| 36 | + $status = LATEPOINT_STATUS_ERROR; | |
| 42 | 37 | |
| 43 | - if ( is_wp_error( $result ) ) { | |
| 38 | + if ( is_wp_error($result) ) { | |
| 44 | 39 | $message = $result->get_error_message(); |
| 45 | - } elseif ( $result['status'] == LATEPOINT_STATUS_ERROR ) { | |
| 40 | + }elseif($result['status'] == LATEPOINT_STATUS_ERROR){ | |
| 46 | 41 | $message = $result['message']; |
| 47 | - } elseif ( $result['status'] == LATEPOINT_STATUS_SUCCESS ) { | |
| 42 | + }elseif($result['status'] == LATEPOINT_STATUS_SUCCESS) { | |
| 48 | 43 | // Success |
| 49 | - $status = LATEPOINT_STATUS_SUCCESS; | |
| 50 | - $message = OsOTPHelper::create_verification_token( $contact_value, $contact_type ); | |
| 44 | + $status = LATEPOINT_STATUS_SUCCESS; | |
| 45 | + $message = OsOTPHelper::create_verification_token($contact_value, $contact_type); | |
| 51 | 46 | // if auth is enabled - make sure customer is logged in |
| 52 | - if ( OsAuthHelper::is_customer_auth_enabled() && ! OsAuthHelper::is_customer_logged_in() ) { | |
| 53 | - $customer = OsCustomerHelper::get_by_contact( $contact_value, $contact_type ); | |
| 54 | - if ( $customer && ! $customer->is_new_record() ) { | |
| 55 | - OsAuthHelper::authorize_customer( $customer->id ); | |
| 47 | + if(OsAuthHelper::is_customer_auth_enabled() && !OsAuthHelper::is_customer_logged_in()){ | |
| 48 | + $customer = OsCustomerHelper::get_by_contact($contact_value, $contact_type); | |
| 49 | + if($customer && !$customer->is_new_record()){ | |
| 50 | + OsAuthHelper::authorize_customer($customer->id); | |
| 56 | 51 | } |
| 57 | 52 | } |
| 58 | 53 | } |
| 59 | - $this->send_json( | |
| 60 | - array( | |
| 61 | - 'status' => $status, | |
| 62 | - 'message' => $message, | |
| 63 | - ) | |
| 64 | - ); | |
| 54 | + $this->send_json( array( 'status' => $status, 'message' => $message ) ); | |
| 65 | 55 | } |
| 66 | 56 | |
| 67 | - public function resend_otp() { | |
| 68 | - $this->check_nonce( 'otp_resend_otp_nonce', $this->params['otp']['resend_nonce'] ); | |
| 69 | - $otp_verification_params = $this->params_for_otp_verification(); | |
| 70 | - $contact_type = $otp_verification_params['contact_type']; | |
| 71 | - $contact_value = $otp_verification_params['contact_value']; | |
| 72 | - $delivery_method = $otp_verification_params['delivery_method']; | |
| 73 | 57 | |
| 74 | - $result = OsOTPHelper::generateAndSendOTP( $contact_value, $contact_type, $delivery_method ); | |
| 75 | - | |
| 76 | - $message = __( 'Error sending OTP', 'latepoint' ); | |
| 77 | - $status = LATEPOINT_STATUS_ERROR; | |
| 78 | - | |
| 79 | - if ( is_wp_error( $result ) ) { | |
| 80 | - $message = $result->get_error_message(); | |
| 81 | - } elseif ( $result['status'] == LATEPOINT_STATUS_ERROR ) { | |
| 82 | - $message = $result['message']; | |
| 83 | - } elseif ( $result['status'] == LATEPOINT_STATUS_SUCCESS ) { | |
| 84 | - // Success | |
| 85 | - $status = LATEPOINT_STATUS_SUCCESS; | |
| 86 | - $message = OsOTPHelper::otp_input_box_html( $contact_type, $contact_value, $delivery_method ); | |
| 87 | - } | |
| 88 | - $this->send_json( | |
| 89 | - array( | |
| 90 | - 'status' => $status, | |
| 91 | - 'message' => $message, | |
| 92 | - ) | |
| 93 | - ); | |
| 94 | - } | |
| 95 | - | |
| 96 | - public function request_otp() { | |
| 58 | + public function request_otp(){ | |
| 97 | 59 | $this->check_nonce( 'auth_nonce', $this->params['auth']['nonce'] ); |
| 98 | 60 | |
| 99 | - $auth_params = $this->params_for_otp_request(); | |
| 100 | - $contact_type = $auth_params['contact_type']; | |
| 101 | - $contact_value = $auth_params[ $contact_type ]; | |
| 61 | + $auth_params = $this->params_for_otp_request(); | |
| 62 | + $contact_type = $auth_params['contact_type']; | |
| 63 | + $contact_value = $auth_params[$contact_type]; | |
| 102 | 64 | $delivery_method = $auth_params['delivery_method']; |
| 103 | 65 | |
| 104 | - if ( OsAuthHelper::is_classic_auth_flow() ) { | |
| 66 | + if(OsAuthHelper::is_classic_auth_flow()){ | |
| 105 | 67 | // in classic flow - you can't send a OTP request to a non existent account |
| 106 | 68 | $customer = new OsCustomerModel(); |
| 107 | - if ( $contact_type == 'email' ) { | |
| 108 | - $existing_customer = $customer->where( [ 'email' => $contact_value ] )->set_limit( 1 )->get_results_as_models(); | |
| 109 | - if ( ! $existing_customer ) { | |
| 110 | - $this->send_json( | |
| 111 | - array( | |
| 112 | - 'status' => LATEPOINT_STATUS_ERROR, | |
| 113 | - 'message' => __( 'We don\'t recognize this email. Double-check it or create an account.', 'latepoint' ), | |
| 114 | - ) | |
| 115 | - ); | |
| 69 | + if($contact_type == 'email'){ | |
| 70 | + $existing_customer = $customer->where(['email' => $contact_value])->set_limit(1)->get_results_as_models(); | |
| 71 | + if(!$existing_customer){ | |
| 72 | + $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => __('We don\'t recognize this email. Double-check it or create an account.', 'latepoint') ) ); | |
| 116 | 73 | } |
| 117 | - } elseif ( $contact_type == 'phone' ) { | |
| 118 | - $existing_customer = $customer->where( [ 'phone' => $contact_value ] )->set_limit( 1 )->get_results_as_models(); | |
| 119 | - if ( ! $existing_customer ) { | |
| 120 | - $this->send_json( | |
| 121 | - array( | |
| 122 | - 'status' => LATEPOINT_STATUS_ERROR, | |
| 123 | - 'message' => __( 'We don\'t recognize this phone number. Double-check it or create an account.', 'latepoint' ), | |
| 124 | - ) | |
| 125 | - ); | |
| 74 | + }elseif($contact_type == 'phone'){ | |
| 75 | + $existing_customer = $customer->where(['phone' => $contact_value])->set_limit(1)->get_results_as_models(); | |
| 76 | + if(!$existing_customer){ | |
| 77 | + $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => __('We don\'t recognize this phone number. Double-check it or create an account.', 'latepoint') ) ); | |
| 126 | 78 | } |
| 127 | 79 | } |
| 128 | 80 | } |
| 129 | 81 | |
| 130 | - $result = OsOTPHelper::generateAndSendOTP( $contact_value, $contact_type, $delivery_method ); | |
| 82 | + $result = OsOTPHelper::generateAndSendOTP($contact_value, $contact_type, $delivery_method); | |
| 131 | 83 | |
| 132 | - $message = __( 'Error sending OTP', 'latepoint' ); | |
| 133 | - $status = LATEPOINT_STATUS_ERROR; | |
| 84 | + $message = __('Error sending OTP', 'latepoint'); | |
| 85 | + $status = LATEPOINT_STATUS_ERROR; | |
| 134 | 86 | |
| 135 | - if ( is_wp_error( $result ) ) { | |
| 87 | + if ( is_wp_error($result) ) { | |
| 136 | 88 | $message = $result->get_error_message(); |
| 137 | - } elseif ( $result['status'] == LATEPOINT_STATUS_ERROR ) { | |
| 89 | + }elseif($result['status'] == LATEPOINT_STATUS_ERROR){ | |
| 138 | 90 | $message = $result['message']; |
| 139 | - } elseif ( $result['status'] == LATEPOINT_STATUS_SUCCESS ) { | |
| 91 | + }elseif($result['status'] == LATEPOINT_STATUS_SUCCESS){ | |
| 140 | 92 | // Success |
| 141 | - $status = LATEPOINT_STATUS_SUCCESS; | |
| 142 | - $message = OsOTPHelper::otp_input_box_html( $contact_type, $contact_value, $delivery_method ); | |
| 93 | + $status = LATEPOINT_STATUS_SUCCESS; | |
| 94 | + $message = OsOTPHelper::otp_input_box_html($contact_type, $contact_value, $delivery_method); | |
| 143 | 95 | } |
| 144 | - $this->send_json( | |
| 145 | - array( | |
| 146 | - 'status' => $status, | |
| 147 | - 'message' => $message, | |
| 148 | - ) | |
| 149 | - ); | |
| 96 | + $this->send_json( array( 'status' => $status, 'message' => $message ) ); | |
| 150 | 97 | } |
| 151 | 98 | |
| 152 | 99 | |
| 153 | 100 | private function params_for_otp_verification(): array { |
| @@ -155,26 +102,23 @@ | ||
| 155 | 102 | if ( empty( $params ) ) { |
| 156 | 103 | return []; |
| 157 | 104 | } |
| 158 | 105 | |
| 159 | - $otp_params = OsParamsHelper::permit_params( | |
| 160 | - $params, | |
| 161 | - [ | |
| 162 | - 'contact_value', | |
| 163 | - 'contact_type', | |
| 164 | - 'delivery_method', | |
| 165 | - 'otp_code', | |
| 166 | - ] | |
| 167 | - ); | |
| 106 | + $otp_params = OsParamsHelper::permit_params( $params, [ | |
| 107 | + 'contact_value', | |
| 108 | + 'contact_type', | |
| 109 | + 'delivery_method', | |
| 110 | + 'otp_code' | |
| 111 | + ] ); | |
| 168 | 112 | |
| 169 | - $otp_params['otp_code'] = sanitize_text_field( $otp_params['otp_code'] ); | |
| 113 | + $otp_params['otp_code'] = sanitize_text_field( $otp_params['otp_code'] ); | |
| 170 | 114 | $otp_params['delivery_method'] = sanitize_text_field( $otp_params['delivery_method'] ); |
| 171 | 115 | |
| 172 | - if ( $otp_params['contact_type'] == 'phone' ) { | |
| 116 | + if($otp_params['contact_type'] == 'phone'){ | |
| 173 | 117 | $otp_params['contact_value'] = sanitize_text_field( $otp_params['contact_value'] ); |
| 174 | 118 | } |
| 175 | 119 | |
| 176 | - if ( $otp_params['contact_type'] == 'email' ) { | |
| 120 | + if($otp_params['contact_type'] == 'email'){ | |
| 177 | 121 | $otp_params['contact_value'] = sanitize_email( $otp_params['contact_value'] ); |
| 178 | 122 | } |
| 179 | 123 | |
| 180 | 124 | /** |
| @@ -196,18 +140,15 @@ | ||
| 196 | 140 | if ( empty( $params ) ) { |
| 197 | 141 | return []; |
| 198 | 142 | } |
| 199 | 143 | |
| 200 | - $auth_params = OsParamsHelper::permit_params( | |
| 201 | - $params, | |
| 202 | - [ | |
| 203 | - 'email', | |
| 204 | - 'phone', | |
| 205 | - 'contact_type', | |
| 206 | - 'delivery_method', | |
| 207 | - 'otp_code', | |
| 208 | - ] | |
| 209 | - ); | |
| 144 | + $auth_params = OsParamsHelper::permit_params( $params, [ | |
| 145 | + 'email', | |
| 146 | + 'phone', | |
| 147 | + 'contact_type', | |
| 148 | + 'delivery_method', | |
| 149 | + 'otp_code' | |
| 150 | + ] ); | |
| 210 | 151 | |
| 211 | 152 | if ( ! empty( $auth_params['email'] ) ) { |
| 212 | 153 | $auth_params['email'] = sanitize_email( $auth_params['email'] ); |
| 213 | 154 | } |
| @@ -234,54 +175,37 @@ | ||
| 234 | 175 | |
| 235 | 176 | |
| 236 | 177 | // Logs out customer and shows blank contact step |
| 237 | 178 | public function logout_customer() { |
| 238 | - // Verify nonce. | |
| 239 | - $this->check_nonce( 'auth_nonce', $this->params['auth']['nonce'] ); | |
| 240 | - | |
| 241 | 179 | OsAuthHelper::logout_customer(); |
| 242 | 180 | |
| 243 | 181 | if ( $this->get_return_format() == 'json' ) { |
| 244 | - $this->send_json( | |
| 245 | - array( | |
| 246 | - 'status' => LATEPOINT_STATUS_SUCCESS, | |
| 247 | - 'message' => __( 'You have been logged out of your account.', 'latepoint' ), | |
| 248 | - ) | |
| 249 | - ); | |
| 182 | + $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => __( 'You have been logged out of your account.', 'latepoint' ) ) ); | |
| 250 | 183 | } |
| 251 | 184 | } |
| 252 | 185 | |
| 253 | 186 | // Login customer and show contact step with prefilled info |
| 254 | 187 | public function login_customer() { |
| 255 | - // Verify nonce. | |
| 256 | - $this->check_nonce( 'auth_nonce', $this->params['auth']['nonce'] ); | |
| 257 | - | |
| 258 | - $contact_type = $this->params['auth']['contact_type']; | |
| 259 | - $contact_value = ( $contact_type == 'email' ) ? $this->params['auth']['email'] : $this->params['auth']['phone']; | |
| 260 | - $customer = OsAuthHelper::login_customer( $contact_value, $this->params['auth']['password'], $this->params['auth']['contact_type'] ); | |
| 188 | + $contact_type = $this->params['auth']['contact_type']; | |
| 189 | + $contact_value = ($contact_type == 'email') ? $this->params['auth']['email'] : $this->params['auth']['phone']; | |
| 190 | + $customer = OsAuthHelper::login_customer( $contact_value, $this->params['auth']['password'], $this->params['auth']['contact_type'] ); | |
| 261 | 191 | if ( $customer ) { |
| 262 | 192 | $status = LATEPOINT_STATUS_SUCCESS; |
| 263 | 193 | $customer_id = $customer->id; |
| 264 | 194 | $response_html = __( 'Welcome back', 'latepoint' ); |
| 265 | 195 | } else { |
| 266 | - $status = LATEPOINT_STATUS_ERROR; | |
| 267 | - if ( $contact_type == 'email' ) { | |
| 196 | + $status = LATEPOINT_STATUS_ERROR; | |
| 197 | + if($contact_type == 'email'){ | |
| 268 | 198 | $response_html = __( 'Sorry, that email or password didn\'t work.', 'latepoint' ); |
| 269 | - } elseif ( $contact_type == 'phone' ) { | |
| 199 | + }elseif($contact_type == 'phone'){ | |
| 270 | 200 | $response_html = __( 'Sorry, that phone number or password didn\'t work.', 'latepoint' ); |
| 271 | - } else { | |
| 201 | + }else{ | |
| 272 | 202 | $response_html = __( 'Sorry, that didn\'t work.', 'latepoint' ); |
| 273 | 203 | } |
| 274 | - $customer_id = ''; | |
| 204 | + $customer_id = ''; | |
| 275 | 205 | } |
| 276 | 206 | if ( $this->get_return_format() == 'json' ) { |
| 277 | - $this->send_json( | |
| 278 | - array( | |
| 279 | - 'status' => $status, | |
| 280 | - 'message' => $response_html, | |
| 281 | - 'customer_id' => $customer_id, | |
| 282 | - ) | |
| 283 | - ); | |
| 207 | + $this->send_json( array( 'status' => $status, 'message' => $response_html, 'customer_id' => $customer_id ) ); | |
| 284 | 208 | } |
| 285 | 209 | } |
| 286 | 210 | |
| 287 | 211 | public function login_customer_using_social_data( $network, $social_user ) { |
| @@ -342,46 +266,31 @@ | ||
| 342 | 266 | $status = LATEPOINT_STATUS_ERROR; |
| 343 | 267 | $response_html = $social_user['error']; |
| 344 | 268 | } |
| 345 | 269 | if ( $this->get_return_format() == 'json' ) { |
| 346 | - $this->send_json( | |
| 347 | - array( | |
| 348 | - 'status' => $status, | |
| 349 | - 'message' => $response_html, | |
| 350 | - 'customer_id' => $customer_id, | |
| 351 | - ) | |
| 352 | - ); | |
| 270 | + $this->send_json( array( 'status' => $status, 'message' => $response_html, 'customer_id' => $customer_id ) ); | |
| 353 | 271 | } |
| 272 | + | |
| 354 | 273 | } |
| 355 | 274 | |
| 356 | 275 | |
| 357 | 276 | public function login_customer_using_google_token() { |
| 358 | - // Condition for pro compatibility. Remove later. | |
| 359 | - if ( isset( $this->params['nonce'] ) ) { | |
| 360 | - // Verify nonce. | |
| 361 | - $this->check_nonce( 'social_login_nonce', $this->params['nonce'] ); | |
| 362 | - } | |
| 363 | - | |
| 364 | 277 | $social_user = []; |
| 365 | 278 | $token = sanitize_text_field( $this->params['token'] ); |
| 366 | 279 | $social_user = apply_filters( 'latepoint_get_social_user_by_token', $social_user, 'google', $token ); |
| 367 | - if ( ! empty( $social_user ) ) { | |
| 280 | + if ( !empty($social_user) ) { | |
| 368 | 281 | $this->login_customer_using_social_data( 'google', $social_user ); |
| 369 | 282 | } |
| 370 | 283 | } |
| 371 | 284 | |
| 372 | 285 | public function login_customer_using_facebook_token() { |
| 373 | - // Condition for pro compatibility. Remove later. | |
| 374 | - if ( isset( $this->params['nonce'] ) ) { | |
| 375 | - // Verify nonce. | |
| 376 | - $this->check_nonce( 'social_login_nonce', $this->params['nonce'] ); | |
| 377 | - } | |
| 378 | - | |
| 379 | 286 | $social_user = []; |
| 380 | 287 | $token = sanitize_text_field( $this->params['token'] ); |
| 381 | 288 | $social_user = apply_filters( 'latepoint_get_social_user_by_token', $social_user, 'facebook', $token ); |
| 382 | - if ( ! empty( $social_user ) ) { | |
| 289 | + if ( !empty($social_user) ) { | |
| 383 | 290 | $this->login_customer_using_social_data( 'facebook', $social_user ); |
| 384 | 291 | } |
| 385 | 292 | } |
| 293 | + | |
| 294 | + | |
| 386 | 295 | } |
| 387 | -endif; | |
| 296 | +endif; | |