Captcha
5 years ago
LoginRoute.php
5 years ago
LogoutRoute.php
5 years ago
VerifyEmailRoute.php
5 years ago
LoginRoute.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonorDashboards\Routes; |
| 4 | |
| 5 | use WP_REST_Request; |
| 6 | use WP_REST_Response; |
| 7 | use Give\API\RestRoute; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.10.0 |
| 11 | */ |
| 12 | class LoginRoute implements RestRoute { |
| 13 | |
| 14 | /** @var string */ |
| 15 | protected $endpoint = 'donor-dashboard/login'; |
| 16 | |
| 17 | /** |
| 18 | * @inheritDoc |
| 19 | */ |
| 20 | public function registerRoute() { |
| 21 | register_rest_route( |
| 22 | 'give-api/v2', |
| 23 | $this->endpoint, |
| 24 | [ |
| 25 | [ |
| 26 | 'methods' => 'POST', |
| 27 | 'callback' => [ $this, 'handleRequest' ], |
| 28 | 'permission_callback' => '__return_true', |
| 29 | ], |
| 30 | 'args' => [ |
| 31 | 'login' => [ |
| 32 | 'type' => 'string', |
| 33 | 'required' => true, |
| 34 | 'sanitize_callback' => 'sanitize_text_field', |
| 35 | ], |
| 36 | 'password' => [ |
| 37 | 'type' => 'string', |
| 38 | 'required' => true, |
| 39 | 'sanitize_callback' => 'sanitize_text_field', |
| 40 | ], |
| 41 | ], |
| 42 | ] |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Handles login request |
| 48 | * |
| 49 | * @param WP_REST_Request $request |
| 50 | * |
| 51 | * @return array |
| 52 | * |
| 53 | * @since 2.10.0 |
| 54 | */ |
| 55 | public function handleRequest( WP_REST_Request $request ) { |
| 56 | |
| 57 | $login = $request->get_param( 'login' ); |
| 58 | $password = $request->get_param( 'password' ); |
| 59 | |
| 60 | $user = get_user_by( 'login', $login ); |
| 61 | |
| 62 | if ( ! $user ) { |
| 63 | $user = get_user_by( 'email', $login ); |
| 64 | } |
| 65 | |
| 66 | if ( $user ) { |
| 67 | if ( wp_check_password( $password, $user->user_pass, $user->ID ) ) { |
| 68 | give_log_user_in( $user->ID, $login, $password ); |
| 69 | return new WP_REST_Response( |
| 70 | [ |
| 71 | 'status' => 200, |
| 72 | 'response' => 'login_successful', |
| 73 | 'body_response' => [ |
| 74 | 'login' => $user->login, |
| 75 | 'id' => $user->ID, |
| 76 | ], |
| 77 | ] |
| 78 | ); |
| 79 | } else { |
| 80 | return new WP_REST_Response( |
| 81 | [ |
| 82 | 'status' => 400, |
| 83 | 'response' => 'incorrect_password', |
| 84 | 'body_response' => [ |
| 85 | 'message' => __( 'The provided password was incorrect.', 'give' ), |
| 86 | ], |
| 87 | ] |
| 88 | ); |
| 89 | } |
| 90 | } else { |
| 91 | return new WP_REST_Response( |
| 92 | [ |
| 93 | 'status' => 400, |
| 94 | 'response' => 'unidentified_login', |
| 95 | 'body_response' => [ |
| 96 | 'message' => sprintf( __( 'A record for the provided login (%s) could not be found.', 'give' ), $login ), |
| 97 | ], |
| 98 | ] |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 |