Skip to content

Commit

Permalink
Added a ToolatraAuth example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
tenfensw committed Dec 5, 2019
1 parent da868fb commit 361e946
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ run
== What does it have?
[squares]
- Sinatra-like syntax
- Minimalistic templates
- A module that provides a fully-featured template engine
- Built-in web server that integrates easily with Nginx or Apache
- A module for generating and validating authorization tokens

== Installation
=== On macOS
Expand Down Expand Up @@ -225,5 +226,59 @@ get /settoken {
}
----

Authorization example:

[source,tcl]
----
set toolatra_auth ",(!%" ;# this is a 4-digit string that will be used to later encode the tokens that ToolatraAuth produces
package require Toolatra 19.12
package require ToolatraTemplates 19.11
package require ToolatraAuth 19.12
get / {
set cv [cookie authToken]
if {! [tokenValid $cv]} {
redirect /login
} else {
redirect /greet
}
}
get /login {
if {! [dict exists $params nm]} {
etcl form.html
} else {
set name [dict get $params nm]
set tkn [token $name] ;# the generated token will expire in 1 day, to specify the expiration date, specify the number of seconds as the second argument
cookie authToken $tkn
redirect /greet
}
}
get /greet {
set tkn [cookie authToken]
if {! [tokenValid $tkn]} {
redirect /login
} else {
set name [tokenValue $tkn]
show "Greetings, $name!"
}
}
run
----

where ``form.html`` is:

[source,html]
----
<form>
<p>To continue, please enter your name.</p>
<p>Name: <input type=text name=nm /></p>
<button type=submit>Next</button>
</form>
----

== License
As always, MIT License.

0 comments on commit 361e946

Please sign in to comment.