A small TypeScript package for Logto-based auth: JWT verification against Logto's JWKS, Express middleware, React hooks and a guest mode.
It collects the Logto setup that small apps tend to repeat: verify access tokens on an Express API, read the signed-in user in React, and let visitors try a few actions as a guest before signing in.
The package is not on the npm registry. The built dist/ folder is committed, so you can install it straight from GitHub:
npm install github:neurabytelabs/authIt installs under the name @mrsarac/auth. @logto/react (>= 3) and react (>= 18) are optional peer dependencies, needed only for the React exports.
import { createAuthMiddleware } from '@mrsarac/auth/middleware';
const auth = createAuthMiddleware({
endpoint: 'https://your-tenant.logto.app', // your Logto endpoint, without /oidc
audience: 'your-api-resource', // a string, or an array to accept several audiences
});
app.use('/api/protected', auth);
// handlers then read req.user (id, email, name, picture) and req.tokenPayloadcreateAuthMiddleware also takes an optional getDbUserId(logtoId) callback that sets req.user.dbUserId.
import { LogtoProvider } from '@logto/react';
import { AuthProvider, useAuth } from '@mrsarac/auth/react';
function App() {
return (
<LogtoProvider config={logtoConfig}>
<AuthProvider apiResource="https://api.example.com">
<MyApp />
</AuthProvider>
</LogtoProvider>
);
}
function MyComponent() {
const { isAuthenticated, user, login, logout, getAccessToken } = useAuth();
// ...
}npm ci
npm run build # tsup -> dist/ (CJS + ESM + type declarations)
npm run test:run # vitest
npm run typecheck # tsc --noEmitflowchart LR
Client[React app<br/>AuthProvider / useAuth] -->|Bearer access token| MW[createAuthMiddleware]
MW -->|jwtVerify via jose| JWKS[Logto endpoint/oidc/jwks]
MW -->|req.user| Handler[your route]
Exports by entry point:
| Entry | Exports |
|---|---|
@mrsarac/auth |
verifyToken, verifyTokenMultiAudience, createJWKS, createLogtoConfig, syncUser, getUserByLogtoId, authLogger, the middleware below, constants and types |
@mrsarac/auth/middleware |
createAuthMiddleware, authMiddleware, optionalAuthMiddleware, createGuestMiddleware, getGuestSession, isGuestMode |
@mrsarac/auth/react |
AuthProvider, useAuth, AuthContext, GuestModeProvider, useGuestMode |
- Token verification uses
josewith a remote JWKS at<endpoint>/oidc/jwks, cached per endpoint, with a 60-second clock tolerance by default. - Guest mode.
GuestModeProviderkeeps a guest session inlocalStorageand counts actions against a limit (default 3 actions, 24-hour expiry).createGuestMiddlewarereads a session ID from thex-guest-sessionheader and keeps sessions in memory. - User sync.
syncUserandgetUserByLogtoIdtake your ownquery(sql, params)function and expect auserstable withid,logto_id,email,nameandupdated_atcolumns (PostgreSQL placeholders). - Logging.
authLoggerprints warnings and errors by default; setAUTH_LOG_LEVEL(Node) orwindow.__AUTH_LOG_LEVEL__(browser) todebug,info,warn,errororsilent.
- Early version (0.1.0), no releases. Not published to any registry.
- On the current
main, 4 of 93 tests fail andnpm run typecheckreports 5 errors (all in test files), so CI is red. The build passes and the committeddist/matches it. - The prebuilt
authMiddlewareandoptionalAuthMiddlewarereadLOGTO_ENDPOINTandAPI_RESOURCE(orLOGTO_APP_ID) from the environment and fall back to the author's own Logto instance. PrefercreateAuthMiddlewarewith an explicit endpoint;createLogtoConfighas the same fallback. optionalAuthMiddlewareis currently identical toauthMiddleware: it still rejects requests without a token.- Guest sessions in
createGuestMiddlewarelive in an in-processMap: they are lost on restart, not shared between instances, and the middleware does not count actions itself. QuotaInfois only a type; there is no quota logic yet.
MIT. See LICENSE.