diff --git a/Gruntfile.js b/Gruntfile.js index 92309b6..6c28824 100755 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -41,7 +41,7 @@ module.exports = function(grunt) { jshintrc : '.jshintrc' }, devise : [ 'src/*.js' ], - test : [ 'test/*.js', 'test/specs/*.js' ], + test : [ 'test/*.js', 'test/spec/*.js' ], }, plato: { @@ -71,6 +71,11 @@ module.exports = function(grunt) { continuous: { singleRun: false } + }, + + watch: { + files: ['src/*.js', 'test/*.js', 'test/spec/*.js'], + tasks: ['karma:unit'] } }); diff --git a/lib/devise-min.js b/lib/devise-min.js index 7447919..acc541d 100644 --- a/lib/devise-min.js +++ b/lib/devise-min.js @@ -1,10 +1,10 @@ // AngularDevise // ------------------- -// v0.3.0 +// v0.3.1 // // Copyright (c)2014 Justin Ridgewell // Distributed under MIT license // // https://github.com/cloudspace/angular_devise -"use strict";!function(a){var b=a.module("Devise",[]);b.factory("deviseInterceptor401",["$rootScope","$q",function(a,b){return{responseError:function(c){if(401===c.status&&!c.config.ignoreAuth){var d=b.defer();return a.$broadcast("devise:unauthorized",c,d),d.promise}return b.reject(c)}}}]).config(["$httpProvider",function(a){a.interceptors.push("deviseInterceptor401")}]),b.provider("Auth",function(){function b(a,b){var c={method:f[a].toLowerCase(),url:e[a],ignoreAuth:g};return b&&(c.data=b),c}function c(b,c){a.forEach(b,function(a,d){this[d+c]=function(a){return void 0===a?b[d]:(b[d]=a,this)}},this)}function d(a){return function(){return a}}var e={login:"/users/sign_in.json",logout:"/users/sign_out.json",register:"/users.json"},f={login:"POST",logout:"DELETE",register:"POST"},g=!1,h=function(a){return a.data};c.call(this,f,"Method"),c.call(this,e,"Path"),this.ignoreAuth=function(a){return void 0===a?g:(g=!!a,this)},this.parse=function(a){return"function"!=typeof a?h:(h=a,this)},this.$get=["$q","$http",function(a,c){function e(a){return g._currentUser=a,a}function f(){e(null)}var g={_currentUser:null,login:function(a){return a=a||{},c(b("login",{user:a})).then(h).then(e)},logout:function(){var a=d(g._currentUser);return c(b("logout")).then(f).then(a)},register:function(a){return a=a||{},c(b("register",{user:a})).then(h).then(e)},currentUser:function(){return g.isAuthenticated()?a.when(g._currentUser):g.login()},isAuthenticated:function(){return!!g._currentUser}};return g}]})}(angular); \ No newline at end of file +"use strict";!function(a){var b=a.module("Devise",[]);b.factory("deviseInterceptor401",["$rootScope","$q",function(a,b){return{responseError:function(c){if(401===c.status&&!c.config.ignoreAuth){var d=b.defer();return a.$broadcast("devise:unauthorized",c,d),d.promise}return b.reject(c)}}}]).config(["$httpProvider",function(a){a.interceptors.push("deviseInterceptor401")}]),b.provider("Auth",function(){function b(a,b){var c={method:f[a].toLowerCase(),url:e[a],ignoreAuth:g};return b&&(c.data=b),c}function c(b,c){a.forEach(b,function(a,d){this[d+c]=function(a){return void 0===a?b[d]:(b[d]=a,this)}},this)}function d(a){return function(){return a}}var e={login:"/users/sign_in.json",logout:"/users/sign_out.json",register:"/users.json",confirm:"/users/confirmation.json"},f={login:"POST",logout:"DELETE",register:"POST",confirm:"GET"},g=!1,h=function(a){return a.data};c.call(this,f,"Method"),c.call(this,e,"Path"),this.ignoreAuth=function(a){return void 0===a?g:(g=!!a,this)},this.parse=function(a){return"function"!=typeof a?h:(h=a,this)},this.$get=["$q","$http",function(a,c){function e(a){return g._currentUser=a,a}function f(){e(null)}var g={_currentUser:null,login:function(a){return a=a||{},c(b("login",{user:a})).then(h).then(e)},logout:function(){var a=d(g._currentUser);return c(b("logout")).then(f).then(a)},register:function(a){return a=a||{},c(b("register",{user:a})).then(h).then(e)},confirm:function(a){return a=a||{},c(b("confirm",a)).then(h).then(e)},currentUser:function(){return g.isAuthenticated()?a.when(g._currentUser):g.login()},isAuthenticated:function(){return!!g._currentUser}};return g}]})}(angular); \ No newline at end of file diff --git a/lib/devise.js b/lib/devise.js index 1ea15c5..106cf0d 100644 --- a/lib/devise.js +++ b/lib/devise.js @@ -30,7 +30,8 @@ var paths = { login: '/users/sign_in.json', logout: '/users/sign_out.json', - register: '/users.json' + register: '/users.json', + confirm: '/users/confirmation.json' }; /** * The default HTTP methods to use. @@ -38,7 +39,8 @@ var methods = { login: 'POST', logout: 'DELETE', - register: 'POST' + register: 'POST', + confirm: 'GET' }; /** * Set to true if 401 interception of the provider is not desired @@ -143,6 +145,10 @@ creds = creds || {}; return $http(httpConfig('register', { user: creds })).then(parse).then(save); }, + confirm: function (token) { + token = token || {}; + return $http(httpConfig('confirm', token)).then(parse).then(save); + }, currentUser: function () { if (service.isAuthenticated()) { return $q.when(service._currentUser); diff --git a/package.json b/package.json index 4cc01ea..634faa7 100755 --- a/package.json +++ b/package.json @@ -1,35 +1,36 @@ { - "name": "AngularDevise", - "version": "0.3.0", - "description": "A small AngularJS Service to interact with Devise Authentication.", - "main": "lib/devise.js", - "scripts": { - "test": "grunt test" - }, - "repository": { - "type": "git", - "url": "https://github.com/cloudspace/angular_devise.git" - }, - "keywords": [ - "Angular", - "AngularJS", - "Devise" - ], - "author": "Justin Ridgewell", - "license": "MIT", - "bugs": { - "url": "https://github.com/cloudspace/angular_devise/issues" - }, - "dependencies": {}, - "devDependencies": { - "grunt": "~0.4.1", - "grunt-ngmin": "~0.0.2", - "grunt-plato": "~0.2.1", - "grunt-preprocess": "~2.3.0", - "grunt-contrib-jshint": "~0.6.2", - "grunt-contrib-uglify": "~0.2.2", - "grunt-karma": "~0.6.2", - "load-grunt-tasks": "~0.2.0", - "time-grunt": "~0.2.1" - } + "name": "AngularDevise", + "version": "0.3.1", + "description": "A small AngularJS Service to interact with Devise Authentication.", + "main": "lib/devise.js", + "scripts": { + "test": "grunt test" + }, + "repository": { + "type": "git", + "url": "https://github.com/cloudspace/angular_devise.git" + }, + "keywords": [ + "Angular", + "AngularJS", + "Devise" + ], + "author": "Justin Ridgewell", + "license": "MIT", + "bugs": { + "url": "https://github.com/cloudspace/angular_devise/issues" + }, + "dependencies": {}, + "devDependencies": { + "grunt": "~0.4.1", + "grunt-ngmin": "~0.0.2", + "grunt-plato": "~0.2.1", + "grunt-preprocess": "~2.3.0", + "grunt-contrib-jshint": "~0.6.2", + "grunt-contrib-uglify": "~0.2.2", + "grunt-karma": "~0.6.2", + "load-grunt-tasks": "~0.2.0", + "time-grunt": "~0.2.1", + "grunt-contrib-watch": "^0.6.1" + } } diff --git a/src/auth.js b/src/auth.js index b4d0d73..22c9143 100644 --- a/src/auth.js +++ b/src/auth.js @@ -5,7 +5,8 @@ devise.provider('Auth', function AuthProvider() { var paths = { login: '/users/sign_in.json', logout: '/users/sign_out.json', - register: '/users.json' + register: '/users.json', + confirm: '/users/confirmation.json' }; /** @@ -14,7 +15,8 @@ devise.provider('Auth', function AuthProvider() { var methods = { login: 'POST', logout: 'DELETE', - register: 'POST' + register: 'POST', + confirm: 'GET' }; /** @@ -185,6 +187,28 @@ devise.provider('Auth', function AuthProvider() { return $http(httpConfig('register', {user: creds})).then(parse).then(save); }, + /** + * A confirm function to confirm + * with the server. + * + * The path and HTTP method used to login are configurable + * using + * + * angular.module('myModule', ['Devise']). + * config(function(AuthProvider) { + * AuthProvider.confirmPath('path/on/server.json'); + * AuthProvider.confirmMethod('GET'); + * }); + * + * @param {Object} [token] A confirmation token. + * @returns {Promise} A $http promise that will be resolved or + * rejected by the server. + */ + confirm: function(token) { + token = token || {}; + return $http(httpConfig('confirm', token)).then(parse).then(save); + }, + /** * A helper function that will return a promise with the currentUser. * Three different outcomes can happen: diff --git a/test/spec/auth.js b/test/spec/auth.js index dfc5ed4..b0a9e4f 100644 --- a/test/spec/auth.js +++ b/test/spec/auth.js @@ -60,6 +60,10 @@ describe('Provider: Devise.Auth', function () { testPathConfigure('register', 'POST'); }); + it('.confirmPath', function() { + testPathConfigure('confirm', 'GET'); + }); + it('.loginMethod', function() { testPathConfigure('login', 'GET', true); }); @@ -72,6 +76,10 @@ describe('Provider: Devise.Auth', function () { testPathConfigure('register', 'GET', true); }); + it('.confirmMethod', function() { + testPathConfigure('confirm', 'POST', true); + }); + it('.parse', function() { initService(function() { AuthProvider.parse(function(response) { @@ -240,6 +248,46 @@ describe('Provider: Devise.Auth', function () { }); }); + describe('.confirm', function() { + var user; + var postCallback; + function constantTrue() { + return true; + } + function callbackWraper(data) { + data = JSON.parse(data); + return postCallback(data); + } + + beforeEach(function() { + postCallback = constantTrue; + $httpBackend.expect('GET', '/users/confirmation.json', callbackWraper).respond({}); + }); + afterEach(function() { + $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingRequest(); + }); + + it('GETs to /users/confirmation.json', function() { + Auth.confirm(); + $httpBackend.flush(); + }); + + it('GETs token', function() { + var u = {confirmation_token: 'test'}; + postCallback = function(data) { + return jsonEquals(data, u); + }; + Auth.confirm(u); + $httpBackend.flush(); + }); + + it('returns a promise', function() { + expect(Auth.confirm().then).toBeDefined(); + $httpBackend.flush(); + }); + }); + describe('.currentUser', function() { describe('when authenticated', function() { var user;