Skip to content

Commit

Permalink
Merge pull request #112 from spira/feature/navigation-structure
Browse files Browse the repository at this point in the history
Feature/navigation structure
  • Loading branch information
Jeremy Sik committed Jul 30, 2015
2 parents abc3b4b + 22fe07b commit 3eee79b
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/src/app/_partials/navigation/navigation.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</md-list-item>

<md-list-item>
<pre>{{NavigationController.ngJwtAuthService.user|json}}</pre>
Logged in as <strong>{{NavigationController.loggedInUser.fullName()}}</strong>
</md-list-item>

<md-list-item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/_partials/navigation/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ module app.partials.navigation {
public navigableStates:global.IState[] = [];

static $inject = ['stateHelperService', '$window', 'ngJwtAuthService', '$state'];
public loggedInUser:common.models.User;

constructor(protected stateHelperService:common.providers.StateHelperService,
private $window:global.IWindowService,
protected ngJwtAuthService:NgJwtAuth.NgJwtAuthService,
protected $state:ng.ui.IStateService) {

this.navigableStates = this.getNavigationStates();
this.loggedInUser = <common.models.User>(<any>ngJwtAuthService).user;
}

protected getNavigationStates():global.IState[] {
Expand Down
7 changes: 6 additions & 1 deletion app/src/app/admin/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ module app.admin.users {

static $inject = ['allUsers'];

constructor(public allUsers:global.IUser[]) {
constructor(public allUsers:common.models.User[]) {

this.allUsers = allUsers.map((user:common.models.User) => {
(<any>user).fullname = user.fullName();
return user;
})

}

Expand Down
1 change: 1 addition & 0 deletions app/src/app/guest/login/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('Login', () => {

it('should have initialised the auth service', () => {

$scope.$apply();
expect((<any>authService).refreshTimerPromise).to.be.ok;

});
Expand Down
8 changes: 6 additions & 2 deletions app/src/app/guest/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ module app.guest.login {

class LoginInit {

static $inject = ['$rootScope', 'ngJwtAuthService', '$mdDialog', '$timeout', '$window', '$state'];
static $inject = ['$rootScope', 'ngJwtAuthService', '$mdDialog', '$timeout', '$window', '$state', '$q'];
constructor(
private $rootScope:global.IRootScope,
private ngJwtAuthService:NgJwtAuth.NgJwtAuthService,
private $mdDialog:ng.material.IDialogService,
private $timeout:ng.ITimeoutService,
private $window:ng.IWindowService,
private $state:ng.ui.IStateService
private $state:ng.ui.IStateService,
private $q:ng.IQService
) {

ngJwtAuthService
.registerUserFactory((subClaim: string, tokenData: global.JwtAuthClaims): ng.IPromise<common.models.User> => {
return this.$q.when(new common.models.User(tokenData._user));
})
.registerLoginPromptFactory((deferredCredentials:ng.IDeferred<NgJwtAuth.ICredentials>, loginSuccessPromise:ng.IPromise<NgJwtAuth.IUser>, currentUser:NgJwtAuth.IUser): ng.IPromise<any> => {

let dialogConfig:ng.material.IDialogOptions = {
Expand Down
27 changes: 27 additions & 0 deletions app/src/common/models/userModel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
///<reference path="../../../build/js/declarations.d.ts" />

(() => {

let seededChance = new Chance(1);

describe('User Model', () => {

it('should return the user\'s full name', () => {

let userData:global.IUserData = {
userId:seededChance.guid(),
email:seededChance.email(),
firstName:seededChance.first(),
lastName:seededChance.last(),
};

let user = new common.models.User(userData);

expect(user.fullName()).to.equal(userData.firstName + ' '+userData.lastName);


});

});

})();
29 changes: 29 additions & 0 deletions app/src/common/models/userModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module common.models {

export class User implements global.IUserData{

public userId:string;
public email:string;
public firstName:string;
public lastName:string;
public _userCredential:global.IUserCredential;

constructor(data:global.IUserData) {

_.assign(this, data);

}

public fullName() {
return this.firstName + ' ' + this.lastName;
}

}
//
//angular.module(namespace, [])
// .service('userService', UserService);

}



2 changes: 1 addition & 1 deletion app/src/common/services/articleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module common.services.article {
title:string;
body:string;
permalink:string;
_author?:global.IUser;
_author?:common.models.User;
}

export class ArticleService {
Expand Down
6 changes: 3 additions & 3 deletions app/src/common/services/userService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
buildUser: (overrides = {}) => {

let userId = seededChance.guid();
let defaultUser:global.IUser = {
let defaultUser:global.IUserData = {
_self: '/users/'+userId,
userId: userId,
email: seededChance.email(),
Expand All @@ -22,8 +22,8 @@

return _.merge(defaultUser, overrides);
},
get user():global.IUser {
return fixtures.buildUser();
get user():common.models.User {
return new common.models.User(fixtures.buildUser());
},
get users() {
return _.range(10).map(() => fixtures.user);
Expand Down
45 changes: 29 additions & 16 deletions app/src/common/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,35 @@ module common.services.user {
export class UserService {

static $inject:string[] = ['ngRestAdapter', 'ngJwtAuthService', '$q', '$mdDialog'];
constructor(
private ngRestAdapter: NgRestAdapter.INgRestAdapterService,
private ngJwtAuthService:NgJwtAuth.NgJwtAuthService,
private $q:ng.IQService,
private $mdDialog:ng.material.IDialogService
) {

constructor(private ngRestAdapter:NgRestAdapter.INgRestAdapterService,
private ngJwtAuthService:NgJwtAuth.NgJwtAuthService,
private $q:ng.IQService,
private $mdDialog:ng.material.IDialogService) {

}

/**
* Get an instance of a user from data
* @param userData
* @returns {common.services.user.User}
*/
public userFactory(userData:global.IUserData):common.models.User {
return new common.models.User(userData);
}

/**
* Get all users from the API
* @returns {any}
*/
public getAllUsers():ng.IPromise<global.IUser[]>{
public getAllUsers():ng.IPromise<common.models.User[]> {

return this.ngRestAdapter.get('/users')
.then((res) => {
return <global.IUser[]>res.data;

return _.map(res.data, (userData:global.IUserData) => {
return new common.models.User(userData);
});
})
;

Expand All @@ -34,11 +45,11 @@ module common.services.user {
* @param password
* @param firstName
* @param lastName
* @returns {IPromise<global.IUser>}
* @returns {IPromise<global.IUserData>}
*/
private register(email:string, password:string, firstName:string, lastName:string):ng.IPromise<global.IUser>{
private register(email:string, password:string, firstName:string, lastName:string):ng.IPromise<global.IUserData> {

let user:global.IUser = {
let userData:global.IUserData = {
userId: this.ngRestAdapter.uuid(),
email: email,
firstName: firstName,
Expand All @@ -49,7 +60,9 @@ module common.services.user {
}
};

return this.ngRestAdapter.put('/users/'+user.userId, user)
let user = new common.models.User(userData);

return this.ngRestAdapter.put('/users/' + user.userId, user)
.then(() => user); //return this user object
}

Expand All @@ -61,13 +74,13 @@ module common.services.user {
* @param lastName
* @returns {IPromise<TResult>}
*/
public registerAndLogin(email:string, password:string, firstName:string, lastName:string):ng.IPromise<any>{
public registerAndLogin(email:string, password:string, firstName:string, lastName:string):ng.IPromise<any> {

return this.register(email, password, firstName, lastName)
.then((user) => {
.then((user:common.models.User) => {
return this.ngJwtAuthService.authenticateCredentials(user.email, user._userCredential.password);
})
;
;

}

Expand All @@ -82,7 +95,7 @@ module common.services.user {
.skipInterceptor()
.head('/users/email/' + email)
.then(() => true, () => false) //200 OK is true (email exists) 404 is false (email not registered)
;
;

}

Expand Down
6 changes: 5 additions & 1 deletion app/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare module global {
password: string;
}

export interface IUser extends NgJwtAuth.IUser {
export interface IUserData extends NgJwtAuth.IUser {
userId:string;
firstName:string; //make compulsory
lastName:string; //make compulsory
Expand All @@ -41,5 +41,9 @@ declare module global {
socialLogin(type:string, redirectState?:string, redirectStateParams?:Object);
}

export interface JwtAuthClaims extends NgJwtAuth.IJwtClaims{
_user: IUserData;
}


}

0 comments on commit 3eee79b

Please sign in to comment.