import { setClient } from '../actions'

/**
 * Ehhhhhhhhhhhhhh... Needs a little more cleanup, but in general
 * we kinda dumbly trust a user from localStorage if there is no user
 * in state.
 *
 * It is entirely possible that I suck at branching logic
 */

const isUser = user => user.id && user.email && user.token
const isAdmin = user => isUser(user) && (user.id === 1 || user.email === 'rasmus@designit.com')

const checkStoredAuthorization = (dispatch, verifyUser) => {
  const storedUser = localStorage.getItem('user')

  if (storedUser) {
    const user = JSON.parse(storedUser)

    // We need a better system here to verify a user from localStorage
    // is who they say they are, and additionally verify the JWT token

    // const created = Math.round(createdDate.getTime() / 1000)
    // const ttl = 1209600
    // const expiry = created + ttl

    // if the user has expired return false
    // if (created > expiry) return false

    dispatch(setClient(user))
    return verifyUser(user)
  }

  return false
}

export function isAuthorizedUser(user, dispatch) {
  if (!isUser(user))
    return checkStoredAuthorization(dispatch, isUser)
  return true
}

export function isAuthorizedAdmin(user, dispatch) {
  if (!isUser(user))
    return checkStoredAuthorization(dispatch, isAdmin)
  return isAdmin(user)
}

import { setClient } from '../actions'

/**
 * Ehhhhhhhhhhhhhh... Needs a little more cleanup, but in general
 * we kinda dumbly trust a user from localStorage if there is no user
 * in state.
 *
 * It is entirely possible that I suck at branching logic
 */

const isUser = user => user.id && user.email && user.token
const isAdmin = user => isUser(user) && (user.email === 'bruce@designitcontoso.onmicrosoft.com' || user.email === 'roodmin@designitcontoso.onmicrosoft.com')

const checkStoredAuthorization = (dispatch, verifyUser) => {
  const storedUser = localStorage.getItem('user')

  if (storedUser) {
    const user = JSON.parse(storedUser)

    // We need a better system here to verify a user from localStorage
    // is who they say they are, and additionally verify the JWT token

    // const created = Math.round(createdDate.getTime() / 1000)
    // const ttl = 1209600
    // const expiry = created + ttl

    // if the user has expired return false
    // if (created > expiry) return false

    dispatch(setClient(user))
    return verifyUser(user)
  }

  return false
}

export function isAuthorizedUser(user, dispatch) {
  if (!isUser(user))
    return checkStoredAuthorization(dispatch, isUser)
  return true
}

export function isAuthorizedAdmin(user, dispatch) {
  if (!isUser(user))
    return checkStoredAuthorization(dispatch, isAdmin)
  return isAdmin(user)
}

Change hard-coded email for admin users
Change hard-coded email for admin users
