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 componentconst email = this.auth.customer?.email;// Inside pageconst email = this.auth.customer?.email;// Inside other fileimport theme from '@/theme';const email = theme.auth.customer?.email;
typescript// Inside componentconst email = this.auth.customer?.email;// Inside pageconst email = this.auth.customer?.email;// Inside other fileimport 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):
typescriptinterface 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;}
typescriptinterface 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:
typescriptif (!this.auth.customer) {// not logged in}
typescriptif (!this.auth.customer) {// not logged in}
Checking customer tags:
typescriptisVip() {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;}
typescriptisVip() {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.tsimport {BaseCustomer} from 'salvo-ts';export default class Customer extends BaseCustomer {isWholesale(): boolean {return this.tags.indexOf('WHOLESALE') > -1;}}// usageconst wholesale = this.auth.customer?.isWholesale() || false;
typescript// theme/Customer.tsimport {BaseCustomer} from 'salvo-ts';export default class Customer extends BaseCustomer {isWholesale(): boolean {return this.tags.indexOf('WHOLESALE') > -1;}}// usageconst wholesale = this.auth.customer?.isWholesale() || false;