Guest User

Untitled

a guest
Mar 13th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import org.scalajs.dom.document
  2.  
  3. object Session {
  4. val CookieName = "SessionId"
  5. val key: String = CookieName
  6.  
  7. var username: Option[String] = Option(JsGlobals.username)
  8.  
  9. def get: Option[String] =
  10. document.cookie.split(";").toList.flatMap { part =>
  11. part.split("=").toList match {
  12. case `key` :: value :: Nil if value.nonEmpty => List(value)
  13. case _ => Nil
  14. }
  15. }.headOption
  16.  
  17. def set(key: String, value: String, expires: String, path: String = "/"): Unit =
  18. document.cookie = s"$key=$value; expires=$expires; path=$path"
  19.  
  20. def set: Option[String] => Unit = {
  21. case None => set(key, "", "Thu, 01-Jan-1970 00:00:01 GMT")
  22. case Some(sess) => set(key, sess, "Fri, 31 Dec 9999 23:59:59 GMT")
  23. }
  24.  
  25. def reset(): Unit = {
  26. username = None
  27. set(None)
  28. }
  29.  
  30. def set(username: String, hash: String): Unit = {
  31. this.username = Some(username)
  32. set(Some(hash))
  33. }
  34. }
  35.  
  36. object LogInPage {
  37. def manage(): Unit = {
  38. TagRef[tag.Button]("login").click := {
  39. val username = TagRef[tag.Input]("username").dom.value
  40. val password = TagRef[tag.Input]("password").dom.value
  41.  
  42. API.request(protocol.LogIn(username, password)).foreach {
  43. case None =>
  44. Session.reset()
  45. window.alert("Log-in failed")
  46.  
  47. case Some(hash) =>
  48. Session.set(username, hash)
  49. val host = window.location.origin
  50. window.location.href = s"$host/"
  51. }
  52. }
  53. }
  54. }
Add Comment
Please, Sign In to add comment