Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions example/auth-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Elysia, t } from 'elysia'
import { jwt } from '../src'

const authPlugin = new Elysia({ name: 'authPlugin' })
.use(
jwt({
name: 'jwt',
secret: 'top-secret',
schema: t.Object({
name: t.String()
})
})
)
.derive(async ({ cookie: { auth }, jwt }) => {
const user = await jwt.verify(auth.value)
if (!user) throw new Error('Unauthorized')
return { user: user }
})
Comment on lines +14 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Guard against missing auth cookie to avoid runtime errors

If the auth cookie is absent (unauthenticated request or different cookie name), auth will be undefined and auth.value will throw a TypeError before you can treat the request as unauthorized. That will likely surface as a 500 instead of a clean 401-style auth failure.

Consider defensively checking the cookie before verifying the token:

-	.derive(async ({ cookie: { auth }, jwt }) => {
-		const user = await jwt.verify(auth.value)
-		if (!user) throw new Error('Unauthorized')
-		return { user: user }
-	})
+	.derive(async ({ cookie: { auth }, jwt }) => {
+		const token = auth?.value
+
+		if (!token) {
+			throw new Error('Unauthorized')
+		}
+
+		const user = await jwt.verify(token)
+
+		if (!user) {
+			throw new Error('Unauthorized')
+		}
+
+		return { user }
+	})

This keeps the middleware semantics (all failures become “unauthorized”) while avoiding low-level runtime errors from missing cookies.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.derive(async ({ cookie: { auth }, jwt }) => {
const user = await jwt.verify(auth.value)
if (!user) throw new Error('Unauthorized')
return { user: user }
})
.derive(async ({ cookie: { auth }, jwt }) => {
const token = auth?.value
if (!token) {
throw new Error('Unauthorized')
}
const user = await jwt.verify(token)
if (!user) {
throw new Error('Unauthorized')
}
return { user }
})
🤖 Prompt for AI Agents
In example/auth-plugin.ts around lines 14 to 18, the code assumes cookie.auth
exists and accesses auth.value which can raise a TypeError if the cookie is
missing; update the derive handler to first check that cookie.auth is present
and has a value (e.g., early-throw an Unauthorized error if missing), then call
jwt.verify inside a try/catch (or otherwise handle verification failures) and
throw the same Unauthorized error on any verification or falsy user result so
all failure modes produce a controlled 401-style response instead of a runtime
500.

.as('scoped')

const protectedRoutes = new Elysia()
.use(authPlugin)
.get('/me', ({ user }) => `Viewing protected data as ${user.name}`)

const app = new Elysia()
.use(protectedRoutes)
.get('/public', () => ({ message: 'Public Data' }))
.listen(8080)