Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds login and logout

  • Loading branch information...
commit 33134d3707129933ecd6a8542e3c74f93652303e 1 parent 1b67163
Dominik Guzei authored
View
6 app/assets/javascripts/app/mediators/authenticationMediator.js.coffee
@@ -3,9 +3,11 @@ App = @App
App.mediators.authenticationMediator = SC.Object.create {
- userName: null,
- password: null,
+ userName: null
+ password: null
loggedIn: NO
+ notice: null
+ error: null
}
View
64 app/assets/javascripts/app/states/AuthenticationState.js.coffee
@@ -3,56 +3,36 @@
App = @App
-App.states.AuthenticationState = SC.State.extend {
-
- initialSubstate: 'Login',
-
- Login: SC.State.extend {
-
- initialSubstate: 'LoggedOut'
-
- LoggedOut: SC.State.extend {
-
- enterState: ->
- App.mediators.authenticationMediator.set 'loggedIn', NO
- App.mediators.authenticationMediator.set 'userName', null
- App.mediators.authenticationMediator.set 'password', null
+authenticationMediator = App.mediators.authenticationMediator
- login: ->
- @gotoState 'Authentication.Login.Authenticate'
- }
-
- LoggedIn: SC.State.extend {
- enterState: ->
- App.mediators.authenticationMediator.set 'loggedIn', YES
+App.states.AuthenticationState = SC.State.extend {
- context = App.controllers.statechartController.get 'lastRouteContext'
- if context then context.retry()
+ initialSubstate: 'LoggedOut'
- logout: ->
- @gotoState 'Authentication.Login.LoggedOut'
- }
+ LoggedOut: SC.State.extend {
- Authenticate: SC.State.extend {
- enterState: ->
- App.mediators.authenticationMediator.set 'userName', 'DominikGuzei'
- App.mediators.authenticationMediator.set 'password', 'xyz'
+ enterState: ->
+ authenticationMediator.set 'loggedIn', NO
+ authenticationMediator.set 'userName', null
+ authenticationMediator.set 'password', null
- @gotoState 'Authentication.Login.LoggedIn'
- }
+ authenticate: (userName, password) ->
+ if userName is 'user' and password is 'password'
+ authenticationMediator.set 'userName', userName
+ authenticationMediator.set 'password', password
+ @gotoState 'Authentication.LoggedIn'
+ else
+ authenticationMediator.set 'error', 'username or password are wrong.'
}
- Registration: SC.State.extend {
-
- initialSubstate: 'Intern'
+ LoggedIn: SC.State.extend {
+ enterState: ->
+ authenticationMediator.set 'loggedIn', YES
- Intern: SC.State.extend {
+ context = App.controllers.statechartController.get 'lastRouteContext'
+ if context then context.retry()
- }
-
- Facebook: SC.State.extend {
-
- }
+ logout: ->
+ @gotoState 'Authentication.LoggedOut'
}
-
}
View
47 app/assets/javascripts/app/states/BrowsingState.js.coffee
@@ -2,6 +2,7 @@
#= require app/mediators/pagesMediator
#= require app/views/pages/ListNotesView
#= require app/views/pages/CreateNoteView
+#= require app/views/pages/LoginView
App = @App
pagesMediator = App.mediators.pagesMediator
@@ -13,17 +14,43 @@ App.states.BrowsingState = SC.State.extend {
Public: SC.State.extend {
- initialSubstate: 'ListNotes'
+ initialSubstate: 'Index'
+
+ Index: SC.State.extend {
+ enterState: ->
+ # wait until SC.routes has initialized before
+ # deciding to redirect to default page
+ SC.run.next =>
+ if SC.routes.get('location') is ''
+ @gotoState 'Browsing.Public.ListNotes'
+ }
ListNotes: SC.State.extend {
representRoute: 'notes'
enterState: ->
+ SC.routes.set('location', 'notes')
pagesMediator.set 'currentPage', pages.ListNotesView
- createNote: ->
- @gotoState 'Browsing.Restricted.CreateNote'
+ }
+
+ Login: SC.State.extend {
+
+ representRoute: 'user/login'
+
+ enterState: ->
+ # let SC.routes run first
+ SC.run.next =>
+ SC.routes.set('location', 'user/login')
+
+ pagesMediator.set 'currentPage', pages.LoginView
+
+ loginFormSubmitted: (userName, password) ->
+ App.mediators.authenticationMediator.set('notice', '')
+ App.mediators.authenticationMediator.set('error', '')
+ @get('statechart').send('authenticate', userName, password)
+
}
}
@@ -33,10 +60,8 @@ App.states.BrowsingState = SC.State.extend {
beforeFilter: ->
unless App.mediators.authenticationMediator.get('loggedIn')
- # don't call gotoState directly here, because there
- # might be multiple parts of application interested in
- # handling the login action
- @get('statechart').send('login')
+ App.mediators.authenticationMediator.set('notice', 'You have no access to this page. Please login!')
+ @gotoState 'Browsing.Public.Login'
# don't trigger transition to substate
return false
@@ -44,15 +69,15 @@ App.states.BrowsingState = SC.State.extend {
# trigger transition to substate
return true
+ logout: ->
+ @gotoState 'Browsing.Public.ListNotes'
+
CreateNote: SC.State.extend {
representRoute: 'notes/create'
enterState: ->
+ SC.routes.set('location', 'notes/create')
pagesMediator.set 'currentPage', pages.CreateNoteView
}
-
- logout: ->
- @gotoState 'Browsing.Public.ListNotes'
}
-
}
View
5 app/assets/javascripts/app/templates/navigation.js.hjs
@@ -2,4 +2,9 @@
<ul>
<li><a href="#notes">List</a></li>
<li><a href="#notes/create">Create</a></li>
+ {{#if loggedIn}}
+ <li><button type="button" {{action "logout"}}>Logout</button></li>
+ {{else}}
+ <li><a href="#user/login">Login</a></li>
+ {{/if}}
</ul>
View
16 app/assets/javascripts/app/templates/pages/login.js.hjs
@@ -0,0 +1,16 @@
+
+<h2>Login:</h2>
+
+{{#if mediator.loggedIn}}
+ <p>You are logged in as '{{mediator.userName}}'.</p>
+{{else}}
+ <p class="notice">{{mediator.notice}}</p>
+ <p class="error">{{mediator.error}}</p>
+
+ <input type="text" placeholder="username" id="login-username" />
+ <input type="password" placeholder="password" id="login-password" />
+
+ <button type="button" {{action "login"}}>login</button>
+
+ <p class="info">You can use 'user' as userName and 'password' as password.</p>
+{{/if}}
View
5 app/assets/javascripts/app/views/NavigationView.js.coffee
@@ -8,4 +8,9 @@ App.views.NavigationView = SC.View.extend {
templateName: 'app_templates_navigation'
elementId: 'navigation'
+ loggedInBinding: 'App.mediators.authenticationMediator.loggedIn'
+
+ logout: ->
+ App.statechart.send 'logout'
+
}
View
9 app/assets/javascripts/app/views/PagesView.js.coffee
@@ -9,15 +9,16 @@ App.views.PagesView = SC.View.extend {
currentPageView: null
didInsertElement: ->
- @_makeCurrentPageView @get('currentPage').create()
+ @_makeCurrentPageView @get('currentPage')
currentPageChanged: SC.observer (->
- @_makeCurrentPageView @get('currentPage').create()
+ @_makeCurrentPageView @get('currentPage')
), 'currentPage'
_makeCurrentPageView: (pageView) ->
- @_removeCurrentPageView()
- @_addCurrentPageView pageView
+ if pageView?
+ @_removeCurrentPageView()
+ @_addCurrentPageView pageView.create()
_removeCurrentPageView: ->
currentPageView = @get 'currentPageView'
View
19 app/assets/javascripts/app/views/pages/LoginView.js.coffee
@@ -0,0 +1,19 @@
+
+#= require app/templates/pages/login
+
+App = @App
+
+App.views.pages.LoginView = SC.View.extend {
+
+ templateName: 'app_templates_pages_login'
+ elementId: 'login-page'
+
+ mediatorBinding: 'App.mediators.authenticationMediator'
+
+ login: ->
+ userName = $('#login-username').val()
+ password = $('#login-password').val()
+
+ App.statechart.send 'loginFormSubmitted', userName, password
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.