Permalink
Browse files

Merge pull request #1 from dicarlo2/master

Login view
  • Loading branch information...
reedlabotz committed Sep 10, 2012
2 parents ee88687 + 9feb094 commit 2044630bc1a020bec00f838f2eb43f450a9c99d1
Showing with 290 additions and 53 deletions.
  1. +7 −2 src/main.coffee
  2. +33 −0 src/models/session_model.coffee
  3. +33 −0 src/views/login_view.coffee
  4. +18 −6 www/index.html
  5. +185 −45 www/js/app.js
  6. +14 −0 www/js/lib/jstorage.min.js
View
@@ -1,3 +1,8 @@
#<< views/app_view
-
-appview = new AppView
+#<< models/session_model
+#<< views/login_view
+$(document).ready( ->
+ appview = new AppView
+ sessionModel = new SessionModel
+ loginView = new LoginView {model: sessionModel}
+)
@@ -0,0 +1,33 @@
+class SessionModel extends Backbone.Model
+ defaults : {
+ "ticket_hash" : null
+ "user_id" : null
+ }
+
+ constructor: ->
+ super
+ _.bindAll @
+ _.extend @, Backbone.Events
+ @fetch()
+
+ login: (username, password) ->
+ jQuery.post("https://www.udjplayer.com/udj/0_6/auth", "username=#{username}&password=#{password}")
+ .done((data) => if @set(data) then @save())
+ .fail((data) => @trigger("session:loginFailed", data.getResponseHeader))
+
+ validate: (attributes) ->
+ if attributes.ticket_hash? and attributes.user_id?
+ @trigger "session:loggedIn"
+ return
+ else
+ @trigger "session:loggedOut"
+ "You are not logged in"
+
+ sync: (method, model) ->
+ if method is "create" or method is "update"
+ ttl = 24 * 360 * 1000
+ $.jStorage.set "udj-session", model.toJSON(), {TTL: ttl}
+ else if method is "read"
+ @set $.jStorage.get("udj-session")
+ else if method is "delete"
+ $.jStorage.removeKey "udj-session"
@@ -0,0 +1,33 @@
+class LoginView extends Backbone.View
+ events: {
+ "submit form[id=loginForm]" : "login"
+ }
+
+ constructor: ->
+ super
+ _.bindAll @
+ @setElement $("#login_container")
+ @model.on "session:loggedOut", @loggedOut
+ @model.on "session:loggedIn", @loggedIn
+ @model.on "session:loginFailed", @loginFailed
+ if not @model.isValid()
+ @render()
+
+ render: ->
+ @$el.show()
+
+ login: (event) =>
+ @model.login @$("[name=username]").val(), @$("[name=password]").val()
+ return false
+
+ loginFailed: (headers) =>
+ if headers?('WWW-Authenticate') is 'password'
+ alert 'Invalid username or password'
+ else
+ alert 'An unknown error has occurred. Please try again.'
+
+ loggedOut: (event) =>
+ @render()
+
+ loggedIn: (event) =>
+ @$el.hide()
View
@@ -21,14 +21,16 @@
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="favicon.ico">
+ <script src="js/lib/jquery-1.8.0.min.js"></script>
+ <script src="js/lib/jquery.cookie.js"></script>
<script src="js/lib/underscore-min.js"></script>
<script src="js/lib/backbone-min.js"></script>
<script src="js/lib/bootstrap.min.js"></script>
- <script src="js/lib/jquery-1.8.0.min.js"></script>
- <script src="js/lib/jquery.cookie.js"></script>
+ <script src="js/lib/jstorage.min.js"></script>
<script src="js/app.js"></script>
- <script type="text/javascript">
+ <!-- Templates -->
+ <!--<script type="text/javascript">
/*
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-146052-10']);
@@ -38,8 +40,8 @@
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
- */
- </script>
+
+ </script> -->
</head>
<body id="udjapp">
<div class="navbar navbar-fixed-top">
@@ -70,7 +72,17 @@ <h3>Queue</h3>
<h3>Library</h3>
</div>
</div>
+ <div id="login_container" class="well row offset4 span4" style="display: none;">
+ <div style="margin-left:auto; margin-right:auto; width:6em;">
+ <h3>Log In</h3>
+ </div>
+ <form id="loginForm" style="text-align: center">
+ <input type="text" name="username" placeholder="Username" style="width: 90%;" required autofocus>
+ <input type="password" name="password" placeholder="Password" style="width: 90%" required>
+ <input type="submit" class="btn btn-primary" value="Log In"/>
+ </form>
+ </div>
</div>
</body>
-</html>
+</html>
Oops, something went wrong.

0 comments on commit 2044630

Please sign in to comment.