All files / src/lobby index.ts

0% Statements 0/11
0% Branches 0/2
0% Functions 0/2
0% Lines 0/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                                                                                                               
import * as did from '../did'
import * as ucan from '../ucan'
import * as ucanInternal from '../ucan/internal'
import { api } from '../common'
import { setup } from '../setup/internal'
 
export * from './username'
 
 
/**
 * Create a user account.
 */
export async function createAccount(
  userProps: {
    email: string
    username: string
  }
): Promise<{ success: boolean }> {
  const apiEndpoint = setup.endpoints.api
 
  const jwt = ucan.encode(await ucan.build({
    audience: await api.did(),
    issuer: await did.ucan(),
  }))
 
  const response = await fetch(`${apiEndpoint}/user`, {
    method: 'PUT',
    headers: {
      'authorization': `Bearer ${jwt}`,
      'content-type': 'application/json'
    },
    body: JSON.stringify(userProps)
  })
 
  return {
    success: response.status < 300
  }
}
 
 
/**
 * Ask the fission server to send another verification email to the
 * user currently logged in.
 * 
 * Throws if the user is not logged in.
 */
export async function resendVerificationEmail(): Promise<{ success: boolean }> {
  const apiEndpoint = setup.endpoints.api
 
  const localUcan = await ucanInternal.lookupFilesystemUcan("*")
  if (localUcan === null) {
    throw "Could not find your local UCAN"
  }
 
  const jwt = await ucan.build({
    audience: await api.did(),
    issuer: await did.ucan(),
    proof: localUcan, 
    potency: null
  })
 
  const response = await fetch(`${apiEndpoint}/user/email/resend`, {
    method: 'POST',
    headers: {
      'authorization': `Bearer ${jwt}`
    }
  })
  return {
    success: response.status < 300
  }
}