• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help planning functionality with application.cfc files

Contributor ,
Sep 02, 2020 Sep 02, 2020

Copy link to clipboard

Copied

I have little experience with application.cfc files. I've used only rudimentary functions without really understanding, as long as I got the needed result.

Today, I want to do something that I'm finding it hard to find answers for, and I know, for some, this is child's play. I'd just like you to share a word-based description of what I need to do. Once I understand it at a high level, and know which functions to incorporate, I can learn what I need to.

As I hope the following diagram illustrates, when my user arrives at the root of my site, I want them to see the page contents. Anything they click will take them to another page in another folder. All requests will go to various files in the same folder. It's at this point, before a new page opens, that I want to test whether the user is logged in, or whether they need to create an account. All users must have an account.

If the user is logged in, they need to proceed happily along to their requested page.

What I don't want to do is push a login/account creation page on a brand new visitor to the site the as soon as they arrive. Thanks!

John_Allred_0-1599079907755.png

 

TOPICS
Getting started

Views

140

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
community guidelines
Community Expert ,
Sep 02, 2020 Sep 02, 2020

Copy link to clipboard

Copied

This is pretty easy to do, actually, but the specifics will depend on how you manage authentication in your application. The Application.cfc and Application.cfm files basically just let you automatically execute some code prior to the execution of the actual page the user requested (and in some cases, even if the user doesn't request a page with Application.cfc's event handlers).

 

So, setting aside what you actually asked to do for a minute, what if you wanted to force the user to authenticate no matter what page they visited? You could do this easily by using some conditional logic in Application.cfm or the onRequestStart event handler in Application.cfm. So, for example, you might have something like this in your onRequestStart:

 

<cffunction name="onRequestStart" returntype="boolean">

    <cfargument name="targetpage" required="true"> <!--- you don't have to worry about this, it'll automatically be provided by CF --->

     <cfif session.loggedin is false>

          <cfinclude template="/login.cfm"><cfabort>

     </cfif>

</cffunction>

 

OK, so now it should be easy to answer your question by extending this to exclude your home page!

 

<cffunction name="onRequestStart" returntype="boolean">

    <cfargument name="targetpage" required="true"> 

     <cfif session.loggedin is false and arguments.targetpage is not "/path/to/index.cfm">

          <cfinclude template="/login.cfm"><cfabort>

     </cfif>

</cffunction>

 

Note I'm typing this into the little answer box here on the page and I wouldn't guarantee it'll work exactly as is, but it's pretty close. Also, in addition to all that, I'd recommend you spend a few minutes reading this:

 

http://www.learncfinaweek.com/course/index/section/Application_cfc/item/Application_cfc/

 

Dave Watts, Eidolon LLC

Votes

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
community guidelines
Contributor ,
Sep 02, 2020 Sep 02, 2020

Copy link to clipboard

Copied

Thanks, Dave. Good stuff to chew on. You won't believe it, but I have the very page from learncfinaweek open in another browser tab!

Votes

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
community guidelines
Community Expert ,
Sep 06, 2020 Sep 06, 2020

Copy link to clipboard

Copied

LATEST

An addendum to Dave's code. In both cases, either

 

1) delete the attribute  returntype="boolean" 

or 2) add a line to return a boolean before </cffunction>

 

1) is perhaps preferable.

Votes

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
community guidelines
Resources
Documentation