{"id":1734,"date":"2021-04-26T12:12:42","date_gmt":"2021-04-26T12:12:42","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/login-customer-programmatically-without-password-in-magento-2\/"},"modified":"2025-07-16T17:58:43","modified_gmt":"2025-07-16T12:28:43","slug":"login-customer-programmatically-without-password-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/login-customer-programmatically-without-password-in-magento-2\/","title":{"rendered":"How to Login Customer Programmatically Without Password in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0allows customers to log in from the store\u2019s frontend, and the admin can also handle customers from the backend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But, what if you want your customers to log in to the Magento 2 store using only the Email ID?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below solution allows facilitating&nbsp;<em><strong>login customer programmatically without password in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use this login method if you offer&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-extensions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento extensions<\/a>&nbsp;and want to show the demo store where the users can directly log in without having to remember a password.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, if you are developing a login module using which the customer can log in with just a click or by only entering the Email ID, you may find this solution helpful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In all these scenarios, offering an easy login where users can have quick access to the store only contributes to a better user experience and thus benefits your business!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Login Customer Programmatically Without Password in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code in your controller file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Controller;\nuse Magento\\Customer\\Model\\CustomerFactory;\nuse Magento\\Customer\\Model\\Session;\nprotected $session;\nprotected $customerFactory;\npublic function __construct(\n    Session $session,\n    CustomerFactory $customerFactory\n)\n{\n    $this->session = $session;\n    $this->customerFactory = $customerFactory;\n}\n$customer = $this->customerFactory->create();\n$loadCustomer = $customer->loadByEmail($email);\n$this->session->setCustomerAsLoggedIn($loadCustomer);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the $email variable, enter the email id of the customer you want to log in.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case you are developing any third-party application, you can also use&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API<\/a>&nbsp;to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-customer-token\/\" target=\"_blank\" rel=\"noreferrer noopener\">get customer token<\/a>&nbsp;for login purpose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0allows customers to log in from the store\u2019s frontend, and the admin can also handle customers from the backend. But, what if you want&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1734","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1734"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1734\/revisions"}],"predecessor-version":[{"id":18100,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1734\/revisions\/18100"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}