How to redirect to custom url after portal user successful login. Currently the portal user redirect to /my url of odoo. I have created custom module to redirect user to specific url after login, but the odoo does not invoke the custom controller it still used the odoo default login functionality. Following is my code please give me solution to achieve the functionality.
import logging
from odoo import http
from odoo.http import request
from odoo.exceptions import AccessDenied
_logger = logging.getLogger(__name__)
class CustomLoginRedirectController(http.Controller):
# Override the default login route
@http.route('/web/login', type='http', auth='none', website=True, csrf=False)
def custom_web_login(self, redirect=None, **kw):
"""
Override the default login behavior to redirect users to a custom URL after login.
"""
_logger.info("Custom login controller invoked")
# Ensure we have a valid database selected
http.ensure_db()
# If the user is already logged in, redirect to a custom URL
if request.session.uid:
_logger.info("User already logged in, redirecting to /buyer_search")
# Custom redirect URL after login
return request.redirect('/buyer_search') # Replace with your custom URL
# Values for the login page template
values = request.params.copy()
values['login_success'] = False
try:
values['databases'] = http.db_list()
except AccessDenied:
values['databases'] = None
if request.httprequest.method == 'POST':
login = request.params.get('login')
password = request.params.get('password')
_logger.info("POST request received. Attempting to authenticate user %s", login)
try:
# Authenticate the user using the credentials provided in the POST request
if request.session.authenticate(request.db, login, password):
values['login_success'] = True
_logger.info("User %s authenticated successfully. Redirecting to /buyer_search", login)
# Redirect to the custom URL after successful login
return request.redirect('/buyer_search') # Replace with your custom URL
else:
# Invalid login credentials
values['error'] = 'Invalid username or password'
_logger.warning("Invalid login attempt for user %s", login)
except AccessDenied as e:
values['error'] = str(e)
_logger.error("Access denied during login attempt for user %s: %s", login, str(e))
# Handle GET requests and display the original login page
_logger.info("Rendering the login page")
response = request.render('web.login', values)
response.headers['X-Frame-Options'] = 'DENY'
return response