EcomPassport

EcomPassport

new EcomPassport(storeIdopt, langopt, storageKeyopt, localStorageopt, documentopt)

Source:
Construct a new customer account instance object.
Examples
// Default instance
const ecomPassport = new EcomPassport()
// Optionally defining Store ID, lang and custom storage key
const storeId = 2000
const lang = 'en_us'
const storageKey = 'myPassportKey'
const customEcomPassport = new EcomPassport(storeId, lang, storageKey)
Parameters:
Name Type Attributes Default Description
storeId number <optional>
$ecomConfig.get('store_id') Preset Store ID number
lang string <optional>
$ecomConfig.get('lang') Snake case language code
storageKey string | null <optional>
ecomPassportClient Item key to persist account data
localStorage object <optional>
window.localStorage Local Storage interface
document object <optional>
window.document Object reference to document

Members

(static) ecomPassport.Constructor :function

Source:
See:
Construct a new account instance object.
Type:
  • function

(static) ecomPassport.customer :object

Source:
Customer account data following E-Com Plus customer object model.
Type:
  • object

(static) ecomPassport.document :object

Source:
Type:
  • object

(static) ecomPassport.lang :string

Source:
Instance language code.
Type:
  • string

(static) ecomPassport.localStorage :object

Source:
Type:
  • object

(static) ecomPassport.session :object

Source:
Passport authentication session object.
Type:
  • object

(static) ecomPassport.sessionId :object

Source:
Passport random 32 chars session ID.
Type:
  • object

(static) ecomPassport.storageKey :string|null

Source:
Item key to persist JSON EcomPassport#customer with localStorage and cookie name to persist EcomPassport#session.
Type:
  • string | null

(static) ecomPassport.storeId :number

Source:
Respective Store ID number.
Type:
  • number

Methods

checkAuthorization() → {boolean}

Source:
Check session auth level (does not validate token).
Example
ecomPassport.checkAuthorization()
Returns:
Type
boolean

checkLogin() → {boolean}

Source:
Check if customer is identified (does not validate authentication).
Example
ecomPassport.checkLogin()
Returns:
Type
boolean

checkVerification() → {boolean}

Source:
Check session biggest auth level (does not validate token).
Example
ecomPassport.checkVerification()
Returns:
Type
boolean

fetchLogin(email, docNumberopt) → {Promise.<(session|error)>}

Source:
Try to identify/login user with email and doc number.
Example
ecomPassport.fetchLogin('[email protected]', '1234567890')
Parameters:
Name Type Attributes Description
email string Customer main email address
docNumber string <optional>
Customer document number
Returns:
Type
Promise.<(session|error)>

fetchOauthProfile() → {Promise.<(session|error)>}

Source:
Try get session and account data after social login.
Example
ecomPassport.fetchOauthProfile().then(() => {
  console.log(ecomPassport.checkVerification() === true)
})
Returns:
Type
Promise.<(session|error)>

fetchOauthProviders(canAppendIframeopt) → {Promise.<(data|error)>}

Source:
Fetch Passport API to list OAuth providers and start social login flux.
Example
ecomPassport.fetchOauthProviders()
Parameters:
Name Type Attributes Default Description
canAppendIframe boolean <optional>
true Whether iframe should be appended to body to start oauth flux
Returns:
Type
Promise.<(data|error)>

fetchOrder(orderId) → {Promise.<(data|error)>}

Source:
Fetch order by ID from Passport API (if authorized) or Store API (public).
Example
ecomPassport.fetchOrder(orderId).then(order => {
  console.log(order._id)
})
Parameters:
Name Type Description
orderId string Object ID (`_id`) of the order to be fetched
Returns:
Type
Promise.<(data|error)>

fetchOrdersList(fromopt, sizeopt) → {Promise.<(orders|error)>}

Source:
Fetch each order from customer's orders list.
Example
ecomPassport.fetchOrdersList().then(orders => {
  orders.forEach(order => {
    console.log(order.number)
  })
})
Parameters:
Name Type Attributes Default Description
from number <optional>
0 Offset (start from) for pagination
size number <optional>
10 Limit of orders to fetch and list
Returns:
Type
Promise.<(orders|error)>

getCustomer() → {object}

Source:
Get current customer account object.
Example
const customer = ecomPassport.getCustomer()
console.log(customer.main_email)
Returns:
Type
object

getCustomerName() → {string}

Source:
Get nickname or given name from current customer account data.
Example
ecomPassport.getCustomerName()
Returns:
Type
string

loadStoredSession() → {self}

Source:
Try to load session object from cookie and customer data from localStorage.
Example
ecomPassport.loadStoredSession()
Returns:
Type
self

logout(canSaveopt) → {self}

Source:
Reset session and customer account object.
Example
ecomPassport.logout()
Parameters:
Name Type Attributes Default Description
canSave boolean <optional>
true Save (reset) cookie and localStorage after logout
Returns:
Type
self

popupLogin(canSkipLoginopt, baseUriopt) → {window|null}

Source:
Open a new popup to default E-Com Plus Passport login window.
Example
ecomPassport.popupLogin()
Parameters:
Name Type Attributes Default Description
canSkipLogin boolean <optional>
Whether customer can skip login and continue as guest
baseUri string <optional>
'https://passport.e-com.plus/v1/' Passport API base URI
Returns:
Type
window | null
Source:
Open a new popup to received URL for OAuth and try to fetch profile on callback or popup closed.
Example
ecomPassport.popupOauthLink(facebookOauthLink)
Parameters:
Name Type Description
url string Popup URL (OAuth provider link)
Returns:
Type
window | null

requestApi(url, method, dataopt) → {Promise.<(response|error)>}

Source:
Send request to E-Com Plus Passport API.
Example
ecomPassport.requestApi('/me.json', 'patch', { orders })
  .then(({ data }) => {
    console.log(data)
  })
Parameters:
Name Type Attributes Description
url string Passport API endpoint
method string Request HTTP method
data object <optional>
Request body
Returns:
Type
Promise.<(response|error)>

setCustomer(customer, canSaveopt) → {self}

Source:
Set (assign) customer account object and save to local storage.
Example
ecomPassport.setCustomer(customer)
Parameters:
Name Type Attributes Default Description
customer object Customer data (can be partial)
canSave boolean <optional>
true Save to localStorage
Returns:
Type
self

setSession(session, canSaveopt) → {self}

Source:
Set session object and save to cookie.
Example
ecomPassport.setSession(session)
Parameters:
Name Type Attributes Default Description
session object Passport session object
canSave boolean <optional>
true Save to cookie
Returns:
Type
self

Events

change

Source:
Properties:
Name Type Description
customer object Customer account data
Type:
  • object
Example
ecomCart.on('change', ({ customer }) => { console.log(customer._id) })

login

Source:
Example
ecomPassport.on('login', () => {
  console.log(ecomPassport.checkLogin()) // true
})

logout

Source:
Example
ecomPassport.on('logout', () => {
  console.log(ecomPassport.checkLogin()) // false
})