--- displayed_sidebar: jsClientSidebar slug: /references/js-client/AuthResource --- import ParameterTypes from "@site/src/components/ParameterTypes" # AuthResource This class is used to send requests to [Store Auth API Routes](https://docs.medusajs.com/api/store#auth). All its method are available in the JS Client under the `medusa.auth` property. The methods in this class allows you to manage a customer's session, such as login or log out. You can send authenticated requests for a customer either using the Cookie header or using the JWT Token. When you log the customer in using the [authenticate](AuthResource.mdx#authenticate) method, the JS client will automatically attach the cookie header in all subsequent requests. Related Guide: [How to implement customer profiles in your storefront](https://docs.medusajs.com/modules/customers/storefront/implement-customer-profiles). ## Methods ### authenticate Authenticate a customer using their email and password. If the customer is authenticated successfully, the cookie is automatically attached to subsequent requests sent with the JS Client. #### Example ```ts import Medusa from "@medusajs/medusa-js" const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) medusa.auth .authenticate({ email: "user@example.com", password: "user@example.com", }) .then(({ customer }) => { console.log(customer.id) }) ``` #### Parameters `", "description": "Custom headers to attach to the request.", "optional": false, "defaultValue": "{}", "expandable": false, "children": [] } ]} /> #### Returns `", "description": "An optional key-value map with additional details", "optional": false, "defaultValue": "", "expandable": false, "children": [] }, { "name": "orders", "type": "[Order](../internal/classes/internal.Order.mdx)[]", "description": "The details of the orders this customer placed.", "optional": false, "defaultValue": "", "expandable": true, "children": [] }, { "name": "phone", "type": "`string`", "description": "The customer's phone number", "optional": false, "defaultValue": "", "expandable": false, "children": [] }, { "name": "shipping_addresses", "type": "[Address](../internal/classes/internal.Address.mdx)[]", "description": "The details of the shipping addresses associated with the customer.", "optional": false, "defaultValue": "", "expandable": true, "children": [] }, { "name": "updated_at", "type": "`Date`", "description": "The date with timezone at which the resource was updated.", "optional": false, "defaultValue": "", "expandable": false, "children": [] } ] } ] } ] } ]} /> ___ ### deleteSession Log out the customer and remove their authentication session. This method requires [customer authentication](AuthResource.mdx#authenticate). #### Example ```ts import Medusa from "@medusajs/medusa-js" const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) medusa.auth.deleteSession().then(() => { // customer logged out successfully }) ``` #### Parameters `", "description": "Custom headers to attach to the request.", "optional": false, "defaultValue": "{}", "expandable": false, "children": [] } ]} /> #### Returns ___ ### exists Check if the email is already used by another registered customer. Can be used to validate a new customer's email. #### Example ```ts import Medusa from "@medusajs/medusa-js" const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) medusa.auth.exists("user@example.com") ``` #### Parameters `", "description": "Custom headers to attach to the request.", "optional": false, "defaultValue": "{}", "expandable": false, "children": [] } ]} /> #### Returns ___ ### getSession Retrieve the details of the logged-in customer. Can also be used to check if there is an authenticated customer. This method requires [customer authentication](AuthResource.mdx#authenticate). #### Example ```ts import Medusa from "@medusajs/medusa-js" const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) // must be previously logged medusa.auth.getSession().then(({ customer }) => { console.log(customer.id) }) ``` #### Parameters `", "description": "Custom headers to attach to the request.", "optional": false, "defaultValue": "{}", "expandable": false, "children": [] } ]} /> #### Returns `", "description": "An optional key-value map with additional details", "optional": false, "defaultValue": "", "expandable": false, "children": [] }, { "name": "orders", "type": "[Order](../internal/classes/internal.Order.mdx)[]", "description": "The details of the orders this customer placed.", "optional": false, "defaultValue": "", "expandable": true, "children": [] }, { "name": "phone", "type": "`string`", "description": "The customer's phone number", "optional": false, "defaultValue": "", "expandable": false, "children": [] }, { "name": "shipping_addresses", "type": "[Address](../internal/classes/internal.Address.mdx)[]", "description": "The details of the shipping addresses associated with the customer.", "optional": false, "defaultValue": "", "expandable": true, "children": [] }, { "name": "updated_at", "type": "`Date`", "description": "The date with timezone at which the resource was updated.", "optional": false, "defaultValue": "", "expandable": false, "children": [] } ] } ] } ] } ]} /> ___ ### getToken Authenticate the customer and retrieve a JWT token to use for subsequent authenticated requests. #### Example ```ts import Medusa from "@medusajs/medusa-js" const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) medusa.auth .getToken({ email: "user@example.com", password: "supersecret", }) .then(({ access_token }) => { console.log(access_token) }) ``` #### Parameters `", "description": "Custom headers to attach to the request.", "optional": false, "defaultValue": "{}", "expandable": false, "children": [] } ]} /> #### Returns