types#UserDocument TypeScript Examples

The following examples show how to use types#UserDocument. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: user.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export async function findUser(
  db: Database,
  identity: PrivateKey | null
): Promise<UserDocument | null> {
  if (!identity) return null;

  const user = await getUserCollection(db).findOne({ publicKey: publicKeyHex(identity) });

  return user || null;
}
Example #2
Source File: user.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export async function getUser(
  db: Database,
  identity: PrivateKey | null,
  { creator, name, email }: Partial<UserDocument> = {}
): Promise<UserDocument | null> {
  const existing = await findUser(db, identity);

  if (!identity) {
    // create generic user identity to authorize remote etc.
    return null;
  }

  if (existing) {
    return existing;
  }

  if (identity && !existing) {
    const user = getUserCollection(db).create({
      creator,
      name,
      email,
      publicKey: publicKeyHex(identity) as string,
    });
    await user.save();

    return user;
  }

  return Promise.reject(new Error('Unable to create user'));
}
Example #3
Source File: util.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function getUserCollection(db: Database): Collection<UserDocument> {
  const collection = db.collection<UserDocument>('User');

  if (!collection) throw new Error('User collection not found.');

  return collection;
}
Example #4
Source File: init.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export async function init(rpcUrl: string): Promise<[DB, UserDocument | null, PrivateKey | null]> {
  const name = `${rpcUrl}`;

  const db = await initDb(name);
  const [user, identity] = await initIdentity(db);

  if (isLocalNode(rpcUrl)) {
    window.localStorage.setItem(LOCAL_NODE_DB_NAME, name);
  }

  return [db, user, identity];
}
Example #5
Source File: init.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export async function initIdentity(db: DB): Promise<[UserDocument | null, PrivateKey | null]> {
  const identity = getStoredPrivateKey();

  const user = await getUser(db, identity);

  return [user, identity];
}