Thursday, March 9, 2023

How to login your Oracle Apex application from cookies

Managing user authentication efficiently is one of the key aspects of building secure and user-friendly web applications. In Oracle APEX, we usually rely on session-based authentication methods such as database accounts, Single Sign-On (SSO), or custom authentication schemes. However, in some cases, you might want to provide a smoother login experience by leveraging browser cookies. This approach allows your application to remember returning users and automatically log them in without repeatedly prompting for credentials. In this post, we’ll explore how to implement cookie-based login in Oracle APEX step by step. For better understanding you can follow login from cookies. 

Step- 1: Open your Login Page in Developer mode.

 Step- 2: Create a new item and set the below properties-

            Name: P9999_REMEMBER

            Type: Checkbox Group

            Label: Remember Me

            Number of Columns: 1

            List of Values > Type: Share Component

                                        List of Values: REMEMBER_LOGIN_USERNAME

 Step- 3: Navigate to process tab from navigation pane.

            Create a new process under processing section or modify the existing process named "Set         Username Cookie"

            Set the below properties of this process:

            Name: <Your Process Name Here>

            Type: Execute Code

            Source : Local Database

            Language: PL/SQL

            Write the below PL/SQL code in the language properties-

            IF :P9999_REMEMBER = 'Y' THEN

                    BEGIN

                            owa_util.mime_header('text/html', FALSE);

                            OWA_COOKIE.SEND(

                                       NAME => 'APEX_APPS_102',

                                       VALUE => :P9999_USERNAME||':'||apex_util.get_hash(apex_t_varchar2(:P9999_USERNAME, :P9999_PASSWORD),NULL), -- Could also set this to null

                                       EXPIRES => SYSDATE + 100, -- use null if set cookie for a session

                                       PATH => '/',

                                       DOMAIN => NULL, 

                                       SECURE => NULL);

                        END;

            END IF;


 Step- 4: Navigate to rendering tab from navigation pane.

            Navigate to- Pre-Rendering > Process

            Create a new process or you can modify the existing process named "Get Username Cookie".

            Set the below properties of this process:

            Name: <Your Process Name Here>

            Type: Execute Code

            Source : Local Database

            Language: PL/SQL

            Write the below PL/SQL code in the language properties-

            DECLARE

                  c OWA_COOKIE.cookie;

                 a wwv_flow_global.vc_arr2;  

                  l_pass    varchar2(2000);

            BEGIN

                  c := OWA_COOKIE.get('YOUR COOKIE NAME HERE');

                  a := htmldb_util.string_to_table(c.vals(1)); --Retrived value(user name and password) from cookie.

                  :P9999_USERNAME := a(1); -- Segregate username from value and.  

                  :P9999_PASSWORD := a(2);   -- Segregate password from value.  

          BEGIN

                    -- Cross check the cookies user comparing with the existing application user from database.

                  select pass_word

                  into l_pass

                  from app_user

                  where user_id = :P9999_USERNAME;

          EXCEPTION

                  WHEN NO_DATA_FOUND THEN

                    NULL;

          END;

          IF l_pass = :P9999_PASSWORD  THEN

                   -- If matched then call the login function and redirect it to home page

                  apex_authentication.login(

                            p_username => :P9999_USERNAME,

                            p_password => :P9999_PASSWORD );

          else

              null;

          END IF;

        EXCEPTION

                WHEN NO_DATA_FOUND THEN

                NULL;

        END;

No comments:

Post a Comment