Auth Middleware
Kobble provides a built-in auth middleware that you can use to protect your routes from unauthenticated users.
Usage
To use the auth middleware, just create a file named middleware.ts
in the src
directory of your project and add the following code:
All routes defined in the publicRoutes
array will be accessible without authentication.
If an unauthenticated user tries to access a protected route, they will be redirected to the login page on your Customer Portal.
Routes Matcher
Often, you may want to use path parameters or wildcard to match multiple routes. Here is an example of matchers that you can use.
Wildcard
You can use (.*)
to match all routes under a specific path.
Examples:
"/(.*)"
: Matches all routes under the root path."/public/(.*)"
: Matches all routes under the/public
path./something/:user*
: Matches all routes under the/something/:user
(e.g./something/john
,/something/john/doe
, etc).
Path parameters
You can use the :param
notation to match a path parameter with a given name.
Examples:
"/categories/:category"
: Matches all routes under the/categories
path with a path parametercategory
."/categories/:category/posts/:post"
: Matches all routes under the/categories
path with two path parameterscategory
andpost
.
:
Advanced path parameters
You can use more advanced patterns to match path parameters with specific format.
Examples:
/something/:user*
: Matches all routes under the/something/:user
(e.g./something/john
,/something/john/doe
, etc)./something/(\\d+)
: Matches all routes under the/something
path with a path parameter that must be a number./something/:number-(\\d+)
: Matches all routes under the/something
path with a path parameter that must be a number and start withnumber-
./something/(public|p)
: Matches all routes under the/something
path with a path parameter that must be eitherpublic
orp
.