1. Setup
Install the module, nitro (the module runs on Nitro 3), a schema library and mongodb (for ObjectId in schemas):
pnpm add @sfxcode/nuxt-mongocamp-server nitro zod mongodbSwitch the embedded server on. The API then runs inside your app under /api/mongocamp-server, and every composable uses it:
export default defineNuxtConfig({
modules: ['@sfxcode/nuxt-mongocamp-server'],
compatibilityDate: '2026-11-01',
mongocamp: {
server: {
enabled: true,
config: {
CONNECTION_DATABASE: 'shop',
// the users of the tutorial; a real application keeps its users in MongoDB (the default `AUTH_HANDLER: 'mongo'`)
AUTH_HANDLER: 'static',
AUTH_USERS: JSON.stringify([
{ userId: 'ann', password: 'ann-secret', roles: ['shop'] },
{ userId: 'bob', password: 'bob-secret', roles: ['shop'] },
]),
AUTH_ROLES: JSON.stringify([
{ name: 'shop', isAdmin: false, collectionGrants: [
{ name: '*', read: true, write: true, administrate: false, grantType: 'COLLECTION' },
// step 11: the bucket of the avatars
{ name: 'avatars', read: true, write: true, administrate: false, grantType: 'BUCKET' },
] },
]),
},
},
},
})The two users ann and bob come from the configuration (AUTH_HANDLER: 'static'), so the tutorial needs no user management. A real app keeps its users in MongoDB, which is the default: on the first start the server creates admin with a random password and prints it to the log. See Authentication.
The connection uses the CONNECTION_* variables (default localhost:27017) or NUXT_MONGOCAMP_SERVER_CONNECTION_URI. Environment variables win over the config of nuxt.config.ts.
Log in
useMongocampAuth logs in and keeps the session in a cookie. The cookie goes along with server side rendering too, so pages render with the user's data:
const { login, logout, isLoggedIn } = useMongocampAuth()
const user = useMongocampUser()
const form = reactive({ user: 'ann', password: '' })
const failed = ref(false)
async function signIn() {
failed.value = false
try {
await login(form.user, form.password)
}
catch {
failed.value = true
}
}The app shows the lists only to a logged-in user:
<script setup lang="ts">
const { isLoggedIn } = useMongocampAuth()
</script>
<template>
<main>
<h1>Shop</h1>
<ShopLogin />
<!-- the lists need a session: rendered on the server when the request has one, else after the login -->
<template v-if="isLoggedIn">
<CustomerList />
<OrderList />
</template>
</main>
</template>Run pnpm dev and log in as ann with ann-secret.
