Cflogin in application.cfc
Hello,
I have created a login page with cflogin which works pretty good, but i am trying to figure out a way how to check whenever a user goes to any page, lets say the main page, if he is logged in and if not for them to be presented a login page. I know this can be done inside the Application.cfc, but i'm not sure how, the examples that i found online don't seem to work. Any help would be very much appreciated.
Here is what i got so far:
3 roles - admin, sales, tech
3 users - admin1, sales1, tech1
index.cfm
===========================================
<cfform action="login.cfm" method="post" name="frmLogin">
<table align="center">
<tr>
<td>UserName:</td>
<td><cfinput type="text" name="username" required="yes" message="Please enter a username" maxlength="50"></td>
</tr>
<tr>
<td>Password:</td>
<td><cfinput type="password" name="password" required="yes" message="Please enter a password" maxlength="50"></td>
</tr>
<tr>
<td colspan="2"><cfinput type="submit" name="Login" value="Login"></td>
</tr>
</table>
</cfform>
login.cfm
============================================
<cflogin>
<!--- If the user hasn’t gotten the login form yet, display it --->
<cfif not (isDefined("form.username") and isDefined("form.password"))>
No login info
<cfinclude template="index.cfm">
<cfabort>
<!--- Otherwise, the user is submitting the login form --->
<!--- This code decides whether the username and password are valid --->
<cfelse>
<!--- Find record with this Username/Password --->
<!--- If no rows returned, password not valid --->
<cfquery name="getUser">
SELECT uid, firstname, lastname, rTrim(type) as UserRolename
FROM tbl_users LEFT OUTER JOIN tbl_roles
On tbl_users.roleid = tbl_roles.roleid
WHERE username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.username#">
AND password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.password#">
</cfquery>
<!--- If the username and password are correct... --->
<cfif getUser.recordCount eq 1>
<!--- Tell ColdFusion to consider the user “logged in” --->
<!--- For the name attribute, we will provide the user’s --->
<!--- ContactID number and first name, separated by commas --->
<!--- Later, we can access the name value via GetAuthUser() --->
User has been logged in
<cfloginuser name="#getUser.uid#,#getUser.firstname#, #getUser.lastname#" password="#form.password#" roles="#getUser.UserRolename#">
<cfinclude template="main.cfm">
<!--- Otherwise, re-prompt for a valid username and password --->
<cfelse>
Sorry, that username and password are not recognized.
Please try again.
<cfinclude template="index.cfm">
<cfabort>
</cfif>
</cfif>
</cflogin>
main.cfm
==========================================================
<cfoutput>Authenticated User in Main Page: #GetAuthUser()#</cfoutput>
