i recently built a login system in flex with a coldfusion backend with
the help of this forum and i put up the solution on the flex cook
book. now i realized that it was kind of incomplete coz though it
works perfectly, it still lacks a mechanism to save the login data. so
my question is how can i use a sharedObject to save the login info so
that it can be used throughout the flex application. here is the flex
sample code of the login system i created and posted on cookbook.

"login_example.mxml"
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" xmlns:view="components.*">
        <mx:states>
                <mx:State name="Log Out">
                        <mx:SetProperty target="{label1}" name="text" 
value="Log Out"/>
                </mx:State>
        </mx:states>
        <mx:Script>
                <![CDATA[
                        import components.*;
                        //Flash Classes
                        import flash.events.Event;
            import flash.events.MouseEvent;
            
            //Flex Classes
                        import mx.containers.TitleWindow;
                        import mx.controls.Alert;
            import mx.events.CloseEvent;            
            import mx.managers.PopUpManager;
            import mx.core.IFlexDisplayObject;
                                
            public var loggedin:Boolean = false;
                public var pop:Login;                   
                        
            private function showLogin():void
            {
                pop =
Login(PopUpManager.createPopUp(Application.application as
DisplayObject,Login,true));
                pop.showCloseButton =true;
                PopUpManager.centerPopUp(pop);                  
                pop.addEventListener("close",removeMe);
                pop["cancelButton"].addEventListener("click", removeMe);
                pop.addEventListener( "loginSuccessful",
handleLoginSucess ); 
            }
            
            private function removeMe(event:Event):void {
                PopUpManager.removePopUp(pop);
            }
            
            private function handleLoginSucess(event:Event):void{
                viewstack1.selectedChild = admin;
                lbl_intro.text = "Welcome " +pop.username_txt.text;
                removeMe(event);                
                currentState = 'Log Out';
            }  
            
                                
                ]]>
        </mx:Script>
        
        <mx:Label x="0" y="0" text="Sign In" id="label1" buttonMode="true"
useHandCursor="true" mouseChildren="false" click="showLogin()"/>
        <mx:ViewStack x="0" y="26" id="viewstack1" width="100%" height="100%">
                <view:Home id="home"/>
                <view:Admin id="admin"/>
        </mx:ViewStack> 
        <mx:Label x="700" y="0" id="lbl_intro"/>
</mx:Application>
 "login.mxml"

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" title="Login Form">
        <mx:Metadata>
       [Event(name="loginSuccessful", type="flash.events.Event")] 
    </mx:Metadata>
    
    <mx:Script>
        <![CDATA[
                import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            import mx.utils.ObjectUtil;
            import mx.controls.Alert;
            import mx.events.ValidationResultEvent;
            
            private function isValid():Boolean
            {
                var emailValidResult:ValidationResultEvent =
this.emailValidate.validate(this.username_txt.text);
                var pswdValidResult:ValidationResultEvent =
this.pswdValidate.validate(this.password_txt.text);
                
                if (emailValidResult.type==ValidationResultEvent.VALID 
                                && 
pswdValidResult.type==ValidationResultEvent.VALID) 
                {
                        return true;    
                }
                else
                {
                        return false;   
                }
                
            }
            
            private function authenticateUser():void
            {
                                if( isValid() )
                        {
                        authManager.loginUser( this.username_txt.text,
this.password_txt.text );   
                }
            } 
            
            private function errorMessage(msg:String):void
            {
                //Alert.show( ObjectUtil.toString(event.message) );
                this.errorMsg.text = msg;
                this.errorMsg.height = 15;
                this.errorMsg.visible = true;
            }    
   
            private function serverFault(event:FaultEvent):void
            {
                errorMessage(event.message['message']);
            }   
            
                  
            
            private function login_result(event:ResultEvent):void
            {
                // login successful, remember the user.
                if( event.result == "true" || event.result == "TRUE" ||
event.result == "1" || event.result == 1 )
                {                                                               
                                    this.dispatchEvent( new 
Event('loginSuccessful') );
                }
                else
                {
                        // login didn't work. show message
                        errorMessage("Login unsuccessful");     
                }
            }
        ]]>
    </mx:Script>
    
    <mx:RemoteObject 
        id="authManager" 
        destination="ColdFusion" 
        source="login_example.cfc.login" 
        showBusyCursor="true">
       <mx:method name="loginUser" result="login_result(event)"
fault="serverFault(event)" />
    </mx:RemoteObject> 
    
    <mx:StringValidator 
        id="emailValidate" 
        source="{this.username_txt}" 
        property="text" 
        required="true" />
    <mx:StringValidator 
        id="pswdValidate" 
        source="{this.password_txt}" 
        property="text" 
        required="true" />
        
        <mx:Form x="0" y="0">
                <mx:FormItem label="User Name:">
                        <mx:TextInput id="username_txt"/>
                </mx:FormItem>
                <mx:FormItem label="Password:">
                        <mx:TextInput id="password_txt"/>
                </mx:FormItem>
        </mx:Form>
        <mx:Text x="0" y="90" id="errorMsg" visible="true" color="red"
width="100%" height="0" />
        <mx:ControlBar>
                <mx:HBox width="100%">          
                        <mx:Button label="Login" id="okButton" 
click="authenticateUser();"/>
                        <mx:Spacer width="100%"/>
                        <mx:Button label="Cancel" id="cancelButton"/>
                </mx:HBox>
        </mx:ControlBar>        
</mx:TitleWindow>
 
"login.cfc"

<cfcomponent>
        
        <cffunction name="loginUser" access="remote" returntype="boolean">
                    <cfargument name="username" type="string" required="true"/>
                    <cfargument name="password" type="string" required="true"/>

                    <cfquery name="checkAuthentication" 
datasource="authentication">
                          SELECT *
                          FROM profile
                          where username = <cfqueryparam 
cfsqltype="cf_sql_varchar"
value="#arguments.username#">
                                and Password = <cfqueryparam 
cfsqltype="cf_sql_varchar"
value="#arguments.password#">
                    </cfquery>

                    <cfif checkAuthentication.recordCount EQ 1>
                           <cfreturn true/>
                    <cfelse>
                           <cfreturn false/>
                    </cfif>
           </cffunction>

</cfcomponent>

Reply via email to