4 Replies Latest reply on Feb 13, 2007 9:36 PM by WestSide

    HELP!!  In Flex 2 how do I connect my CFCOMPONENT to my MS SQL database to authenticate a user

    JohnLeger
      I currently have the application working great using a combination of user/roles authentication with the username, password and roles hard coded into my cfcomponent below.

      MY ISSUE: I am using Flex 2 with ColdFusion MX 7.02 and MS SQL Server Express 2005. I would like to be able to take my component below and query my MembershipInfo table in my database to authenticate my users against my login form. My database source is "myDatabase" and the table with the login info is called "MembershipInfo." Also in my login form the id for my username textinput is "usernameTextInput" and the id for my password textinput is "passwordTextInput." The name of my login form component is "LoginHBox.mxml" which in a folder called "view."

      MY QUESTION: How should the Query be written to correctly connect to my database to check user authentication AGAINST my login form? I know that it can be done but am having a tough time figuring out the logic. Any help would be greatly appreciated.

      NOTE: I am successfully able to query the same database to fill in a DataGrid, so I am able to connect to my database using remote flashing.

      <cfcomponent>
      <cffunction name="loginUser" access="remote" returntype="WebsiteApplication.cfc.UserCFC">
      <cfargument name="username" type="string" required="true">
      <cfargument name="password" type="string" required="true">

      <cfset var user=createObject("component", "UserCFC")>

      <!-- The following CFIF and CFELSE contain hard-coded username, passwords and roles
      I would like to replace them with info from my database, but need help to figure out the logic -->
      <cfif arguments.username is "admin" and arguments.password is "password">
      <cfset user.loggedin=true>
      <cfset user.username="admin">
      <cfset user.roles="SeniorAdministrator">
      <cfreturn user>

      <cfelseif arguments.username is "user" and arguments.password is "password">
      <cfset user.loggedin=true>
      <cfset user.username="user">
      <cfset user.roles="Visitor">
      <cfreturn user>

      <cfelse>
      <cfreturn user>
      </cfif>
      </cffunction>
      </cfcomponent>
        • 1. HELP!!  Connecting my CFCOMPONENT to my MS SQL database to authenticate a user
          WestSide Level 1
          Hi,

          Your question is independent of Flex it seems and is more of just a CF question.

          MY QUESTION: How should the Query be written to correctly connect to my database to check user authentication AGAINST my login form? I know that it can be done but am having a tough time figuring out the logic. Any help would be greatly appreciated.

          You can compare what the user entered in the form to what is stored in the database. If there is a match authenticate and log the user in. I would add some client and server side validation as well before it even hits your method call in your CFC. If you are using an email field you might want to validate that it is a valid email address, etc.

          Post in the CF forum and you most likely will get more details.

          -Westside


          • 2. Re: HELP!!  Connecting my CFCOMPONENT to my MS SQL database to authenticate a user
            WestSide Level 1
            Hi,

            Your question is independent of Flex it seems and is more of just a CF question.

            MY QUESTION: How should the Query be written to correctly connect to my database to check user authentication AGAINST my login form? I know that it can be done but am having a tough time figuring out the logic. Any help would be greatly appreciated.

            You can compare what the user entered in the form to what is stored in the database. If there is a match authenticate and log the user in.

            Post in the CF forum and you most likely will get more details.

            -Westside


            • 3. HELP!!  In Flex 2 how do I connect my CFCOMPONENT to my MS SQL database to authenticate a user
              JohnLeger Level 1
              I would agree WestSide if I were using ColdFusion with HTML, but I'm not. I am on the other hand building an application using Flex 2. My question is really for those who are using Flex 2, ColdFusion MX 7.02 and MS SQL Server. The specific area I am having dificulty with is noted below. Though I do appreciate and will take your advice and post this also on the ColdFusion forum, but because my issue has to do with how ColdFusion talks with Flex 2 I do not feel it is soley a ColdFusion issue and think it's appropriate to keep it post here as well. :) Personally this is where I would check as a Flex 2 developer!

              <cfquery name="getUser" dataSource="myDatabase">
              SELECT Username, Password, AccessLevelID
              FROM MembershipInfo
              WHERE Username = <<HOW do test for equality against my usernameTextInput in my LoginHBox.mxml
              AND Password = <<HOW do test for equality against my passwordTextInput in my LoginHBox.mxml
              • 4. Re: HELP!!  In Flex 2 how do I connect my CFCOMPONENT to my MS SQL database to authenticate a user
                WestSide Level 1
                Hi,

                Ok, it seems your having a problem with passing the parameters between Flex and your CFC.

                How bout this:

                <cffunction name="loginUser" access="remote" returntype="WebsiteApplication.cfc.UserCFC">
                <cfargument name="loginVO" type="struct" required="true" />
                <cfset var result = "" />
                <cfquery name="result" datasource="#variables.dsn#">
                SELECT u.firstName, u.lastName, u.password, u.email
                FROM Users u
                WHERE u.email = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.loginVO.username#">
                AND u.password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.loginVO.password#">
                </cfquery>
                <!--- You could do: <cfif result.recordCount>, or <cfif result.recordCount eq 1> or however
                <cfreturn result />
                </cffunction>

                I usually return an array to flex. I have heard some have success with returning a query object, but I usually return an array.

                Regarding passing params from flex to CF, I do something like this, its a bit specific to the framework I am using for Flex (Cairngorm 2.x), but basically I take the values from the form fields (i.e, password.text or whatever) and stuff them into a container (loginVO), then dispatch that event. The event contains the user's credentials. You dispatch the event and some event listener would respond and go get the data from the db via some service whether it be Webservices or a RemoteObject is up to you. You can pass the whole data structure in as I do above in the <cffunction>. This works for me. Some use the flex/cf wizard extensions that do some mapping of value objects to your cfc's. Seems to be cool as well, but its not required to make it work. I had similar questions and I will try to dig up links to those questions because someone answered this for me as well, and perhaps I am not explaining it effectively.

                This would be on the same page as your Login.mxml file or whatever, I would refer to it as "view" code or "display" code in CF.

                var loginVO : LoginVO = new LoginVO();
                loginVO.username = username.text;
                loginVO.password = password.text;
                var event : LoginEvent = new LoginEvent( loginVO );

                CairngormEventDispatcher.getInstance().dispatchEvent( event );

                Here is a link that shows how its done or a variation of it. Credit goes to Alex Uhlmann, its his code. I learned from it, maybe you can do the same.

                http://www.alex-uhlmann.de/flash/adobe/blog/cairngormlogin/CairngormLogin.html

                -Westside