Functionality:
When the user clicks the login button on the header from any page, then the login page will load as usual and after login, it will redirect to the same page where the customer clicked the login.
In this case, if there is no previous URL or direct Login, then the extension will redirect to the home page.
Code
Extend the function”Redirect in the LoginRegisterview for implementing the feature.
define(
'Vserve1.login_redirect.loginRedirect', [
'LoginRegister.Login.View',
'Configuration',
'Profile.Model',
'Utils',
'LiveOrder.Model',
'Backbone.FormView',
'Backbone.View',
'Backbone',
'Session',
'underscore'
],
function(
LoginRegisterLoginView,
Configuration,
ProfileModel,
Utils,
LiveOrderModel,
BackboneFormView,
BackboneView,
Backbone,
Session,
_
) {
'use strict';
return {
mountToApp: function mountToApp(container) {
console.log("test redirect");
const previousPageUrl = document.referrer;
console.log("Previously visited page URL", previousPageUrl)
_.extend(LoginRegisterLoginView.prototype, {
redirect: function(context, response) {
const url_options = Utils.parseUrlOptions(window.location.search);
const {
touchpoints
} = response;
const isPasswordReset = url_options.passwdret;
const self = this;
// Track Login Event
this.trackEvent(function() {
if (
!isPasswordReset &&
(url_options.is === 'checkout' || url_options.origin === 'checkout')
) {
self.refreshApplication(response);
} else {
// if we know from which touchpoint the user is coming from
if (url_options.origin && touchpoints[url_options.origin]) {
// we save the URL to that touchpoint
let url = touchpoints[url_options.origin];
// if there is an specific hash
if (url_options.origin_hash) {
// we add it to the URL as a fragment
url = Utils.addParamsToUrl(url, {
fragment: url_options.origin_hash
});
}
console.log("origin url", url)
window.location.href = url;
} else {
// We've got to disable passwordProtectedSite feature if customer registration is disabled.
if (
SC.CONFIGURATION.getRegistrationType() !== 'disabled' &&
SC.ENVIRONMENT.siteSettings.siteloginrequired === 'T'
) {
if (previousPageUrl) {
window.location.href = previousPageUrl;
} else {
window.location.href = touchpoints.home;;
}
} else {
// otherwise we need to take it to the customer center
if (previousPageUrl) {
window.location.href = previousPageUrl;
} else {
window.location.href = touchpoints.home;;
}
}
}
}
});
},
})
}
};
});