0

Sign in

by
Published Apr 10, 2023
Script surrealdb Verified

The script

Submitted by hugo989 Bun
Verified 14 days ago
1
import { Surreal } from "surrealdb@1";
2

3
/**
4
 * @returns The authenication token.
5
 */
6
export async function main(
7
  url: string,
8
  user: string,
9
  password: string,
10
  namespace?: string,
11
  database?: string,
12
  scope?: string,
13
) {
14
  const client = new Surreal();
15
  await client.connect(url);
16
  // surrealdb infers the auth level (root / namespace / database / scope) from
17
  // which keys are present, so only include the ones actually provided —
18
  // passing `scope: undefined` etc. makes it attempt scope auth and fail.
19
  const credentials: Record<string, string> = { username: user, password };
20
  if (namespace !== undefined) credentials.namespace = namespace;
21
  if (database !== undefined) credentials.database = database;
22
  if (scope !== undefined) credentials.scope = scope;
23
  return await client.signin(credentials as Parameters<typeof client.signin>[0]);
24
}
25

Other submissions
  • Submitted by adam186 Deno
    Created 406 days ago
    1
    import Surreal from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    /**
    4
     * @returns The authenication token.
    5
     */
    6
    export async function main(
    7
      url: string,
    8
      user: string,
    9
      password: string,
    10
      namespace?: string,
    11
      database?: string,
    12
      scope?: string,
    13
    ) {
    14
      const client = new Surreal(url);
    15
      return await client.signin({
    16
        user,
    17
        pass: password,
    18
        NS: namespace,
    19
        DB: database,
    20
        SC: scope,
    21
      });
    22
    }
    23