 
         dismiss: ->
             $main = $ '#main'
+            $form = $main.find 'form.themis-form-signin'
+            if $form.length > 0
+                $form.off 'submit'
+            $main.html ''
+            navigationBar.dismiss()
+
+    new SigninView()
+
+
+define 'loginView', ['jquery', 'view', 'renderTemplate', 'dataStore', 'navigationBar', 'jquery.history', 'jquery.form'], ($, View, renderTemplate, dataStore, navigationBar, History) ->
+    class LoginView extends View
+        constructor: ->
+            @urlRegex = /^\/login$/
+
+        present: ->
+            dataStore.getIdentity (err, identity) ->
+                $main = $ '#main'
+                if err?
+                    $main.html renderTemplate 'internal-error'
+                    navigationBar.present()
+                    return
+
+                if identity.role == 'guest'
+                    $main.html renderTemplate 'login-view'
+                    navigationBar.present()
+
+                    $form = $main.find 'form.themis-form-login'
+
+                    $submitError = $form.find '.submit-error > p'
+                    $submitButton = $form.find 'button'
+
+                    $form.on 'submit', (e) ->
+                        e.preventDefault()
+                        $form.ajaxSubmit
+                            beforeSubmit: ->
+                                $submitError.text ''
+                                $submitButton.prop 'disabled', yes
+                            clearForm: yes
+                            dataType: 'json'
+                            xhrFields:
+                                withCredentials: yes
+                            success: (responseText, textStatus, jqXHR) ->
+                                History.pushState urlPath: '/', '', '/'
+                            error: (jqXHR, textStatus, errorThrown) ->
+                                if jqXHR.responseJSON?
+                                    $submitError.text jqXHR.responseJSON
+                            complete: ->
+                                $submitButton.prop 'disabled', no
+                else
+                    $main.html renderTemplate 'already-authenticated'
+                    navigationBar.present()
+
+        dismiss: ->
+            $main = $ '#main'
             $form = $main.find 'form.themis-form-login'
             if $form.length > 0
                 $form.off 'submit'

                    navigationBar.present active: 'signin'

                    $form = $main.find 'form.themis-form-signin'

                    $submitError = $form.find '.submit-error > p'
                    $submitButton = $form.find 'button'

                    $form.on 'submit', (e) ->
                        e.preventDefault()
                        $form.ajaxSubmit
                            beforeSubmit: ->
                                $submitError.text ''
                                $submitButton.prop 'disabled', yes
                            clearForm: yes
                            dataType: 'json'
                            xhrFields:
                                withCredentials: yes
                            success: (responseText, textStatus, jqXHR) ->
                                console.log responseText
                            error: (jqXHR, textStatus, errorThrown) ->
                                if jqXHR.responseJSON?
                                    $submitError.text jqXHR.responseJSON
                            complete: ->
                                $submitButton.prop 'disabled', no
                else
                    $main.html renderTemplate 'already-authenticated'
                    navigationBar.present()

        dismiss: ->
            $main = $ '#main'
            $form = $main.find 'form.themis-form-signin'
            if $form.length > 0
                $form.off 'submit'
            $main.html ''
            navigationBar.dismiss()

    new SigninView()


define 'loginView', ['jquery', 'view', 'renderTemplate', 'dataStore', 'navigationBar', 'jquery.history', 'jquery.form'], ($, View, renderTemplate, dataStore, navigationBar, History) ->
    class LoginView extends View
        constructor: ->
            @urlRegex = /^\/login$/

        present: ->
            dataStore.getIdentity (err, identity) ->
                $main = $ '#main'
                if err?
                    $main.html renderTemplate 'internal-error'
                    navigationBar.present()
                    return

                if identity.role == 'guest'
                    $main.html renderTemplate 'login-view'
                    navigationBar.present()

                    $form = $main.find 'form.themis-form-login'

                    $submitError = $form.find '.submit-error > p'
                    $submitButton = $form.find 'button'

                            xhrFields:
                                withCredentials: yes
                            success: (responseText, textStatus, jqXHR) ->
                                History.pushState urlPath: '/signin', '', '/signin'
                            error: (jqXHR, textStatus, errorThrown) ->
                                if jqXHR.responseJSON?
                                    $submitError.text jqXHR.responseJSON
