diff --git a/src/generators/app/index.js b/src/generators/app/index.js index 0f3cde8b5..6ab2356dc 100644 --- a/src/generators/app/index.js +++ b/src/generators/app/index.js @@ -174,13 +174,13 @@ export class Generator extends Base { choices: ['CSS', 'Sass', 'Stylus', 'Less'], filter: val => val.toLowerCase() }, { - type: 'list', - name: 'router', - default: 1, - message: 'What Angular router would you like to use?', - choices: ['ngRoute', 'uiRouter'], - filter: val => val.toLowerCase() - }, { + // type: 'list', + // name: 'router', + // default: 1, + // message: 'What Angular router would you like to use?', + // choices: ['ngRoute', 'uiRouter'], + // filter: val => val.toLowerCase() + //}, { type: 'confirm', name: 'bootstrap', message: 'Would you like to include Bootstrap?' @@ -203,8 +203,9 @@ export class Generator extends Base { this.filters[answers.stylesheet] = true; insight.track('stylesheet', answers.stylesheet); - this.filters[answers.router] = true; - insight.track('router', answers.router); + //this.filters[answers.router] = true; + //insight.track('router', answers.router); + this.filters['ngroute'] = true; this.filters.bootstrap = !!answers.bootstrap; insight.track('bootstrap', !!answers.bootstrap); diff --git a/templates/app/_package.json b/templates/app/_package.json index 3ca824ff0..ceea38d0a 100644 --- a/templates/app/_package.json +++ b/templates/app/_package.json @@ -54,16 +54,16 @@ "@angularclass/match-control": "^2.0.0", "reflect-metadata": "^0.1.3", "rxjs": "^5.3.0", - "zone.js": "^0.7.2", - "@angular/common": "~2.4.9", - "@angular/compiler": "~2.4.9", - "@angular/core": "~2.4.9", - "@angular/forms": "~2.4.9", - "@angular/http": "~2.4.9", + "zone.js": "^0.8.4", + "@angular/common": "^4.3.3", + "@angular/compiler": "^4.3.3", + "@angular/core": "^4.3.3", + "@angular/forms": "^4.3.3", + "@angular/http": "^4.3.3", + "@angular/platform-browser": "^4.3.3", + "@angular/platform-browser-dynamic": "^4.3.3", + "@angular/router": "^4.3.3", <%#"@angular/material": "^2.0.0-alpha.10",%> - "@angular/platform-browser": "~2.4.9", - "@angular/platform-browser-dynamic": "~2.4.9", - "@angular/router": "^3.4.7", "@angularclass/hmr": "^1.2.1", <%_ if(filters.ts) { -%> "@types/core-js": "^0.9.41", @@ -82,7 +82,7 @@ "@types/webpack": "^2.2.15", <%_ } -%> "angular2-universal": "2.1.0-rc.1", - "angular2-jwt": "^0.1.24", + "angular2-jwt": "^0.1.28", <% if(filters.auth) { %> "angular-validation-match": "^1.9.0",<% } %> <% if(filters.uirouter) { %> diff --git a/templates/app/client/__index.html b/templates/app/client/__index.html index c934094a7..ccdf4f911 100644 --- a/templates/app/client/__index.html +++ b/templates/app/client/__index.html @@ -1,9 +1,9 @@ + - Angular Full-Stack Generator @@ -25,9 +25,6 @@ ga('send', 'pageview'); - LOADING - - <% if (filters.ngroute) { %>
<% } %><% if (filters.uirouter) { %>
<% } %> - + Loading... diff --git a/templates/app/client/app/account(auth)/account.module.js b/templates/app/client/app/account(auth)/account.module.js index 11765a09f..ff3af0ba6 100644 --- a/templates/app/client/app/account(auth)/account.module.js +++ b/templates/app/client/app/account(auth)/account.module.js @@ -3,15 +3,29 @@ import { FormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; <%_ if (filters.uirouter) { -%> import { UIRouterModule } from 'ui-router-ng2';<% } %> -<%_ if (filters.ngroute) { -%><% } %> +<%_ if (filters.ngroute) { -%> +import { RouterModule, Routes } from '@angular/router';<% } %> <%_ if(filters.oauth) { -%> import { DirectivesModule } from '../../components/directives.module';<% } %> -import { STATES } from './account.routes'; +//import { STATES } from './account.routes'; import { LoginComponent } from './login/login.component'; -import { SignupComponent } from './signup/signup.component'; import { SettingsComponent } from './settings/settings.component'; +import { SignupComponent } from './signup/signup.component'; + +<%_ if (filters.ngroute) { -%> +const accountRoutes: Routes = [{ + path: 'login', + component: LoginComponent, + //data: { title: 'Home' } +}, { + path: 'settings', + component: SettingsComponent, +}, { + path: 'signup', + component: SignupComponent, +}];<% } %> @NgModule({ imports: [ @@ -21,7 +35,8 @@ import { SettingsComponent } from './settings/settings.component'; UIRouterModule.forChild({ states: STATES, }),<% } %> - <%_ if (filters.ngroute) { -%><% } %> + <%_ if (filters.ngroute) { -%> + RouterModule.forChild(accountRoutes),<% } %> <%_ if(filters.oauth) { -%> DirectivesModule,<% } %> ], diff --git a/templates/app/client/app/account(auth)/login/login.component.js b/templates/app/client/app/account(auth)/login/login.component.js index 4bc1d847e..4e900a46b 100644 --- a/templates/app/client/app/account(auth)/login/login.component.js +++ b/templates/app/client/app/account(auth)/login/login.component.js @@ -1,7 +1,8 @@ import { Component } from '@angular/core'; <%_ if(filters.uirouter) { -%> import { StateService } from 'ui-router-ng2';<% } %> -<%_ if(filters.ngroute) { -%><% } %> +<%_ if(filters.ngroute) { -%> +import { Router } from '@angular/router';<% } %> import { AuthService } from '../../../components/auth/auth.service'; // @flow @@ -33,14 +34,16 @@ export class LoginComponent { errors = {login: undefined}; submitted = false; AuthService; + Router; <%_ if(filters.ngroute) { -%><% } %> <%_ if(filters.uirouter) { -%> StateService;<% } %> - static parameters = [AuthService, <% if(filters.ngroute) { %><% } else { %>StateService<% } %>]; - constructor(_AuthService_: AuthService, <% if(filters.ngroute) { %><% } else { %>_StateService_: StateService<% } %>) { + static parameters = [AuthService, <% if(filters.ngroute) { %>Router<% } else { %>StateService<% } %>]; + constructor(_AuthService_: AuthService, <% if(filters.ngroute) { %>router: Router<% } else { %>_StateService_: StateService<% } %>) { this.AuthService = _AuthService_; - <%_ if(filters.ngroute) { -%><% } %> + <%_ if(filters.ngroute) { -%> + this.Router = router;<% } %> <%_ if(filters.uirouter) { -%> this.StateService = _StateService_;<% } %> } @@ -54,7 +57,10 @@ export class LoginComponent { }) .then(() => { // Logged in, redirect to home - this.StateService.go('main'); + <%_ if(filters.ngroute) { -%> + this.Router.navigateByUrl('/home');<% } %> + <%_ if(filters.uirouter) { -%> + this.StateService.go('main');<% } %> }) .catch(err => { this.errors.login = err.message; diff --git a/templates/app/client/app/account(auth)/settings/settings.component.js b/templates/app/client/app/account(auth)/settings/settings.component.js index e426e94e3..c19ee87aa 100644 --- a/templates/app/client/app/account(auth)/settings/settings.component.js +++ b/templates/app/client/app/account(auth)/settings/settings.component.js @@ -40,16 +40,14 @@ export class SettingsComponent { changePassword(form) { this.submitted = true; - if(form.$valid) { - this.AuthService.changePassword(this.user.oldPassword, this.user.newPassword) - .then(() => { - this.message = 'Password successfully changed.'; - }) - .catch(() => { - form.password.$setValidity('mongoose', false); - this.errors.other = 'Incorrect password'; - this.message = ''; - }); - } + return this.AuthService.changePassword(this.user.oldPassword, this.user.newPassword) + .then(() => { + this.message = 'Password successfully changed.'; + }) + .catch(() => { + // form.password.$setValidity('mongoose', false); + this.errors.other = 'Incorrect password'; + this.message = ''; + }); } } diff --git a/templates/app/client/app/account(auth)/settings/settings.html b/templates/app/client/app/account(auth)/settings/settings.html index eed1686fd..6a33253a6 100644 --- a/templates/app/client/app/account(auth)/settings/settings.html +++ b/templates/app/client/app/account(auth)/settings/settings.html @@ -9,20 +9,19 @@

Change Password

- -

- {{ errors.other }} + +

+ Required

- +

+ [hidden]="newPassword.valid || !submitted"> Password must be at least 3 characters.

@@ -30,12 +29,11 @@

Change Password

- + minlength="3"/>

+ [hidden]="confirmPassword.valid || !submitted"> Passwords must match.

diff --git a/templates/app/client/app/account(auth)/signup/signup.component.js b/templates/app/client/app/account(auth)/signup/signup.component.js index 96f051bc6..551e675d9 100644 --- a/templates/app/client/app/account(auth)/signup/signup.component.js +++ b/templates/app/client/app/account(auth)/signup/signup.component.js @@ -2,9 +2,10 @@ import { Component } from '@angular/core'; <%_ if(filters.uirouter) { -%> import { StateService } from 'ui-router-ng2';<% } %> -<%_ if(filters.ngroute) { -%><% } %> +<%_ if(filters.ngroute) { -%> +import { Router } from '@angular/router'; +<% } %> import { AuthService } from '../../../components/auth/auth.service'; -import {ANGULARCLASS_MATCH_CONTROL_DIRECTIVES} from '@angularclass/match-control'; <%_ if(filters.flow) { -%> type User = { @@ -21,8 +22,7 @@ interface User { @Component({ selector: 'signup', - template: require('./signup.<%=templateExt%>'), - directives: [...ANGULARCLASS_MATCH_CONTROL_DIRECTIVES] + template: require('./signup.<%=templateExt%>') }) export class SignupComponent { user: User = { @@ -33,14 +33,16 @@ export class SignupComponent { errors = {}; submitted = false; AuthService; - <%_ if(filters.ngroute) { -%><% } %> + <%_ if(filters.ngroute) { -%> + Router;<% } %> <%_ if(filters.uirouter) { -%> StateService;<% } %> - static parameters = [AuthService, <% if(filters.ngroute) { %><% } else { %>StateService<% } %>]; - constructor(_AuthService_: AuthService, <% if(filters.ngroute) { %><% } else { %>_StateService_: StateService<% } %>) { + static parameters = [AuthService, <% if(filters.ngroute) { %>Router<% } else { %>StateService<% } %>]; + constructor(_AuthService_: AuthService, <% if(filters.ngroute) { %>router: Router<% } else { %>_StateService_: StateService<% } %>) { this.AuthService = _AuthService_; - <%_ if(filters.ngroute) { -%><% } -%> + <%_ if(filters.ngroute) { -%> + this.Router = router;<% } -%> <%_ if(filters.uirouter) { -%> this.StateService = _StateService_;<% } -%> } @@ -55,7 +57,7 @@ export class SignupComponent { }) .then(() => { // Account created, redirect to home - <% if(filters.ngroute) { %>this.$location.path('/');<% } -%> + <% if(filters.ngroute) { %>this.Router.navigateByUrl('/home');<% } -%> <% if(filters.uirouter) { %>this.StateService.go('main');<% } -%> }) .catch(err => { diff --git a/templates/app/client/app/admin(auth)/admin.component.js b/templates/app/client/app/admin(auth)/admin.component.js index 738dccbf8..d1c5dbe7c 100644 --- a/templates/app/client/app/admin(auth)/admin.component.js +++ b/templates/app/client/app/admin(auth)/admin.component.js @@ -12,7 +12,7 @@ export class AdminComponent { <%_ } _%> static parameters = [UserService]; - constructor(userService: UserService) { + constructor(<%= private() %>userService: UserService) { this.userService = userService; // Use the user service to fetch all users this.userService.query().subscribe(users => { diff --git a/templates/app/client/app/admin(auth)/admin.module.js b/templates/app/client/app/admin(auth)/admin.module.js index 15a7336f5..e165f9e41 100644 --- a/templates/app/client/app/admin(auth)/admin.module.js +++ b/templates/app/client/app/admin(auth)/admin.module.js @@ -7,16 +7,19 @@ import { RouterModule, Routes } from '@angular/router';<% } %> import { AdminComponent } from './admin.component'; -<%_ if(filters.ngroute) { -%> -import { ROUTES } from './admin.routes';<% } %> <%_ if(filters.uirouter) { -%> import { STATES } from './admin.routes';<% } %> +<%_ if (filters.ngroute) { -%> +const adminRoutes: Routes = [{ + path: 'admin', + component: AdminComponent, +}];<% } %> @NgModule({ imports: [ BrowserModule, <%_ if(filters.ngroute) { _%> - RouterModule.forChild(ROUTES),<% } %> + RouterModule.forChild(adminRoutes),<% } %> <%_ if(filters.uirouter) { _%> UIRouterModule.forChild({ states: STATES, diff --git a/templates/app/client/app/app.component.js b/templates/app/client/app/app.component.js index 0a4f290ce..7acc50c2a 100644 --- a/templates/app/client/app/app.component.js +++ b/templates/app/client/app/app.component.js @@ -3,7 +3,7 @@ import { Component } from '@angular/core'; @Component({ selector: 'app', template: ` - + <% if (filters.ngroute) { %><% } %><% if (filters.uirouter) { %><% } %> ` }) export class AppComponent {} diff --git a/templates/app/client/app/app.constants.js b/templates/app/client/app/app.constants.js index 39adeaea5..30d84c9e8 100644 --- a/templates/app/client/app/app.constants.js +++ b/templates/app/client/app/app.constants.js @@ -1,6 +1,6 @@ <%_ if(filters.babel) { -%> export default from '../../server/config/environment/shared';<% } %> <%_ if(filters.ts) { -%> -import shared from '../../server/config/environment/shared'; +import shared from '../../server/config/environment/shared.js'; export default shared;<% } %> diff --git a/templates/app/client/app/app.module.js b/templates/app/client/app/app.module.js index 33eda9b50..46d78a47d 100644 --- a/templates/app/client/app/app.module.js +++ b/templates/app/client/app/app.module.js @@ -1,57 +1,13 @@ -// import angular from 'angular'; -// // import ngAnimate from 'angular-animate'; -// import ngCookies from 'angular-cookies'; -// import ngResource from 'angular-resource'; -// import ngSanitize from 'angular-sanitize'; -// <%_ if(filters.socketio) { _%> -// import 'angular-socket-io';<% } %> -// <%_ if(filters.ngroute) { _%> -// const ngRoute = require('angular-route');<% } %> -// <%_ if(filters.uirouter) { _%> -// import uiRouter from 'angular-ui-router';<% } %> -// <%_ if(filters.uibootstrap) { _%> -// import uiBootstrap from 'angular-ui-bootstrap';<% } %> -// // import ngMessages from 'angular-messages'; -// <%_ if(filters.auth) { _%> -// // import ngValidationMatch from 'angular-validation-match';<% } %> - -// import {routeConfig} from './app.config'; - -// <%_ if(filters.auth) { _%> -// import _Auth from '../components/auth/auth.module'; -// import account from './account'; -// import admin from './admin';<% } %> -// import navbar from '../components/navbar/navbar.component'; -// import footer from '../components/footer/footer.component'; -// import main from './main/main.component'; -// import constants from './app.constants'; -// import util from '../components/util/util.module'; -// <%_ if(filters.socketio) { _%> -// import socket from '../components/socket/socket.service';<% } %> - -// .config(routeConfig) -// <%_ if(filters.auth) { _%> -// .run(function($rootScope, $location, Auth) { -// 'ngInject'; -// // Redirect to login if route requires auth and you're not logged in -// $rootScope.$on('$stateChangeStart', function(event, next) { -// Auth.isLoggedIn(function(loggedIn) { -// if(next.authenticate && !loggedIn) { -// $location.path('/login'); -// } -// }); -// }); -// })<% } %>; - - import { NgModule, ErrorHandler, Injectable, ApplicationRef, + Provider, } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { + Http, HttpModule, BaseRequestOptions, RequestOptions, @@ -60,28 +16,36 @@ import { import { removeNgStyles, createNewHosts, - disposeOldHosts, createInputTransfer, - restoreInputValues, } from '@angularclass/hmr'; <%_ if (filters.uirouter) { -%> import { UIRouterModule } from 'ui-router-ng2';<% } %> -import { provideAuth } from 'angular2-jwt'; +<%_ if (filters.ngroute) { -%> +import { RouterModule, Routes } from '@angular/router';<% } %> +import { provideAuth, AuthHttp, AuthConfig } from 'angular2-jwt'; import { AppComponent } from './app.component'; import { MainModule } from './main/main.module'; +import { MainComponent } from './main/main.component'; import { DirectivesModule } from '../components/directives.module'; import { AccountModule } from './account/account.module'; -import { AdminModule } from './admin/admin.module'; +//import { AdminModule } from './admin/admin.module'; import constants from './app.constants'; -let providers = [ - provideAuth({ - // Allow using AuthHttp while not logged in +export function getAuthHttp(http) { + return new AuthHttp(new AuthConfig({ noJwtError: true, - }) -]; + globalHeaders: [{'Accept': 'application/json'}], + tokenGetter: (() => localStorage.getItem('id_token')), + }), http); +} + +let providers: Provider[] = [{ + provide: AuthHttp, + useFactory: getAuthHttp, + deps: [Http] +}]; if(constants.env === 'development') { @Injectable() @@ -95,16 +59,34 @@ if(constants.env === 'development') { providers.push({ provide: RequestOptions, useClass: HttpOptions }); } +const appRoutes: Routes = [ + //{ path: 'crisis-center', component: CrisisListComponent }, + //{ path: 'hero/:id', component: HeroDetailComponent }, + { + path: 'home', + component: MainComponent, + data: { title: 'Home' } + }, + { path: '', + redirectTo: '/home', + pathMatch: 'full' + }, + //{ path: '**', component: PageNotFoundComponent } +]; + @NgModule({ providers, imports: [ BrowserModule, HttpModule, - UIRouterModule.forRoot(), + <%_ if (filters.uirouter) { -%> + UIRouterModule.forRoot(),<% } %> + <%_ if (filters.ngroute) { -%> + RouterModule.forRoot(appRoutes, { enableTracing: process.env.NODE_ENV === 'development' }),<% } %> MainModule, DirectivesModule, AccountModule, - AdminModule, + //AdminModule, ], declarations: [ AppComponent, @@ -113,7 +95,7 @@ if(constants.env === 'development') { }) export class AppModule { static parameters = [ApplicationRef]; - constructor(appRef/*: ApplicationRef*/) { + constructor(<%= private() %>appRef: ApplicationRef) { this.appRef = appRef; } diff --git a/templates/app/client/app/main/main.component.js b/templates/app/client/app/main/main.component.js index 79d0bdc79..541cebab6 100644 --- a/templates/app/client/app/main/main.component.js +++ b/templates/app/client/app/main/main.component.js @@ -15,16 +15,15 @@ export class MainComponent implements OnInit<% if(filters.ws) { %>, OnDestroy<% <%_ if(filters.models) { -%> newThing = '';<% } %> - <%_ if(filters.babel) { -%> - static parameters = [Http, SocketService];<% } %> + static parameters = [Http, SocketService]; constructor(<%= private() %>http: Http<% if(filters.ws) { %>, <%= private() %>socketService: SocketService<% } %>) { - this.Http = http; + this.http = http; <%_ if(filters.ws) { -%> this.SocketService = socketService;<% } %> } ngOnInit() { - this.Http.get('/api/things') + this.http.get('/api/things') .map(res => { return res.json(); }) @@ -46,7 +45,7 @@ export class MainComponent implements OnInit<% if(filters.ws) { %>, OnDestroy<% let text = this.newThing; this.newThing = ''; - return this.Http.post('/api/things', { name: text }) + return this.http.post('/api/things', { name: text }) .map(res => res.json()) .catch(err => Observable.throw(err.json().error || 'Server error')) .subscribe(thing => { @@ -56,7 +55,7 @@ export class MainComponent implements OnInit<% if(filters.ws) { %>, OnDestroy<% } deleteThing(thing) { - return this.Http.delete(`/api/things/${thing._id}`) + return this.http.delete(`/api/things/${thing._id}`) .map(res => res.json()) .catch(err => Observable.throw(err.json().error || 'Server error')) .subscribe(() => { diff --git a/templates/app/client/app/main/main.component.spec.js b/templates/app/client/app/main/main.component.spec.js index 4c3f7ea33..80ff15d93 100644 --- a/templates/app/client/app/main/main.component.spec.js +++ b/templates/app/client/app/main/main.component.spec.js @@ -1,3 +1,4 @@ +/* 'use strict'; import main from './main.component'; @@ -44,3 +45,4 @@ describe('Component: MainComponent', function() { <%= expect() %>mainComponent.awesomeThings.length<%= to() %>.equal(4);<% } %> }); }); +*/ diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index a99cf9366..a62fe8fb0 100644 --- a/templates/app/client/components/auth(auth)/auth.service.js +++ b/templates/app/client/components/auth(auth)/auth.service.js @@ -6,7 +6,7 @@ import { Http } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/toPromise'; import { safeCb, extractData } from '../util'; -import { userRoles } from '../../app/app.constants'; +import constants from '../../app/app.constants'; // @flow class User { @@ -19,19 +19,19 @@ class User { @Injectable() export class AuthService { - _currentUser: User = {}; + _currentUser: User = new User(); @Output() currentUserChanged = new EventEmitter(true); - userRoles = userRoles || []; + userRoles = constants.userRoles || []; static parameters = [Http, AuthHttp, UserService]; - constructor(_Http_: Http, _AuthHttp_: AuthHttp, _UserService_: UserService) { - this.Http = _Http_; - this.AuthHttp = _AuthHttp_; - this.UserService = _UserService_; + constructor(<%= private() %>http: Http, <%= private() %>authHttp: AuthHttp, <%= private() %>userService: UserService) { + this.http = http; + this.authHttp = authHttp; + this.userService = userService; if(localStorage.getItem('id_token')) { - this.UserService.get().toPromise() - .then(user => { + this.userService.get().toPromise() + .then((user:User) => { this.currentUser = user; }) .catch(err => { @@ -48,7 +48,7 @@ export class AuthService { * @param {String} role - role to check against */ static hasRole(userRole, role) { - return userRoles.indexOf(userRole) >= userRoles.indexOf(role); + return constants.userRoles.indexOf(userRole) >= constants.userRoles.indexOf(role); } get currentUser() { @@ -68,7 +68,7 @@ export class AuthService { * @return {Promise} */ login({email, password}, callback) { - return this.Http.post('/auth/local', { + return this.http.post('/auth/local', { email, password }) @@ -76,9 +76,9 @@ export class AuthService { .then(extractData) .then(res => { localStorage.setItem('id_token', res.token); - return this.UserService.get().toPromise(); + return this.userService.get().toPromise(); }) - .then(user => { + .then((user: User) => { this.currentUser = user; localStorage.setItem('user', JSON.stringify(user)); safeCb(callback)(null, user); @@ -98,7 +98,7 @@ export class AuthService { logout() { localStorage.removeItem('user'); localStorage.removeItem('id_token'); - this.currentUser = {}; + this.currentUser = new User(); return Promise.resolve(); } @@ -110,12 +110,12 @@ export class AuthService { * @return {Promise} */ createUser(user, callback) { - return this.UserService.create(user).toPromise() + return this.userService.create(user).toPromise() .then(data => { localStorage.setItem('id_token', data.token); - return this.UserService.get().toPromise(); + return this.userService.get().toPromise(); }) - .then(_user => { + .then((_user: User) => { this.currentUser = _user; return safeCb(callback)(null, _user); }) @@ -134,7 +134,7 @@ export class AuthService { * @return {Promise} */ changePassword(oldPassword, newPassword, callback) { - return this.UserService.changePassword({id: this.currentUser._id}, oldPassword, newPassword) + return this.userService.changePassword({id: this.currentUser._id}, oldPassword, newPassword).toPromise() .then(() => safeCb(callback)(null)) .catch(err => safeCb(callback)(err)); } @@ -145,7 +145,7 @@ export class AuthService { * @param {Function} [callback] - function(user) * @return {Promise} */ - getCurrentUser(callback) { + getCurrentUser(callback?) { safeCb(callback)(this.currentUser); return Promise.resolve(this.currentUser); } @@ -161,10 +161,11 @@ export class AuthService { /** * Checks if user is logged in + * @param {function} [callback] * @returns {Promise} */ - isLoggedIn(callback) { - let is = this.currentUser.hasOwnProperty('role'); + isLoggedIn(callback?) { + let is = !!this.currentUser._id; safeCb(callback)(is); return Promise.resolve(is); } @@ -180,10 +181,10 @@ export class AuthService { /** * Check if a user is an admin * - * @param {Function|*} callback - optional, function(is) + * @param {Function|*} [callback] - optional, function(is) * @return {Promise} */ - isAdmin(callback) { + isAdmin(callback?) { return this.getCurrentUser().then(user => { var is = user.role === 'admin'; safeCb(callback)(is); diff --git a/templates/app/client/components/auth(auth)/user.service.js b/templates/app/client/components/auth(auth)/user.service.js index 543df3c5a..d6126e5e3 100644 --- a/templates/app/client/components/auth(auth)/user.service.js +++ b/templates/app/client/components/auth(auth)/user.service.js @@ -10,44 +10,46 @@ import 'rxjs/add/operator/toPromise'; // @flow type UserType = { // TODO: use Mongoose model - name: string; - email: string; + id?: string; + _id?: string; + name?: string; + email?: string; +} + +function handleError(err) { + return Observable.throw(err.json().error || 'Server error'); } @Injectable() export class UserService { static parameters = [AuthHttp]; - constructor(authHttp: AuthHttp) { - this.AuthHttp = authHttp; - } - - handleError(err) { - Observable.throw(err.json().error || 'Server error'); + constructor(<%= private() %>authHttp: AuthHttp) { + this.authHttp = authHttp; } query(): Observable { - return this.AuthHttp.get('/api/users/') + return this.authHttp.get('/api/users/') .map((res:Response) => res.json()) - .catch(this.handleError); + .catch(handleError); } - get(user = {id: 'me'}): Observable { - return this.AuthHttp.get(`/api/users/${user.id || user._id}`) + get(user: UserType = {id: 'me'}): Observable { + return this.authHttp.get(`/api/users/${user.id || user._id}`) .map((res:Response) => res.json()) - .catch(this.handleError); + .catch(handleError); } create(user: UserType) { - return this.AuthHttp.post('/api/users/', user) + return this.authHttp.post('/api/users/', user) .map((res:Response) => res.json()) - .catch(this.handleError); + .catch(handleError); } changePassword(user, oldPassword, newPassword) { - return this.AuthHttp.put(`/api/users/${user.id || user._id}/password`, {oldPassword, newPassword}) + return this.authHttp.put(`/api/users/${user.id || user._id}/password`, {oldPassword, newPassword}) .map((res:Response) => res.json()) - .catch(this.handleError); + .catch(handleError); } remove(user) { - return this.AuthHttp.delete(`/api/users/${user.id || user._id}`) + return this.authHttp.delete(`/api/users/${user.id || user._id}`) .map(() => user) - .catch(this.handleError); + .catch(handleError); } } diff --git a/templates/app/client/components/directives.module.js b/templates/app/client/components/directives.module.js index e67c223b8..9de20f6e6 100644 --- a/templates/app/client/components/directives.module.js +++ b/templates/app/client/components/directives.module.js @@ -1,6 +1,5 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; -import { UIRouterModule } from 'ui-router-ng2'; import { CollapseModule } from 'ng2-bootstrap'; import { AuthModule } from './auth/auth.module'; @@ -13,7 +12,6 @@ import { OauthButtonsComponent } from './oauth-buttons/oauth-buttons.component'; @NgModule({ imports: [ CommonModule, - UIRouterModule.forChild(), CollapseModule, AuthModule, ], diff --git a/templates/app/client/components/navbar/navbar.component.js b/templates/app/client/components/navbar/navbar.component.js index a1e4608fb..c62d966a4 100644 --- a/templates/app/client/components/navbar/navbar.component.js +++ b/templates/app/client/components/navbar/navbar.component.js @@ -1,6 +1,6 @@ import { Component } from '@angular/core'; <%_ if (filters.auth) { -%> - <%_ if (filters.uirouter) { -%> +<%_ if (filters.uirouter) { -%> import { StateService } from 'ui-router-ng2';<% } %> import { AuthService } from '../auth/auth.service';<% } %> @@ -20,34 +20,35 @@ export class NavbarComponent { <%_ if(filters.auth) { -%> static parameters = [AuthService<% if(filters.uirouter) { %>, StateService<% } %>]; - constructor(authService: AuthService<% if(filters.uirouter) { %>, stateService: StateService<% } %>) { - this.AuthService = authService; - this.StateService = stateService; + constructor(<%= private() %>authService: AuthService<% if(filters.uirouter) { %>, <%= private() %>stateService: StateService<% } %>) { + this.authService = authService; + <%_ if (filters.uirouter) { -%> + this.stateService = stateService;<% } %> this.reset(); - this.AuthService.currentUserChanged.subscribe(user => { - this.currentuser = user; + this.authService.currentUserChanged.subscribe(user => { + this.currentUser = user; this.reset(); }) } reset() { - this.AuthService.isLoggedIn().then(is => { + this.authService.isLoggedIn().then(is => { this.isLoggedIn = is; }); - this.AuthService.isAdmin().then(is => { + this.authService.isAdmin().then(is => { this.isAdmin = is; }); - this.AuthService.getCurrentUser().then(user => { + this.authService.getCurrentUser().then(user => { this.currentUser = user; }); } logout() { - let promise = this.AuthService.logout(); + let promise = this.authService.logout(); <%_ if (filters.uirouter) { -%> - this.StateService.go('login');<% } -%> + this.stateService.go('login');<% } -%> <%_ if (filters.ngroute) { -%><% } -%> return promise; }<% } -%> diff --git a/templates/app/client/components/navbar/navbar.html b/templates/app/client/components/navbar/navbar.html index 2b4284e55..2be85d3b8 100644 --- a/templates/app/client/components/navbar/navbar.html +++ b/templates/app/client/components/navbar/navbar.html @@ -12,7 +12,7 @@