Skip to content

Commit fde9abf

Browse files
Return this in some Auth0Response methods
1 parent 6a7fc06 commit fde9abf

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

src/server/auth-client.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -967,12 +967,12 @@ export class AuthClient {
967967

968968
if (!session) {
969969
if (this.noContentProfileResponseWhenUnauthenticated) {
970-
auth0Res.status(null, 204);
971-
} else {
972-
auth0Res.status(null, 401);
970+
return auth0Res.status(null, 204);
973971
}
974-
return auth0Res;
972+
973+
return auth0Res.status(null, 401);
975974
}
975+
976976
auth0Res.json(session?.user);
977977
auth0Res.addCacheControlHeadersForSession();
978978
return auth0Res;
@@ -987,7 +987,7 @@ export class AuthClient {
987987
const scope = auth0Req.getUrl().searchParams.get("scope");
988988

989989
if (!session) {
990-
auth0Res.json(
990+
return auth0Res.json(
991991
{
992992
error: {
993993
message: "The user does not have an active session.",
@@ -998,7 +998,6 @@ export class AuthClient {
998998
status: 401
999999
}
10001000
);
1001-
return auth0Res;
10021001
}
10031002

10041003
const [error, getTokenSetResponse] = await this.getTokenSet(session, {
@@ -1007,7 +1006,7 @@ export class AuthClient {
10071006
});
10081007

10091008
if (error) {
1010-
auth0Res.json(
1009+
return auth0Res.json(
10111010
{
10121011
error: {
10131012
message: error.message,
@@ -1018,7 +1017,6 @@ export class AuthClient {
10181017
status: 401
10191018
}
10201019
);
1021-
return auth0Res;
10221020
}
10231021

10241022
const { tokenSet: updatedTokenSet } = getTokenSetResponse;
@@ -1047,37 +1045,32 @@ export class AuthClient {
10471045
auth0Res: Auth0Response
10481046
): Promise<Auth0Response> {
10491047
if (!this.sessionStore.store) {
1050-
auth0Res.status("A session data store is not configured.", 500);
1051-
return auth0Res;
1048+
return auth0Res.status("A session data store is not configured.", 500);
10521049
}
10531050

10541051
if (!this.sessionStore.store.deleteByLogoutToken) {
1055-
auth0Res.status(
1052+
return auth0Res.status(
10561053
"Back-channel logout is not supported by the session data store.",
10571054
500
10581055
);
1059-
return auth0Res;
10601056
}
10611057

10621058
const body = new URLSearchParams(await auth0Req.getBody());
10631059
const logoutToken = body.get("logout_token");
10641060

10651061
if (!logoutToken) {
1066-
auth0Res.status("Missing `logout_token` in the request body.", 400);
1067-
return auth0Res;
1062+
return auth0Res.status("Missing `logout_token` in the request body.", 400);
10681063
}
10691064

10701065
const [error, logoutTokenClaims] =
10711066
await this.verifyLogoutToken(logoutToken);
10721067
if (error) {
1073-
auth0Res.status(error.message, 400);
1074-
return auth0Res;
1068+
return auth0Res.status(error.message, 400);
10751069
}
10761070

10771071
await this.sessionStore.store.deleteByLogoutToken(logoutTokenClaims);
10781072

1079-
auth0Res.status(null, 204);
1080-
return auth0Res;
1073+
return auth0Res.status(null, 204);
10811074
}
10821075

10831076
async handleConnectAccount(
@@ -1099,13 +1092,11 @@ export class AuthClient {
10991092
);
11001093

11011094
if (!connection) {
1102-
auth0Res.status("A connection is required.", 400);
1103-
return auth0Res;
1095+
return auth0Res.status("A connection is required.", 400);
11041096
}
11051097

11061098
if (!session) {
1107-
auth0Res.status("The user does not have an active session.", 401);
1108-
return auth0Res;
1099+
return auth0Res.status("The user does not have an active session.", 401);
11091100
}
11101101

11111102
const [getTokenSetError, getTokenSetResponse] = await this.getTokenSet(
@@ -1117,11 +1108,10 @@ export class AuthClient {
11171108
);
11181109

11191110
if (getTokenSetError) {
1120-
auth0Res.status(
1111+
return auth0Res.status(
11211112
"Failed to retrieve a connected account access token.",
11221113
401
11231114
);
1124-
return auth0Res;
11251115
}
11261116

11271117
const { tokenSet } = getTokenSetResponse;
@@ -1139,11 +1129,10 @@ export class AuthClient {
11391129
await this.connectAccount({ tokenSet, ...connectAccountParams });
11401130

11411131
if (connectAccountError) {
1142-
auth0Res.status(
1132+
return auth0Res.status(
11431133
connectAccountError.message,
11441134
connectAccountError.cause?.status ?? 500
11451135
);
1146-
return auth0Res;
11471136
}
11481137

11491138
// update the session with the new token set, if necessary

src/server/http/auth0-next-response.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ export class Auth0NextResponse extends Auth0Response<NextResponse> {
3838
*
3939
* @param url - The URL to redirect to.
4040
*/
41-
public redirect(url: string): void {
41+
public redirect(url: string) {
4242
this.res = this.#mergeHeaders(this.res, NextResponse.redirect(url));
43+
44+
return this;
4345
}
4446

4547
/**
@@ -55,12 +57,14 @@ export class Auth0NextResponse extends Auth0Response<NextResponse> {
5557
* res.status("Unauthorized", 401);
5658
* ```
5759
*/
58-
public status(message: string | null, status: number): void {
60+
public status(message: string | null, status: number) {
5961
const body = status === 204 ? null : message;
6062
this.res = this.#mergeHeaders(
6163
this.res,
6264
new NextResponse(body, { status })
6365
);
66+
67+
return this;
6468
}
6569

6670
/**
@@ -74,8 +78,10 @@ export class Auth0NextResponse extends Auth0Response<NextResponse> {
7478
* res.json({ error: "Invalid token" }, { status: 401 });
7579
* ```
7680
*/
77-
public json(body: any, init?: ResponseInit): void {
81+
public json(body: any, init?: ResponseInit) {
7882
this.res = this.#mergeHeaders(this.res, NextResponse.json(body, init));
83+
84+
return this;
7985
}
8086

8187
/**

src/server/http/auth0-response.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export abstract class Auth0Response<TResponse = any> {
5656
*
5757
* @param url - The URL to redirect to. Should be validated before calling this method.
5858
*/
59-
abstract redirect(url: string): void;
59+
abstract redirect(url: string): Auth0Response<TResponse>;
6060

6161
/**
6262
* Sets the HTTP status code of the response.
@@ -67,7 +67,7 @@ export abstract class Auth0Response<TResponse = any> {
6767
* @param message - The response body as a string or null.
6868
* @param status - The HTTP status code (e.g., 200, 401, 500).
6969
*/
70-
abstract status(message: string | null, status: number): TResponse | void;
70+
abstract status(message: string | null, status: number): Auth0Response<TResponse>;
7171

7272
/**
7373
* Sends a JSON response with the specified body.
@@ -82,7 +82,7 @@ export abstract class Auth0Response<TResponse = any> {
8282
* user data, or other structured data.
8383
* @param init - Optional response initialization options (status, headers, etc.).
8484
*/
85-
abstract json(body: any, init?: ResponseInit): void;
85+
abstract json(body: any, init?: ResponseInit): Auth0Response<TResponse>;
8686

8787
/**
8888
* Adds cache control headers to the response to prevent caching of sensitive data.

0 commit comments

Comments
 (0)