Highlighted

Better way to logout user from the system?

New Here ,
Mar 19, 2018

Copy link to clipboard

Copied

Hi everyone, I have single page app and I'm wondering what would be better way to logout user from the system. There is two ways that I have explored one is JQuery/Ajax that will call the function and wait to receive the response. Then base on response I will either redirect user to Login page or show an error to the user if exist. Second example is JQuery/Ajax will call the function then I will use cflocation to redirect user to the login page. However both methods have some wired errors for example first example few times throw JavaScript error for some reason. I couldn't find anything that caused that. Second example does not redirect user to the login page. Instead sits on the page where user was before logout process that is wrong. User always should end up on the login page after logout process is completed.

Example 1:

JS code:

function LogOut() {

    window.onbeforeunload = null;

    $.ajax({

        type: 'POST',

        url: 'Components/AjaxFunctions.cfc?method=LogOut',

        dataType: 'json'

    }).done(function(obj){

        if(obj.STATUS == "200"){

            location.href = 'Login.cfm';

        }else{

           alert(obj.MESSAGE);

        }

    }).fail(function(jqXHR, textStatus, errorThrown){

        alert("Error: "+errorThrown);

    });

}

ColdFusion:

<cffunction name="LogOut" access="remote" output="yes" returnformat="JSON">

     <cfargument name="timedOut" type="string" required="true" default="0">

     <cfset fnResults = structNew()>

     <cfset dt = createODBCDateTime(now())>

     <cfif structKeyExists(VARIABLES, "userID")>

          <cfquery name="userLogin" datasource="#dsn#">

               UPDATE UserLogins

               SET LogOut = #dt#,

                    SessionTimedOut = <cfqueryparam cfsqltype="cf_sql_bit" value="#trim(arguments.timedOut)#">

               WHERE UserID = <cfqueryparam cfsqltype="cf_sql_idstamp" value="#userID#">

          </cfquery>

          <cfset temp = structClear(SESSION)>

          <cfset fnResults.status = "200">

     <cfelse>

          <cfset fnResults.status = "400">

          <cfset fnResults.message = "Error! Please contact your administrator.">

     </cfif>

     <cfreturn fnResults>

</cffunction>

Example 2:

JS code:

function LogOut() {

    window.onbeforeunload = null;

    $.ajax({

        type: 'POST',

        url: 'Components/AjaxFunctions.cfc?method=LogOut'

    }).done(function(obj){

    }).fail(function(jqXHR, textStatus, errorThrown){

        alert("Error: "+errorThrown);

    });

}

ColdFusion code:

<cffunction name="LogOut" access="remote" output="true">

         <cfargument name="timedOut" type="string" required="true" default="0">

     <cfif structKeyExists(VARIABLES, "userID")>

          <cfquery name="trackLogin" datasource="#dsn#">

               UPDATE UserLogins

               SET LogOut = CURRENT_TIMESTAMP,

               SessionTimedOut = <cfqueryparam cfsqltype="cf_sql_bit" value="#trim(arguments.timedOut)#">

               WHERE UserID = <cfqueryparam cfsqltype="cf_sql_idstamp" value="#userID#">

          </cfquery>

     </cfif>

     <cfset temp = structClear(SESSION)>

     <cflocation url="Login.cfm" addtoken="false">

</cffunction>

As you can see my second example has cflocation with the specified file name but for some reason user won't be redirected to that page. If anyone see where the problem is and what option would be better please let me know. Thank you!

Views

342

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Better way to logout user from the system?

New Here ,
Mar 19, 2018

Copy link to clipboard

Copied

Hi everyone, I have single page app and I'm wondering what would be better way to logout user from the system. There is two ways that I have explored one is JQuery/Ajax that will call the function and wait to receive the response. Then base on response I will either redirect user to Login page or show an error to the user if exist. Second example is JQuery/Ajax will call the function then I will use cflocation to redirect user to the login page. However both methods have some wired errors for example first example few times throw JavaScript error for some reason. I couldn't find anything that caused that. Second example does not redirect user to the login page. Instead sits on the page where user was before logout process that is wrong. User always should end up on the login page after logout process is completed.

Example 1:

JS code:

function LogOut() {

    window.onbeforeunload = null;

    $.ajax({

        type: 'POST',

        url: 'Components/AjaxFunctions.cfc?method=LogOut',

        dataType: 'json'

    }).done(function(obj){

        if(obj.STATUS == "200"){

            location.href = 'Login.cfm';

        }else{

           alert(obj.MESSAGE);

        }

    }).fail(function(jqXHR, textStatus, errorThrown){

        alert("Error: "+errorThrown);

    });

}

ColdFusion:

<cffunction name="LogOut" access="remote" output="yes" returnformat="JSON">

     <cfargument name="timedOut" type="string" required="true" default="0">

     <cfset fnResults = structNew()>

     <cfset dt = createODBCDateTime(now())>

     <cfif structKeyExists(VARIABLES, "userID")>

          <cfquery name="userLogin" datasource="#dsn#">

               UPDATE UserLogins

               SET LogOut = #dt#,

                    SessionTimedOut = <cfqueryparam cfsqltype="cf_sql_bit" value="#trim(arguments.timedOut)#">

               WHERE UserID = <cfqueryparam cfsqltype="cf_sql_idstamp" value="#userID#">

          </cfquery>

          <cfset temp = structClear(SESSION)>

          <cfset fnResults.status = "200">

     <cfelse>

          <cfset fnResults.status = "400">

          <cfset fnResults.message = "Error! Please contact your administrator.">

     </cfif>

     <cfreturn fnResults>

</cffunction>

Example 2:

JS code:

function LogOut() {

    window.onbeforeunload = null;

    $.ajax({

        type: 'POST',

        url: 'Components/AjaxFunctions.cfc?method=LogOut'

    }).done(function(obj){

    }).fail(function(jqXHR, textStatus, errorThrown){

        alert("Error: "+errorThrown);

    });

}

ColdFusion code:

<cffunction name="LogOut" access="remote" output="true">

         <cfargument name="timedOut" type="string" required="true" default="0">

     <cfif structKeyExists(VARIABLES, "userID")>

          <cfquery name="trackLogin" datasource="#dsn#">

               UPDATE UserLogins

               SET LogOut = CURRENT_TIMESTAMP,

               SessionTimedOut = <cfqueryparam cfsqltype="cf_sql_bit" value="#trim(arguments.timedOut)#">

               WHERE UserID = <cfqueryparam cfsqltype="cf_sql_idstamp" value="#userID#">

          </cfquery>

     </cfif>

     <cfset temp = structClear(SESSION)>

     <cflocation url="Login.cfm" addtoken="false">

</cffunction>

As you can see my second example has cflocation with the specified file name but for some reason user won't be redirected to that page. If anyone see where the problem is and what option would be better please let me know. Thank you!

Views

343

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 19, 2018 0

Have something to add?

Join the conversation