İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
2240 Görünümler

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




Avatar
Vazgeç
En İyi Yanıt

Hello,

By default, for a portal user, the redirection is to /my, so if you want to redirect a user according to your conditions, you can redirect the user to another URL like the code below, adjusting it according to your needs.

from odoo.addons.web.controllers.home import Home as WebHome
from odoo.addons.web.controllers.utils import is_user_internal

class Home(WebHome):

def _login_redirect(self, uid, redirect=None):
if not redirect and not is_user_internal(uid):
redirect = '/buyer_search' # Replace with your custom URL
return super()._login_redirect(uid, redirect=redirect)

Best regards!

Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
1
Ağu 25
368
0
Ağu 25
221
0
Tem 25
1209
0
Tem 25
3
0
Tem 25
6