Contents
Problem Case:
The problem we are trying to solve here is, suppose you have multiple magento website setup on different domains on the same magento instance, and you want that if a customer login’s on any one our your site he is gets logged into all other sites as well.
Solution:
The solution for this is simple, whenever a user logs in on a website ‘A’, a script needs to notify website ‘B’,’C’, etc that user has logged in and then login needs to be performed in ‘B’,’C’, etc as well. This cannot be handled automatically because session is not shared across domains so this feature is not there by default. The solution for this is to use JSONP and perform a cross domain ajax request with the session id so that session gets shared accross domains.
Attached is the source code of the module
[dm]33[/dm]
Implementation
First Step: Magento has a default configuration option called Share Customer Accounts located at System -> Configuration -> Customer Configuration -> Account Sharing Options, change this to Global. What this means is that when a customer creates his new account, is shared for all your websites.
Second Step:
Next we need to write code to make the JSONP calls. We are using JSONP, since normal cross domain ajax calls are not allowed. For this we are using JQuery library , details of JSONP can be found here.
So below is the codes:
Step1: Include jQuery in Magento
First download jQuery and then place it inside /js/jquery folder, so path would be /js/jquery/jquery.js. Next create a javascript file called noconflict.js in the jquery folder (/js/jquery/noconflict.js) . Write this code inside noconflict.js file
1 |
|
Next open that page.xml layout file in your theme folder [app/design/frontend/default/YOUR_THEME/layout/page.xml or in your default magento theme] and place this code inside tag
1 2 3 4 |
|
Now open home page in your magento, and through firebug or chrome inspector, see if these two jquery files are included in your page.
Step2: Crossdomain ajax call
Next we need to write ajax call. The code for this should be written in header.phtml. You need to rewrite the base header.phtml in your theme [app/design/frontend/default/YOUR_THEME/page/html/header.phtml]
Here’s the added code for header.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
Here first we are getting all stores, then in a foreach loop we are getting the domain url of each store. Then we are checking if the current domain is not equal to the fetched url. Then we perform the jsonp call which will go to the other domain logincd Action and login the customer on the other domain. For this we are sharing the customer session id i.e. SID with other domains. We get the session id of the logged in customer with the code
1 |
|
We are appending session id in the url and this will be set in other domain and the customer will be logged in.
Now let us see how ‘jsonp’ request is different from normal ajax request.
1 |
|
In this line we we are appending the session id of the customer session of the current domain, and a ‘callback’ parameter which will be generated by jQuery.
It is a unique callback name for this request (something like json1268267816). It will be received at the other domain and it will treat the request as jsonp.
Mage::getSingleton(‘core/session’)->getLogincd() parameter is set so that this jsonp call is done only once and not every time a page is opened.
Step3: Overwrite the AccountController.php of Mage/Customer/controllers/
Next we need to overwrite AccountController.php file in our module [app/code/local/YOUR_NAMESPACE/MODULE_NAME/controllers/AccountController.php]. The AccountController.php file will have an action ‘logincd’ which will receive the ajax call and then do all the work required to make customer login in the domain. Here’s the code for AccountController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
This line designates the content to be in json format, encoded in the UTF-8 character encoding.
1 |
|
And then the success message has been set and sent back to the calling function.
1 |
|
this will send back the response to the requesting ajax call as jsonp.
This is all that is required to make cross domain login work. The cross domain logout will work automatically in the current case.
Revisions
- April 25, 2017 @ 11:24:11 [Current Revision] by Sharing Solution
- April 25, 2017 @ 11:24:11 by Sharing Solution
I have been reading out many of your articles and i must say pretty clever stuff. I will make sure to bookmark your blog.
Thankyou such a lot of with respect to stating this great post. The country’s extremely a good choice for me. Awesome position!