Customers & Sessions
Nimbu.Customer represents customer accounts. It extends Nimbu.Object.
Create A Customer
js
const customer = new Nimbu.Customer({
email: '[email protected]',
firstname: 'Ada',
lastname: 'Lovelace'
});
await customer.save();Sign Up
js
const customer = await Nimbu.Customer.signUp('[email protected]', 'secret', {
email: '[email protected]',
firstname: 'Ada',
lastname: 'Lovelace'
});Successful signup makes the customer current.
Log In
js
try {
const customer = await Nimbu.Customer.logIn('[email protected]', 'secret');
console.log(customer.id);
} catch (error) {
console.error(error.message);
}Successful login stores the current customer session.
Current Customer
js
const current = Nimbu.Customer.current();
if (current && current.authenticated()) {
console.log(current.get('email'));
}Initialize the SDK before reading the current customer in browser or external usage:
js
await Nimbu.initialize(accessToken);
const current = Nimbu.Customer.current();Become A Customer Session
Use become() when you already have a customer session token.
js
const customer = await Nimbu.Customer.become(sessionToken, {
useACL: true
});Log Out
js
await Nimbu.Customer.logOut();This clears the current customer from SDK storage.
Password Reset
js
await Nimbu.Customer.requestPasswordReset('[email protected]', {
useACL: true
});Customer Queries
js
const customer = await new Nimbu.Query(Nimbu.Customer)
.equalTo('email', '[email protected]')
.first();You can query customers as a string too, but the class form returns Nimbu.Customer instances.
Roles
js
const customer = await new Nimbu.Query(Nimbu.Customer).get(customerId);
const role = await new Nimbu.Query(Nimbu.Role).get(roleId);
role.getCustomers().add(customer);
await role.save();