zippie-api/src/passport.js

/*
 * Copyright (c) 2019 Zippie Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * @module zippie-api/Passport
 */

var context = null
var data = null

/**
 * Initialize Passport API by passing in vault instance.
 * @param {Vault} vault reference to vault
 */
export async function init (vault) {
  context = vault
  data = (await context.getUserData('passport')) || {}
}

/**
 * Get the users full name
 */
export function getFullName () { return data.fullname || '' }

/**
 * Set the users full name
 * @param {string} fullname Users' new name
 */
export async function setFullName (v) { await update({fullname: v}) }

/**
 * Get the users phone number
 */
export function getPhone () { return data.phone || '' }

/**
 * Set the users phone number
 * @param {string} phone Users' new phone number
 */
export async function setPhone (v) { await update({phone: v}) }

/**
 * Get the users email address
 */
export function getEmail () { return data.email || '' }

/**
 * Set the users email address
 * @param {string} email Users' new email
 */
export async function setEmail (v) { await update({email: v}) }

/**
 * Get the users encoded avatar image
 */
export function getAvatar () { return data.avatar || '' }

/**
 * Set the users encoded avatar image
 * @param {string} image Users' new avatar
 */
export async function setAvatar (v) { await update({avatar: v}) }

/**
 * Get the users preferred language
 */
export function getLanguage () { return data.language || 'en' }

/**
 * Set the users preferred language
 * @param {string} language Users' new language
 */
export async function setLanguage (v) { await update({language: v}) }

/**
 * Get all user passport properties
 */
export function props () {
  return JSON.parse(JSON.stringify(data))
}

/**
 * Update multiple user details
 * @param {object} props Properties to update and their values.
 */
export async function update (props) {
  if (!data) return Promise.reject('Passport API not initialized')
  data = Object.assign(data, props)
  return context.setUserData('passport', data)
}