diff --git a/README.md b/README.md index 55b5111..8570103 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ It is possible to customize the `default` preset or define your own Database pre NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_HOST="https://example.com" NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_NS="surrealdb" NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_DB="docs" +NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_SC="user" # For auth # user and pass separated by a colon diff --git a/playground/app.vue b/playground/app.vue index 73d7bf3..9b242bf 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -108,9 +108,6 @@ const { signin, signout, session, refreshInfo } = useSurrealAuth() interface UserCredentials { username?: string password?: string - SC: string } -const credentials = reactive({ - SC: 'user', -}) +const credentials = reactive({}) diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 801f479..8b7497b 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -9,6 +9,7 @@ export default defineNuxtConfig({ host: '', NS: '', DB: '', + SC: '', auth: '', }, }, diff --git a/src/module.ts b/src/module.ts index 349aefd..ec140e7 100644 --- a/src/module.ts +++ b/src/module.ts @@ -30,6 +30,7 @@ export default defineNuxtModule({ host: '', NS: '', DB: '', + SC: '', auth: '', }, }, diff --git a/src/runtime/composables/surreal-auth.ts b/src/runtime/composables/surreal-auth.ts index e7ff027..a185d51 100644 --- a/src/runtime/composables/surreal-auth.ts +++ b/src/runtime/composables/surreal-auth.ts @@ -1,4 +1,6 @@ import type { PublicRuntimeConfig } from 'nuxt/schema' +import { defu } from 'defu' + import type { RpcParams } from '../types' import type { UserSession } from '#surreal-auth' @@ -53,14 +55,18 @@ export function useSurrealAuth() { } // signin - async function signin(credentials: Omit[0], 'NS' | 'DB'>) { - const { NS, DB } = databases[authDatabase] - const { SC, ...crd } = credentials + async function signin(credentials: RpcParams[0]) { + const _credentials = defu( + credentials, + { + NS: databases[authDatabase].NS, + DB: databases[authDatabase].DB, + SC: databases[authDatabase].SC, + }, + ) + if (!_credentials.NS || !_credentials.DB || !_credentials.SC) throw createError({ statusCode: 500, message: 'Invalid database preset' }) const { result } = await $signin({ - ...crd, - NS, - DB, - SC, + ..._credentials, }, { database: authDatabase, token: false }) if (result) { token.value = result @@ -70,19 +76,23 @@ export function useSurrealAuth() { } // signup - async function signup(credentials: Omit[0], 'NS' | 'DB'>) { - const { NS, DB } = databases[authDatabase] - const { SC, ...crd } = credentials + async function signup(credentials: RpcParams[0]) { + const _credentials = defu( + credentials, + { + NS: databases[authDatabase].NS, + DB: databases[authDatabase].DB, + SC: databases[authDatabase].SC, + }, + ) + if (!_credentials.NS || !_credentials.DB || !_credentials.SC) throw createError({ statusCode: 500, message: 'Invalid database preset' }) const { result } = await $signup({ - ...crd, - NS, - DB, - SC, + ..._credentials, }, { database: authDatabase, token: false }) if (result) { token.value = result refreshCookie(cookieName) - await signin({ ...crd, SC }) + await signin(_credentials) await refreshInfo() } } diff --git a/src/runtime/types/index.d.ts b/src/runtime/types/index.d.ts index cf71d5d..28c1de0 100644 --- a/src/runtime/types/index.d.ts +++ b/src/runtime/types/index.d.ts @@ -20,6 +20,7 @@ export interface DatabasePreset { host?: string NS?: string DB?: string + SC?: string auth?: AuthToken }