Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. var noMsg = "";
  2.  
  3. sig login_widget : (String, Handler((username:String, pass:String))) ~> Page
  4. fun login_widget(msg, return) {
  5. page
  6. <html>
  7. <style>
  8. label {{float: left; width: 90pt; text-align: right; display: inline;
  9. margin-right: 3pt; }}
  10. </style>
  11. <body>
  12. <div class="error">{stringToXml(msg)}</div>
  13. <form l:action="{return((username=username, pass=pass))}" method="post">
  14. <label>Name:</label>
  15. <div> <input l:name="username" /></div>
  16. <label>Password:</label>
  17. <div><input l:name="pass" type="password" /></div>
  18. <input type="Submit" />
  19. </form>
  20. <a l:href="{main()}">Start over</a>
  21. </body>
  22. </html>
  23. }
  24.  
  25. sig validAuth : (String, String) -> Bool
  26. fun validAuth(name, pass) {
  27. name == "foo" && pass == "bar"
  28. }
  29.  
  30. sig get_user : (String) ~> String
  31. fun get_user(msg) {
  32. var current_user = getCookie("loginname");
  33. if (current_user <> "") # User is logged in! Return creds.
  34. current_user
  35. else { # User is not logged in, show login page.
  36. var (username=name, pass=pass) =
  37. sendSuspend(fun (r){login_widget(msg, r)});
  38. if (validAuth(name, pass)) {
  39. # User logged in successfully, set cookie and return creds.
  40. setCookie("loginname", name);
  41. name
  42. } else
  43. # User failed to log in, show page again.
  44. get_user("The password you entered was incorrect")
  45. }
  46. }
  47.  
  48. sig logout : () ~> ()
  49. fun logout() {
  50. setCookie("loginname", "");
  51. }
  52.  
  53. sig logoutLink : (() ~> Page) ~> Xml
  54. fun logoutLink(target) {
  55. <a l:href="{logout(); freshResource(); target()}">Logout</a>
  56. }
  57.  
  58. fun main() {
  59. var user = get_user(noMsg);
  60.  
  61. page
  62. <html>
  63. <body>
  64. <div>Thanks for logging in, {stringToXml(user)}.</div>
  65. <ul><li><a href="" l:onclick="{addContent()}">Add Content</a></li>
  66. <li><button l:onclick="{addContent()}">Add Content</button></li>
  67. <li>{logoutLink(main)}</li>
  68. </ul>
  69. <div id="content">Intitial content here that should be replaced</div>
  70. </body>
  71. </html>
  72. }
  73.  
  74. fun addContent() {
  75. replaceNode(
  76. <h3>Added content should appear here</h3>,
  77. getNodeById("content"))
  78. }
  79.  
  80. fun mainPage(_) { main() }
  81.  
  82. fun start () {
  83. addRoute("",mainPage);
  84. servePages()
  85. }
  86.  
  87. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement