@@ -43,19 +43,24 @@ public function __construct() {
4343 */
4444 public function register_routes (): void {
4545 // Generate JWT token
46- // register_rest_route(
47- // $this->namespace,
48- // '/' . $this->rest_base . '/authorize',
49- // array(
50- // 'methods' => WP_REST_Server::READABLE,
51- // 'callback' => array( $this, 'generate_token' ),
52- // 'permission_callback' => function ( WP_REST_Request $request ) {
53- // $authorization = $request->get_header( 'authorization' );
54- //
55- // return ! is_null( $authorization );
56- // },
57- // )
58- // );
46+ register_rest_route (
47+ $ this ->namespace ,
48+ '/ ' . $ this ->rest_base . '/authorize ' ,
49+ array (
50+ 'methods ' => WP_REST_Server::READABLE ,
51+ 'callback ' => array ( $ this , 'generate_token ' ),
52+ 'permission_callback ' => function ( WP_REST_Request $ request ) {
53+ // special case for user=demo param
54+ if ( $ request ->get_param ( 'user ' ) === 'demo ' ) {
55+ return true ;
56+ }
57+
58+ $ authorization = $ request ->get_header ( 'authorization ' );
59+
60+ return ! is_null ( $ authorization );
61+ },
62+ )
63+ );
5964
6065 // Validate JWT token
6166 register_rest_route (
@@ -125,20 +130,20 @@ public function generate_token( WP_REST_Request $request ) {
125130 /** Try to authenticate the user with the passed credentials*/
126131 $ user = wp_authenticate ( $ username , $ password );
127132
128- // If the authentication fails return a error
133+ // If the authentication fails return an error
129134 if ( is_wp_error ( $ user ) ) {
130135 $ error_code = $ user ->get_error_code ();
131136
132- return new WP_Error (
137+ $ data = new WP_Error (
133138 'woocommerce_pos_ ' . $ error_code ,
134139 $ user ->get_error_message ( $ error_code ),
135140 array (
136141 'status ' => 403 ,
137142 )
138143 );
139- }
140-
141- $ data = $ this -> auth_service -> generate_token ( $ user );
144+ } else {
145+ $ data = $ this -> auth_service -> generate_token ( $ user );
146+ }
142147
143148 /**
144149 * Let the user modify the data before sending it back
0 commit comments