createVincentUserMiddleware<Defined in: app-sdk/src/expressMiddleware/express.ts:79 Returns an Express middleware function to authenticate a user using a JWT token, and a type-guard wrapper function for type-safe usage of route handlers guarded by the middleware. TheUserKey
>(config
):object
middleware()
function:
- Checks the
Authorization
header for a Bearer token, verifies the token, and checks its audience. - If the token is valid, it attaches the user information (decoded JWT, and raw JWT string) to the request object
- If the token is missing or invalid, it returns a 401 Unauthorized response with an error message.
req
should be set with the JWT with the userKey
configuration option.
The handler()
function:
- Provides a type-safe reference to
req
where theuserKey
you have provided is correctly inferred to the appropriate type - Note that it is still your responsibility to ensure you have attached the
middleware
somewhere in the chain before you use thehandler()
- If you forget, the
handler()
function will throw an error if the expectedreq[userKey]
does not exist.
- If you forget, the
Type Parameters
UserKey
UserKey
extends string
Parameters
config
allowedAudience
string
requiredAppId
undefined
| number
userKey
UserKey
Returns
object
handler()
handler: (handler
) => (req
,res
,next
) =>void
|Promise
<void
>
Parameters
handler
AuthenticatedRequestHandler
<UserKey
>
Returns
(req
,res
,next
):void
|Promise
<void
>
Parameters
req
Request
res
Response
next
NextFunction
Returns
void
| Promise
<void
>
middleware()
middleware: (req
,res
,next
) =>Promise
<void
>
Parameters
req
Request
res
Response
next
NextFunction
Returns
Promise
<void
>
Example
middleware
returned by this function. You can adapt this logic
to the HTTP framework of your choice.