You have to override Ajax login (root_dir\vendor\magento\module-customer\Controller\Ajax\Login.php) controller and login.js(vendor\magento\module-customer\view\frontend\web\js\action\login.js) for ajax login redirect and need to add your conditions accordingly.
Step 1. Create/edit Your_Company/Your_Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Controller\Ajax\Login" type="Your_Company\Your_Module\Controller\Ajax\Login" />
</config>
Step 2. Overridden controller will reside in Your_Company\Your_Module\Controller\Ajax\Login.php
Now copy and paste the code from parent Ajax\Login.php
in execute method you will see
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) {
$response['redirectUrl'] = $this->_redirect->success($redirectRoute);
$this->getAccountRedirect()->clearRedirectCookie();
}
Step 3. Now you have to insert your logic below this condition.
$response['redirectUrl'] = $this->_redirect->success($this->_url->getUrl("customer/account"));
This will set the response.redirectUrl for login.js(vendor\magento\module-customer\view\frontend\web\js\action\login.js) and here we got the real issue there is a code snippet you can check
if (redirectUrl) {
window.location.href = redirectUrl;
} else if (response.redirectUrl) {
window.location.href = response.redirectUrl;
} else {
location.reload();
}
You just have to change it to
if (response.redirectUrl) {
window.location.href = response.redirectUrl;
} else if (redirectUrl) {
window.location.href = redirectUrl;
} else {
location.reload();
}
To override login.js follow steps.
Step 4. Create file requirejs-config.js in You_Company\Your_Module\requirejs-config.js and write below code...
var config = {
map: {
'*': {
'Magento_Customer/js/action/login': 'YourCompany_YourModule/js/login'
}
}
};
Step 5.
Copy login.js from vendor\magento\module-customer\view\frontend\web\js\action\login.js to YourCompany\YourModule\view\frontend\web\js\login.js
That's all.