diff --git a/core/client/app.js b/core/client/app.js index 27671a09aa..49b5c9af01 100755 --- a/core/client/app.js +++ b/core/client/app.js @@ -1,5 +1,6 @@ import Resolver from 'ember/resolver'; import initFixtures from 'ghost/fixtures/init'; +import {currentUser, injectCurrentUser} from 'ghost/initializers/current-user'; var App = Ember.Application.extend({ /** @@ -16,4 +17,7 @@ var App = Ember.Application.extend({ initFixtures(); -export default App; +App.initializer(currentUser); +App.initializer(injectCurrentUser); + +export default App; \ No newline at end of file diff --git a/core/client/controllers/application.js b/core/client/controllers/application.js new file mode 100644 index 0000000000..74a5a52bce --- /dev/null +++ b/core/client/controllers/application.js @@ -0,0 +1,5 @@ +var ApplicationController = Ember.Controller.extend({ + isLogin: Ember.computed.equal('currentPath', 'login') +}); + +export default ApplicationController; \ No newline at end of file diff --git a/core/client/fixtures/init.js b/core/client/fixtures/init.js index 9bdbb71924..1ec5719cc8 100644 --- a/core/client/fixtures/init.js +++ b/core/client/fixtures/init.js @@ -1,32 +1,41 @@ /*global ic */ import postFixtures from 'ghost/fixtures/posts'; +import userFixtures from 'ghost/fixtures/users'; + +var response = function (responseBody, status) { + status = status || 200; + var textStatus = (status === 200) ? 'success' : 'error'; -var post = function (id) { return { - response: postFixtures.findBy('id', id), - jqXHR: {}, - textStatus: 'success' + response: responseBody, + jqXHR: { status: status }, + textStatus: textStatus }; }; -var posts = function () { - return { - response: { - 'posts': postFixtures, - 'page': 1, - 'limit': 15, - 'pages': 1, - 'total': 2 - }, - jqXHR: {}, - textStatus: 'success' - }; +var user = function (status) { + return response(userFixtures.findBy('id', 1), status); }; -var defineFixtures = function () { - ic.ajax.defineFixture('/ghost/api/v0.1/posts', posts()); - ic.ajax.defineFixture('/ghost/api/v0.1/posts/1', post(1)); - ic.ajax.defineFixture('/ghost/api/v0.1/posts/2', post(2)); +var post = function (id, status) { + return response(postFixtures.findBy('id', id), status); +}; + +var posts = function (status) { + return response({ + 'posts': postFixtures, + 'page': 1, + 'limit': 15, + 'pages': 1, + 'total': 2 + }, status); +}; + +var defineFixtures = function (status) { + ic.ajax.defineFixture('/ghost/api/v0.1/posts', posts(status)); + ic.ajax.defineFixture('/ghost/api/v0.1/posts/1', post(1, status)); + ic.ajax.defineFixture('/ghost/api/v0.1/posts/2', post(2, status)); + ic.ajax.defineFixture('/ghost/api/v0.1/signin', user(status)); }; export default defineFixtures; \ No newline at end of file diff --git a/core/client/fixtures/users.js b/core/client/fixtures/users.js new file mode 100644 index 0000000000..e663e5c083 --- /dev/null +++ b/core/client/fixtures/users.js @@ -0,0 +1,23 @@ +var users = [ + { + "id": 1, + "uuid": "ba9c67e4-8046-4b8c-9349-0eed3cca7529", + "name": "some-user", + "slug": "some-user", + "email": "some@email.com", + "image": null, + "cover": null, + "bio": "", + "website": "", + "location": "", + "accessibility": null, + "status": "active", + "language": "en_US", + "meta_title": null, + "meta_description": null, + "created_at": "2014-02-15T20:02:25.000Z", + "updated_at": "2014-03-11T14:06:43.000Z" + } +]; + +export default users; \ No newline at end of file diff --git a/core/client/initializers/current-user.js b/core/client/initializers/current-user.js new file mode 100644 index 0000000000..2766bdab5a --- /dev/null +++ b/core/client/initializers/current-user.js @@ -0,0 +1,26 @@ +import userFixtures from 'ghost/fixtures/users'; + +var currentUser = { + name: 'currentUser', + + initialize: function (container) { + var userFixture = userFixtures.findBy("id", 1); + + container.register('user:current', Ember.Object.extend(userFixture)); + // Todo: remove userFixture + // Todo: use model User instead of Ember.Object once model layer exists + } +}; + +var injectCurrentUser = { + name: 'injectCurrentUser', + + initialize: function (container) { + if (container.lookup('user:current')) { + container.injection('route', 'user', 'user:current'); + container.injection('controller', 'user', 'user:current'); + } + } +}; + +export {currentUser, injectCurrentUser}; \ No newline at end of file diff --git a/core/client/router.js b/core/client/router.js index 1e3c906f03..c972622c72 100755 --- a/core/client/router.js +++ b/core/client/router.js @@ -9,6 +9,7 @@ Router.reopen({ }); Router.map(function () { + this.route('login', { path: '/signin' }); this.resource('posts', { path: '/' }, function () { this.route('post', { path: ':post_id' }); }); diff --git a/core/client/routes/authenticated.js b/core/client/routes/authenticated.js new file mode 100644 index 0000000000..4ab3ba3fbc --- /dev/null +++ b/core/client/routes/authenticated.js @@ -0,0 +1,11 @@ +var AuthenticatedRoute = Ember.Route.extend({ + actions: { + error: function (error) { + if (error.jqXHR.status === 401) { + this.transitionTo('login'); + } + } + } +}); + +export default AuthenticatedRoute; \ No newline at end of file diff --git a/core/client/routes/editor.js b/core/client/routes/editor.js index 6b10c32b76..5bda901bdd 100644 --- a/core/client/routes/editor.js +++ b/core/client/routes/editor.js @@ -1,7 +1,8 @@ import ajax from 'ghost/utils/ajax'; import styleBody from 'ghost/mixins/style-body'; +import AuthenticatedRoute from 'ghost/routes/authenticated'; -var EditorRoute = Ember.Route.extend(styleBody, { +var EditorRoute = AuthenticatedRoute.extend(styleBody, { classNames: ['editor'], model: function (params) { diff --git a/core/client/routes/login.js b/core/client/routes/login.js new file mode 100644 index 0000000000..fae8160857 --- /dev/null +++ b/core/client/routes/login.js @@ -0,0 +1,32 @@ +import ajax from 'ghost/utils/ajax'; +import styleBody from 'ghost/mixins/style-body'; + +var isEmpty = Ember.isEmpty; + +var LoginRoute = Ember.Route.extend(styleBody, { + classNames: ['ghost-login'], + + actions: { + login: function () { + var self = this, + controller = this.get('controller'), + data = controller.getProperties('email', 'password'); + + if (!isEmpty(data.email) && !isEmpty(data.password)) { + + ajax('/ghost/api/v0.1/signin', data).then( + function (response) { + self.set('user', response); + self.transitionTo('posts'); + }, function () { + window.alert('Error'); // Todo Show notification + } + ); + } else { + window.alert('Must enter email + passwort. Todo: Must show as notification'); // Todo Show notification + } + } + } +}); + +export default LoginRoute; \ No newline at end of file diff --git a/core/client/routes/posts.js b/core/client/routes/posts.js index 95ef4f321f..85e871b1ff 100644 --- a/core/client/routes/posts.js +++ b/core/client/routes/posts.js @@ -1,7 +1,8 @@ import ajax from 'ghost/utils/ajax'; import styleBody from 'ghost/mixins/style-body'; +import AuthenticatedRoute from 'ghost/routes/authenticated'; -var PostsRoute = Ember.Route.extend(styleBody, { +var PostsRoute = AuthenticatedRoute.extend(styleBody, { classNames: ['manage'], model: function () { diff --git a/core/client/templates/application.hbs b/core/client/templates/application.hbs index 225707b2dc..eccf64a1fe 100644 --- a/core/client/templates/application.hbs +++ b/core/client/templates/application.hbs @@ -1,5 +1,7 @@ -{{partial "navbar"}} +{{#unless isLogin}} + {{partial "navbar"}} +{{/unless}}
{{outlet}} -
+ \ No newline at end of file diff --git a/core/client/templates/login.hbs b/core/client/templates/login.hbs new file mode 100644 index 0000000000..43bc0ffebe --- /dev/null +++ b/core/client/templates/login.hbs @@ -0,0 +1,19 @@ +
+ + +
+ +
+
\ No newline at end of file diff --git a/core/server/middleware/middleware.js b/core/server/middleware/middleware.js index 0f67ffe178..6658c66c53 100644 --- a/core/server/middleware/middleware.js +++ b/core/server/middleware/middleware.js @@ -31,7 +31,7 @@ var middleware = { authenticate: function (req, res, next) { var noAuthNeeded = [ '/ghost/signin/', '/ghost/signout/', '/ghost/signup/', - '/ghost/forgotten/', '/ghost/reset/' + '/ghost/forgotten/', '/ghost/reset/', '/ghost/ember/' ], subPath;