-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add support for passport and authentication process purposal
This commit add the ability to have built-in authentication process in a hand clap time. It relies on passport, jsonwebtoken, and mongo-connect. It implements several features, for example an Auth service on client side with ready to use methods such `login` and `signup`. On a purposed future, this authentication process could be enrich with some Oauth methods (connect with google, github...) or maybe a simple lognup? To be continued...
- Loading branch information
Showing
26 changed files
with
649 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict'; | ||
|
||
angular.module('<%= appname %>') | ||
.service('Auth', function ($rootScope, $cookieStore, $q, $http) { | ||
|
||
var _user = {}; | ||
|
||
if($cookieStore.get('token')) { | ||
$http.get('/api/users/me') | ||
.then(function (res) { | ||
_user = res.data; | ||
}) | ||
.catch(function (err) { | ||
console.log(err); | ||
}); | ||
} | ||
|
||
/** | ||
* Signup | ||
* | ||
* @param user | ||
* @returns {promise} | ||
*/ | ||
this.signup = function (user) { | ||
var deferred = $q.defer(); | ||
$http.post('/api/users', user) | ||
.then(function (res) { | ||
_user = res.data.user; | ||
$cookieStore.put('token', res.data.token); | ||
deferred.resolve(); | ||
}) | ||
.catch(function (err) { | ||
deferred.reject(err.data); | ||
}); | ||
return deferred.promise; | ||
}; | ||
|
||
/** | ||
* Login | ||
* | ||
* @param user | ||
* @returns {promise} | ||
*/ | ||
this.login = function (user) { | ||
var deferred = $q.defer(); | ||
$http.post('/auth/local', user) | ||
.then(function (res) { | ||
_user = res.data.user; | ||
$cookieStore.put('token', res.data.token); | ||
deferred.resolve(); | ||
}) | ||
.catch(function (err) { | ||
deferred.reject(err.data); | ||
}); | ||
return deferred.promise; | ||
}; | ||
|
||
/** | ||
* Logout | ||
*/ | ||
this.logout = function () { | ||
$cookieStore.remove('token'); | ||
_user = {}; | ||
}; | ||
|
||
/** | ||
* Check if user is logged | ||
* | ||
* @returns {boolean} | ||
*/ | ||
this.isLogged = function () { | ||
return _user.hasOwnProperty('email'); | ||
}; | ||
|
||
/** | ||
* Returns the user | ||
* | ||
* @returns {object} | ||
*/ | ||
this.getUser = function () { | ||
return _user; | ||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
describe('Service: Auth', function () { | ||
|
||
beforeEach(module('<%= appname %>')); | ||
|
||
var Auth, | ||
$httpBackend, | ||
$cookieStore; | ||
|
||
beforeEach(inject(function (_Auth_, _$httpBackend_, _$cookieStore_) { | ||
Auth = _Auth_; | ||
$httpBackend = _$httpBackend_; | ||
$cookieStore = _$cookieStore_; | ||
$cookieStore.remove('token'); | ||
})); | ||
|
||
afterEach(function() { | ||
$httpBackend.verifyNoOutstandingExpectation(); | ||
$httpBackend.verifyNoOutstandingRequest(); | ||
}); | ||
|
||
it('should log user', function () { | ||
expect(Auth.isLogged()).toBe(false); | ||
Auth.login({ email: 'test@test.com', password: 'test' }); | ||
$httpBackend.expectPOST('/auth/local') | ||
.respond({ token: 'abcde', user: { email: 'test@test.com' } }); | ||
$httpBackend.flush(); | ||
expect($cookieStore.get('token')).toBe('abcde'); | ||
expect(Auth.getUser().email).toBe('test@test.com'); | ||
expect(Auth.isLogged()).toBe(true); | ||
}); | ||
|
||
}); |
32 changes: 32 additions & 0 deletions
32
app/templates/client/views/login(auth)/login.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
angular.module('<%= appname %>') | ||
.controller('LoginCtrl', function (Auth, $location) { | ||
|
||
var vm = this; | ||
|
||
angular.extend(vm, { | ||
|
||
name: 'LoginCtrl', | ||
|
||
/** | ||
* User credentials | ||
*/ | ||
user: { email: 'test@test.com', password: 'test' }, | ||
|
||
/** | ||
* Login method | ||
*/ | ||
login: function () { | ||
Auth.login(vm.user) | ||
.then(function () { | ||
$location.path('/'); | ||
}) | ||
.catch(function (err) { | ||
vm.error = err; | ||
}); | ||
} | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<div> | ||
{{ vm.name }} | ||
</div> | ||
|
||
<form ng-submit="vm.login()"> | ||
<input type="email" | ||
ng-model="vm.user.email" | ||
placeholder="email"> | ||
<input type="password" | ||
ng-model="vm.user.password" | ||
placeholder="password"> | ||
<button type="submit"> | ||
login | ||
</button> | ||
</form> | ||
|
||
<pre ng-if="vm.error">{{ vm.error | json }}</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
angular.module('<%= appname %>') | ||
.config(function ($routeProvider) { | ||
$routeProvider | ||
.when('/login', { | ||
templateUrl: 'views/login/login.html', | ||
controller: 'LoginCtrl', | ||
controllerAs: 'vm' | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
describe('Controller: LoginCtrl', function () { | ||
|
||
beforeEach(module('<%= appname %>')); | ||
|
||
var LoginCtrl, | ||
$httpBackend, | ||
$location; | ||
|
||
beforeEach(inject(function ($controller, _$httpBackend_, _$location_) { | ||
LoginCtrl = $controller('LoginCtrl', {}); | ||
$httpBackend = _$httpBackend_; | ||
$location = _$location_; | ||
})); | ||
|
||
afterEach(function() { | ||
$httpBackend.verifyNoOutstandingExpectation(); | ||
$httpBackend.verifyNoOutstandingRequest(); | ||
}); | ||
|
||
it('should redirect to / after successful login', function () { | ||
LoginCtrl.login({ email: 'test@test.com', password: 'test' }); | ||
$httpBackend.expectPOST('/auth/local') | ||
.respond({ token: 'token' }); | ||
$httpBackend.flush(); | ||
expect($location.path()).toBe('/'); | ||
}); | ||
|
||
}); |
32 changes: 32 additions & 0 deletions
32
app/templates/client/views/signup(auth)/signup.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
angular.module('<%= appname %>') | ||
.controller('SignupCtrl', function (Auth, $location) { | ||
|
||
var vm = this; | ||
|
||
angular.extend(vm, { | ||
|
||
name: 'SignupCtrl', | ||
|
||
/** | ||
* User credentials | ||
*/ | ||
user: { email: 'test@test.com', password: 'test' }, | ||
|
||
/** | ||
* Signup | ||
*/ | ||
signup: function () { | ||
Auth.signup(vm.user) | ||
.then(function () { | ||
$location.path('/'); | ||
}) | ||
.catch(function (err) { | ||
vm.error = err; | ||
}); | ||
} | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<div> | ||
{{ vm.name }} | ||
</div> | ||
|
||
<form ng-submit="vm.signup()"> | ||
<input type="email" | ||
ng-model="vm.user.email" | ||
placeholder="email"> | ||
<input type="password" | ||
ng-model="vm.user.password" | ||
placeholder="password"> | ||
<button type="submit"> | ||
signup | ||
</button> | ||
</form> | ||
|
||
<pre ng-if="vm.error">{{ vm.error | json }}</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
angular.module('<%= appname %>') | ||
.config(function ($routeProvider) { | ||
$routeProvider | ||
.when('/signup', { | ||
templateUrl: 'views/signup/signup.html', | ||
controller: 'SignupCtrl', | ||
controllerAs: 'vm' | ||
}); | ||
}); |
Oops, something went wrong.