Goal: App will check if user auth flag has been set. If not, then will redirect to auth page and set auth flag. Once auth flag is set, app will show that user has been authed. Not a practical app, just shows concepts.
ruleset a60x502 {
meta {
name "delay-replace-inner-and-redirect"
description <<
delay-replace-inner-and-redirect
>>
author "Mike Grace"
logging on
}
rule clear_to_run_example_again {
select when pageview "example.com/\?clear"
{
notify("Your auth flag has been cleared","") with sticky = true;
}
fired {
clear ent:authed;
last;
}
}
rule fake_authed {
select when pageview "http:\/\/stackoverflow\.com\/users\/login"
{
notify("You are now authorized","") with sticky = true;
}
fired {
set ent:authed;
last;
}
}
rule check_auth {
select when pageview ".*"
pre {
authed = ent:authed || false;
}
if (authed) then {
notify("Auth check","Thanks for being authorized") with sticky = true;
}
notfired {
raise explicit event redirect_to_auth
}
}
rule redirect_to_auth {
select when explicit redirect_to_auth
pre {
content =<<
<p style="font-size: <span class=;">20px;"></p>
You need to be logged in to StackOverflow to be able to use this Kynetx app.
Redirecting to the <a href="http://stackoverflow.com/users/login">
login page</a> in <span id="fo-countdown">8</span> seconds
</p>
>>;
}
{
notify("<h1>FriendOverflow</h1>",content) with sticky = true and opacity = 1;
replace_inner("#fo-countdown", "7") with delay = 1;
replace_inner("#fo-countdown", "6") with delay = 2;
replace_inner("#fo-countdown", "5") with delay = 3;
replace_inner("#fo-countdown", "4") with delay = 4;
replace_inner("#fo-countdown", "3") with delay = 5;
replace_inner("#fo-countdown", "2") with delay = 6;
replace_inner("#fo-countdown", "1") with delay = 7;
replace_inner("#fo-countdown", "0") with delay = 8;
redirect("http://stackoverflow.com/users/login") with delay = 9;
}
}
}
- the order of the rules in this example app is very important as rules run in the order they are written for a given event – this doesn’t include events that are added to the schedule via a raised explicit event -> those go to the end of the line
- ‘clear_to_run_example_again’ rule is first to check if user is clearing persistence
- 18 stop execution if the action block fires
- ‘fake_authed’ rule marks auth flag if user is on auth page
- set ent:authed flag to true
- 29 stop execution if the action block fires
- ‘check_auth’ rule checks auth and shows nice notify if user is authed
- 42 if action block didn’t fire (user not authed) then add ‘redirect_to_auth’ rule to the end of the schedule for execution
- 59-66 update countdown using a delay parameter on the replace_inner action
- 67 redirect current window to new url
App run first time on StackOverflow.com with bookmarklet
App run on stackoverflow.com/users/login after being redirected by app with bookmarklet
App run second time on stackoverflow.com with bookmarklet
App run on example.com/?clear with bookmarklet
- This kind of pattern could be used to ensure that a user of the app has done something before allowing them to access another part of the apps functionality
- I use a similar pattern to ensure that I have a users twitter user’s name before running the rest of the app
Get the bookmarklet to try it out yourself!
Gratuitous day 16 Grace face




