Skip to content

Commit

Permalink
feat(useSurrealAuth): SC param per database preset
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jun 2, 2024
1 parent 2ed09c2 commit a054de8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ const { signin, signout, session, refreshInfo } = useSurrealAuth()
interface UserCredentials {
username?: string
password?: string
SC: string
}
const credentials = reactive<UserCredentials>({
SC: 'user',
})
const credentials = reactive<UserCredentials>({})
</script>
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineNuxtConfig({
host: '',
NS: '',
DB: '',
SC: '',
auth: '',
},
},
Expand Down
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default defineNuxtModule<ModuleOptions>({
host: '',
NS: '',
DB: '',
SC: '',
auth: '',
},
},
Expand Down
40 changes: 25 additions & 15 deletions src/runtime/composables/surreal-auth.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -53,14 +55,18 @@ export function useSurrealAuth() {
}

// signin
async function signin(credentials: Omit<RpcParams<any, 'signin'>[0], 'NS' | 'DB'>) {
const { NS, DB } = databases[authDatabase]
const { SC, ...crd } = credentials
async function signin(credentials: RpcParams<any, 'signin'>[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<string>({
...crd,
NS,
DB,
SC,
..._credentials,
}, { database: authDatabase, token: false })
if (result) {
token.value = result
Expand All @@ -70,19 +76,23 @@ export function useSurrealAuth() {
}

// signup
async function signup(credentials: Omit<RpcParams<any, 'signup'>[0], 'NS' | 'DB'>) {
const { NS, DB } = databases[authDatabase]
const { SC, ...crd } = credentials
async function signup(credentials: RpcParams<any, 'signup'>[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<string>({
...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()
}
}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface DatabasePreset {
host?: string
NS?: string
DB?: string
SC?: string
auth?: AuthToken
}

Expand Down

0 comments on commit a054de8

Please sign in to comment.