Skip to main content
Version: 0.1.x

Customer

obsolete: check how this is referenced now

The main way of working with the customer's login session in SALVO-TS is via the Auth singleton object.

You can access Auth in components or pages via this.auth. If you need to use it from elsewhere, import the Theme instance and access it it there:

typescript
// Inside component
const email = this.auth.customer?.email;
// Inside page
const email = this.auth.customer?.email;
// Inside other file
import theme from '@/theme';
const email = theme.auth.customer?.email;
typescript
// Inside component
const email = this.auth.customer?.email;
// Inside page
const email = this.auth.customer?.email;
// Inside other file
import theme from '@/theme';
const email = theme.auth.customer?.email;

this.auth.customer is an instance of null|Customer (it will be null if the customer is not logged in):

typescript
interface Customer {
id: number;
email: null|string;
phone: null|string;
first_name: string;
last_name: string;
name: string;
tags: string[];
has_account: boolean;
accepts_marketing: boolean;
orders_count: number;
tax_exempt: boolean;
total_spent: number;
}
typescript
interface Customer {
id: number;
email: null|string;
phone: null|string;
first_name: string;
last_name: string;
name: string;
tags: string[];
has_account: boolean;
accepts_marketing: boolean;
orders_count: number;
tax_exempt: boolean;
total_spent: number;
}

Examples

Checking if customer is logged in:

typescript
if (!this.auth.customer) {
// not logged in
}
typescript
if (!this.auth.customer) {
// not logged in
}

Checking customer tags:

typescript
isVip() {
const customer = this.customer.current;
if (!customer) {
return false;
}
const email = this.customer.current.email;
const isVip = this.customer.current.tags.indexOf('VIP') != -1;
return isVip;
}
typescript
isVip() {
const customer = this.customer.current;
if (!customer) {
return false;
}
const email = this.customer.current.email;
const isVip = this.customer.current.tags.indexOf('VIP') != -1;
return isVip;
}

Extending the Customer interface

You can extend the cart, for example to add new utilty functions, by editing the theme/Customer.ts file.

typescript
// theme/Customer.ts
import {BaseCustomer} from 'salvo-ts';
export default class Customer extends BaseCustomer {
isWholesale(): boolean {
return this.tags.indexOf('WHOLESALE') > -1;
}
}
// usage
const wholesale = this.auth.customer?.isWholesale() || false;
typescript
// theme/Customer.ts
import {BaseCustomer} from 'salvo-ts';
export default class Customer extends BaseCustomer {
isWholesale(): boolean {
return this.tags.indexOf('WHOLESALE') > -1;
}
}
// usage
const wholesale = this.auth.customer?.isWholesale() || false;