global current_user which defines a global property linked to the client token identity.
type User is the model if your user, you are free to change it, that can be done later on thanks to migrations.
access policy author_has_full_access & using (.author ?= global current_user); defines the policy for the users to only have access to their own BlogPost.
// dbschema/default.esdlusing extension auth;module default { global current_user := ( assert_single(( select User { id, name } filter .identity = global ext::auth::ClientTokenIdentity )) ); type User { required name: str; required identity: ext::auth::Identity; } type BlogPost { property content: str { default := 'My blog post content.'; }; property title: str { default := 'My blog post'; }; required author: User; access policy author_has_full_access allow all using (.author ?= global current_user); access policy others_read_only allow select; }}
You can edit this schema while your server is running and accept the prompted messages to auto-migrate.
If you are doing these edits while the server is off, you can run edgedb migration create and edgedb migrate.
Setup EdgeDB auth in your server
You will need to enable auth providers on your EdgeDB server.
That can be done in the EdgeDB tab through your DevTools.
Browse your database to Auth Admin and specify:
auth_signing_key
allowed_redirect_urls
You must also enable some providers. You can start with Email + Password for instance.
If you enable required_verification, you will need to configure a SMTP server for you EdgeDB instance.
You can find further instructions on how to use Mailtrap locally to try this feature here.
Do not forget these steps must also be performed on your production environment.
Use authentication components on the client
As you enabled auth in your config, the module injected these components in your project:
The authentication process does use a cookie called edgedb-auth-token.
On the server, if you want to authenticate your requests to the database for the current user, you only have to pass the current request object to composables:
export default defineEventHandler(async (req) => { // Will throw an error, as you cannot delete a BlogPost without being the author const { deleteBlogPost } = useEdgeDbQueries() await deleteBlogPost({ blogpost_id: id }) // Success const { deleteBlogPost: deleteBlogPostAuthenticated } = useEdgeDbQueries(req) await deleteBlogPostAuthenticated({ blogpost_id: id }) return { id }})
Other authentication solutions
EdgeDDB Auth is a great solution, but eventually your app may require more features later.
Do not forget that EdgeDB can also be used as a database. You can build your own auth or use existing solutions like:
# Your EdgeDB instance auth extension base URLNUXT_EDGEDB_AUTH_BASE_URL=http://localhost:10702/db/edgedb/ext/auth/# Your EdgeDB instance OAuth callback URLNUXT_EDGEDB_OAUTH_CALLBACK=http://localhost:10702/db/edgedb/ext/auth/callback# Your app callback pageNUXT_EDGEDB_OAUTH_REDIRECT_URL=http://localhost:3000/auth/callback# Your app app reset password URL (receiving the token from the forgot password email)NUXT_EDGEDB_AUTH_RESET_PASSWORD_URL=http://localhost:3000/auth/reset-password# Your app email verify url (receiving the token from email verify feature)NUXT_EDGEDB_AUTH_VERIFY_REDIRECT_URL=http://localhost:3000/auth/verify
Going further with authentication
EdgeDB Auth only provides the bare minimal identity feature for authentication.
In the code example provided for authentication setup, a User type goes along with the Identity interface.
If you want to fill User with other attributes upon registration, you will have to implement this behavior yourself.
If you want to resolve data from OAuth providers, you can use the Nitro hook called edgedb:auth:callback from a Nitro plugin.
// server/plugins/auth.tsexport default defineNitroPlugin((app) => { app.hooks.hook('edgedb:auth:callback', (data) => { const { code, verifier, codeExchangeUrl, codeExchangeResponseData, } = data // codeExchangeResponseData contains the OAuth token from the provider. // ... implement your own authentication logic. })})
Production
If you want to get out of development and deploy your database to prodution, you must follow the EdgeDB guides.
EdgeDB is an open-source database that is designed to be self-hosted.
However, they also offer a Cloud, which is fully compatible with this module thanks to environment variables.
If you want to customize the [DSN] used by the composables, you can use the environment variables provided by the module:
If you want to use the env variables, you have to specify ALL of them, otherwise the client will fallback on default values.
Q&A
Will my database client be exposed in userland?
No, useEdgeDb and useEdgeDbQueries are only available in the server/ context of Nuxt.
You can, as an opt-in feature, import queries from @dbschema/queries on the client.
You will need to provide these queries with a client from createClient().
<script setup lang="ts">import { createClient } from 'edgedb'import { getUser } from '@dbschema/queries'const client = createClient()const user = await getUser(client, 42)</script>
You can also, still as an opt-in feature, import the query builder to the client.
I guess that can be useful for a super-admin/internal dashboard, but use it at your own risks in terms of security access.
<script setup lang="ts">import e, { type $infer } from '#edgedb/builder'const query = e.select(e.Movie, () => ({ id: true, title: true }))type result = $infer<typeof query>// ^ { id: string; title: string }[]</script>
Be careful with these imports, as if you import wrong queries, you might end up with write operations available to the client, potentially damaging your database.
How do I run my migrations in production?
Clone your Nuxt project on your production environment
Ensure you have EdgeDB CLI installed on the server
Add edgedb migrate --quiet to your CLI script
Should I version generated files?
No, as they are generated with your Nuxt client, you should add them to your .gitignore
You must change these paths accordingly if you change the **Dir options.
Is HMR for my database schema really safe?
Well, it depends on when you want to use it.
I would suggest keeping watchPrompt enabled while you casually dev on your project.
That will prevent from running any unwanted migration, and will only prompt when you add new things to your schemas.
If you want to go fast and know what you are doing, you can set watchPrompt to false, and profit from automatic migration creation and applying on any change on your schemas.
If you do not want any of these features, just set watch to false and feel safe about changes applied to your development database.
HMR on your database obviously has NO effect in production context.
Why the name isn't nuxt-edgedb
Because that handle is already taken on NPM.
It seem to be taken by ohmree, but the package seem inactive.
If anyone happens to know him, I would be happy to get in touch with him!
Contributions
There is still plenty of great features to build for this integration.
I would be happy to receive and review any Pull Request.
Development
# Install dependenciesnpm install# Generate type stubsnpm run dev:prepare# Develop with the playgroundnpm run dev# Build the playgroundnpm run dev:build# Run ESLintnpm run lint# Run Vitestnpm run testnpm run test:watch# Release new versionnpm run release