Skip to content

Commit 27a4260

Browse files
committed
- allow cookies to be set upon authentication
- set one header instead of replacing all headers
2 parents 0a28807 + b08e06e commit 27a4260

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/Auth.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,19 @@ public static function is_refresh_token() {
370370
*/
371371
protected static function authenticate_user( $username, $password ) {
372372

373-
/**
374-
* Try to authenticate the user with the passed credentials
375-
*/
376-
$user = wp_authenticate( sanitize_user( $username ), trim( $password ) );
373+
if ( defined( 'GRAPHQL_JWT_AUTH_SET_COOKIES' ) && ! empty( GRAPHQL_JWT_AUTH_SET_COOKIES ) && GRAPHQL_JWT_AUTH_SET_COOKIES ) {
374+
$credentials = [
375+
'user_login' => sanitize_user( $username ),
376+
'user_password' => trim( $password ),
377+
'remember' => false,
378+
];
379+
380+
// Try to authenticate the user with the passed credentials, log him in and set cookies
381+
$user = wp_signon( $credentials, true );
382+
} else {
383+
// Try to authenticate the user with the passed credentials
384+
$user = wp_authenticate( sanitize_user( $username ), trim( $password ) );
385+
}
377386

378387
/**
379388
* If the authentication fails return a error

src/ManageTokens.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,7 @@ public static function add_auth_headers_to_rest_response( $response ) {
342342
*
343343
* Might need a patch to core to allow for individual filtering.
344344
*/
345-
$response->set_headers(
346-
[ 'Access-Control-Expose-Headers' => 'X-WP-Total, X-WP-TotalPages, X-JWT-Refresh' ]
347-
);
345+
$response->header( 'Access-Control-Expose-Headers', 'X-WP-Total, X-WP-TotalPages, X-JWT-Refresh', true );
348346

349347
$refresh_token = null;
350348

@@ -357,7 +355,7 @@ public static function add_auth_headers_to_rest_response( $response ) {
357355
}
358356

359357
if ( $refresh_token ) {
360-
$response->set_headers( [ 'X-JWT-Refresh' => $refresh_token ] );
358+
$response->header( 'X-JWT-Refresh', $refresh_token, true );
361359
}
362360

363361
return $response;

0 commit comments

Comments
 (0)