Skip to content

Commit cc719f1

Browse files
committed
Fix example app + tests
1 parent 0763369 commit cc719f1

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

apps/example-todo-app/lib/middlewares/with-auth-token.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import { UnauthorizedException, Middleware } from "nextlove"
1+
import {
2+
UnauthorizedException,
3+
Middleware,
4+
AuthMethodDoesNotApplyException,
5+
} from "nextlove"
26

37
export const withAuthToken: Middleware<{
48
auth: {
59
authorized_by: "auth_token"
610
}
711
}> = (next) => async (req, res) => {
12+
if (req.headers.authorization === undefined) {
13+
throw new AuthMethodDoesNotApplyException({
14+
type: "unauthorized",
15+
message: "No Authorization header provided",
16+
})
17+
}
818
if (req.headers.authorization?.split("Bearer ")?.[1] !== "auth_token") {
919
throw new UnauthorizedException({
1020
type: "unauthorized",

apps/example-todo-app/lib/middlewares/with-user-session.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import { UnauthorizedException, Middleware } from "nextlove"
1+
import {
2+
UnauthorizedException,
3+
Middleware,
4+
AuthMethodDoesNotApplyException,
5+
} from "nextlove"
26

37
export const withUserSession: Middleware<{
48
auth: {
59
authorized_by: "user_session"
610
}
711
}> = (next) => async (req, res) => {
12+
if (req.headers["x-user-session-token"] === undefined) {
13+
throw new AuthMethodDoesNotApplyException({
14+
type: "unauthorized",
15+
message: "No X-User-Session-Token header provided",
16+
})
17+
}
818
if (req.headers["x-user-session-token"] !== "user_session_token") {
919
throw new UnauthorizedException({
1020
type: "unauthorized",

apps/example-todo-app/tests/api/todo/auth-token-or-user-session.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test("GET /todo/auth-token-or-user-session", async (t) => {
3838
// Message set by custom onMultipleAuthMiddlewareFailures() hook
3939
t.true(
4040
noAuthResponse.data.error.message.includes(
41-
"Multiple auth middleware failures"
41+
"No authentication methods succeeded"
4242
)
4343
)
4444
})

apps/example-todo-app/tests/api/todo/multiple-auth-errors-handled-by-route.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ test("GET /todo/multiple-auth-errors-handled-by-route", async (t) => {
1010
.catch((error: AxiosError) => {
1111
t.like(error.response, {
1212
error: {
13-
message: "Received 2 unauthorized errors",
14-
type: "multiple_unauthorized",
13+
message: "No authentication methods succeeded",
14+
type: "unauthorized",
1515
},
1616
})
1717
})

0 commit comments

Comments
 (0)