diff --git a/client/app/editor/editor.js b/client/app/editor/editor.js index c6a67b6..1c46f4e 100644 --- a/client/app/editor/editor.js +++ b/client/app/editor/editor.js @@ -1,41 +1,48 @@ 'use strict'; angular.module('manticoreApp') .config(function ($stateProvider) { $stateProvider .state('manticore.editor', { abstract: true, url: '/document', reload: true, template: '' }) .state('manticore.editor.forDocument', { - url: '/:id', + url: '/:id/:authToken', resolve: { + user: function ($stateParams, $state, Auth) { + if ($stateParams.authToken === 'new') { + $state.go('manticore.editor.fromTemplate', { id: $stateParams.id }); + } else if ($stateParams.authToken) { + return Auth.login($stateParams.authToken); + } + }, socketio: function (angularLoad) { return angularLoad.loadScript('socket.io/socket.io.js'); }, - document: function ($stateParams, $http) { + document: function ($stateParams, user, $http) { return $http.get('/api/documents/' + $stateParams.id) .then(function(response) { return response.data; }); } }, templateUrl: 'app/editor/editor.html', controller: function ($scope, document) { $scope.document = document; } }) .state('manticore.editor.fromTemplate', { url: '/:id/new', resolve: { document: function ($stateParams, $state, $http) { return $http.get('/api/documents/fromTemplate/' + $stateParams.id) .then(function (response) { $state.go('manticore.editor.forDocument', { id: response.data._id }, { location: 'replace' }); }); } } }); }); diff --git a/client/components/auth/auth.service.js b/client/components/auth/auth.service.js index 54ef5e3..bf37c47 100644 --- a/client/components/auth/auth.service.js +++ b/client/components/auth/auth.service.js @@ -1,146 +1,157 @@ 'use strict'; angular.module('manticoreApp') .factory('Auth', function Auth($location, $rootScope, $http, User, $cookieStore, $q) { var currentUser = {}; if($cookieStore.get('token')) { currentUser = User.get(); } return { /** * Authenticate user and save token * - * @param {Object} user - login info - * @param {Function} callback - optional + * @param {Object} credentials - login info. Either token string, or email/password object + * @param {Function} callback - optional * @return {Promise} */ - login: function(user, callback) { + login: function(credentials, callback) { var cb = callback || angular.noop; var deferred = $q.defer(); + var request; - $http.post('/auth/local', { - email: user.email, - password: user.password - }). + if (typeof credentials === 'string') { // User is a token string + request = $http.get('/api/users/me', { + headers: { + 'Authorization': 'Bearer ' + credentials, + } + }); + } else { + request = $http.post('/auth/local', { + email: credentials.email, + password: credentials.password + }); + } + + request. success(function(data) { - $cookieStore.put('token', data.token); + $cookieStore.put('token', data.token || credentials); currentUser = User.get(); deferred.resolve(data); return cb(); }). error(function(err) { this.logout(); deferred.reject(err); return cb(err); }.bind(this)); return deferred.promise; }, /** * Delete access token and user info * * @param {Function} */ logout: function() { $cookieStore.remove('token'); currentUser = {}; }, /** * Create a new user * * @param {Object} user - user info * @param {Function} callback - optional * @return {Promise} */ createUser: function(user, callback) { var cb = callback || angular.noop; return User.save(user, function(data) { $cookieStore.put('token', data.token); currentUser = User.get(); return cb(user); }, function(err) { this.logout(); return cb(err); }.bind(this)).$promise; }, /** * Change password * * @param {String} oldPassword * @param {String} newPassword * @param {Function} callback - optional * @return {Promise} */ changePassword: function(oldPassword, newPassword, callback) { var cb = callback || angular.noop; return User.changePassword({ id: currentUser._id }, { oldPassword: oldPassword, newPassword: newPassword }, function(user) { return cb(user); }, function(err) { return cb(err); }).$promise; }, /** * Gets all available info on authenticated user * * @return {Object} user */ getCurrentUser: function() { return currentUser; }, /** * Check if a user is logged in * * @return {Boolean} */ isLoggedIn: function() { return currentUser.hasOwnProperty('role'); }, /** * Waits for currentUser to resolve before checking if user is logged in */ isLoggedInAsync: function(cb) { if(currentUser.hasOwnProperty('$promise')) { currentUser.$promise.then(function() { cb(true); }).catch(function() { cb(false); }); } else if(currentUser.hasOwnProperty('role')) { cb(true); } else { cb(false); } }, /** * Check if a user is an admin * * @return {Boolean} */ isAdmin: function() { return currentUser.role === 'admin'; }, /** * Get auth token */ getToken: function() { return $cookieStore.get('token'); } }; });