A minimal example demonstrating authentication in ObjectStack using @objectstack/plugin-auth.
This example shows how to:
- ✅ Set up the
AuthPluginwithObjectKernel - ✅ Configure authentication endpoints
- ✅ Use the ObjectStack client for authentication
- ✅ Register new users
- ✅ Login and logout
- ✅ Manage user sessions
- ✅ Request password resets
cd examples/minimal-auth
pnpm installCreate a .env file:
# Optional: Use a custom auth secret (recommended for production)
AUTH_SECRET=your-super-secret-key-min-32-chars
# Optional: Configure OAuth providers
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secretNote: If
AUTH_SECRETis not set, a development secret will be used automatically.
pnpm devThe server will start on http://localhost:3000 with the following endpoints:
POST /api/v1/auth/sign-up/email- Register new userPOST /api/v1/auth/sign-in/email- LoginPOST /api/v1/auth/sign-out- LogoutGET /api/v1/auth/get-session- Get current sessionPOST /api/v1/auth/forget-password- Request password resetPOST /api/v1/auth/reset-password- Reset password- And more...
pnpm testThis will:
- Register a new user
- Logout
- Login again
- Get the current session
- Test password reset flow
pnpm tsx src/test-discovery.tsThis test demonstrates how the auth service automatically appears in the API discovery response when plugin-auth is registered. Before the plugin is registered, discovery.services.auth.status is "unavailable". After registration, it becomes "available" with the proper route information.
import { ObjectStackClient } from '@objectstack/client';
const client = new ObjectStackClient({
baseUrl: 'http://localhost:3000'
});
// Register
await client.auth.register({
email: 'user@example.com',
password: 'SecurePassword123!',
name: 'John Doe'
});
// Login (auto-sets token)
await client.auth.login({
type: 'email',
email: 'user@example.com',
password: 'SecurePassword123!'
});
// Get current session
const session = await client.auth.me();
// Logout
await client.auth.logout();# Register
curl -X POST http://localhost:3000/api/v1/auth/sign-up/email \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"SecurePassword123!","name":"John Doe"}'
# Login
curl -X POST http://localhost:3000/api/v1/auth/sign-in/email \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"SecurePassword123!"}'
# Get session
curl http://localhost:3000/api/v1/auth/get-session \
-H "Authorization: Bearer YOUR_TOKEN"minimal-auth/
├── src/
│ ├── server.ts # Server setup with AuthPlugin
│ ├── test-auth.ts # Authentication flow test
│ └── test-discovery.ts # Discovery API test (dynamic service detection)
├── package.json
└── README.md
ObjectStack features a dynamic service discovery system that automatically reflects which plugins are registered. This is particularly useful for clients that need to adapt their UI or behavior based on available services.
Discovery Response Without Auth Plugin:
{
"services": {
"auth": {
"enabled": false,
"status": "unavailable",
"message": "Install plugin-auth to enable"
}
}
}Discovery Response With Auth Plugin:
{
"services": {
"auth": {
"enabled": true,
"status": "available",
"route": "/api/v1/auth",
"provider": "plugin-auth"
}
},
"endpoints": {
"auth": "/api/v1/auth"
}
}Clients can use this to check service availability:
const discovery = await client.getDiscovery();
if (discovery.services.auth?.enabled) {
// Auth is available - show login UI
await client.auth.login({ ... });
} else {
// Auth not available - hide login UI
console.log(discovery.services.auth?.message);
}See src/server.ts for examples of enabling advanced features:
new AuthPlugin({
secret: process.env.AUTH_SECRET,
baseUrl: 'http://localhost:3000',
providers: [
{
id: 'google',
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}
]
})new AuthPlugin({
secret: process.env.AUTH_SECRET,
baseUrl: 'http://localhost:3000',
plugins: {
organization: true, // Multi-tenant support
twoFactor: true, // 2FA
passkeys: true, // WebAuthn/Passkeys
magicLink: true, // Passwordless auth
}
})- See the Authentication Guide for complete documentation
- Explore the Todo App example for a full application with auth
- Check the CRM App example for enterprise features
Apache-2.0 © ObjectStack