| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,11 @@ | |||
| 1 | + // @flow | ||
| 1 | 2 | import { Component } from '@angular/core'; | |
| 2 | 3 | <%_ if(filters.uirouter) { -%> | |
| 3 | 4 | import { StateService } from 'ui-router-ng2';<% } %> | |
| 4 | 5 | <%_ if(filters.ngroute) { -%> | |
| 5 | 6 | import { Router } from '@angular/router';<% } %> | |
| 6 | 7 | import { AuthService } from '../../../components/auth/auth.service'; | |
| 7 | 8 | ||
| 8 | - // @flow | ||
| 9 | 9 | <%_ if(filters.flow) { -%> | |
| 10 | 10 | type User = { | |
| 11 | 11 | name: string; | |
@@ -48,8 +48,8 @@ export class LoginComponent { | |||
| 48 | 48 | this.StateService = _StateService_;<% } %> | |
| 49 | 49 | } | |
| 50 | 50 | ||
| 51 | - login() { | ||
| 52 | - this.submitted = true; | ||
| 51 | + login(form) { | ||
| 52 | + if(form.invalid) return; | ||
| 53 | 53 | ||
| 54 | 54 | return this.AuthService.login({ | |
| 55 | 55 | email: this.user.email, | |
@@ -63,7 +63,7 @@ export class LoginComponent { | |||
| 63 | 63 | this.StateService.go('main');<% } %> | |
| 64 | 64 | }) | |
| 65 | 65 | .catch(err => { | |
| 66 | - this.errors.login = err.message; | ||
| 66 | + this.errors.login = err.json().message; | ||
| 67 | 67 | }); | |
| 68 | 68 | } | |
| 69 | 69 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,7 @@ <h1>Login</h1> | |||
| 6 | 6 | <p>Admin account is <code>admin@example.com</code> / <code>admin</code></p> | |
| 7 | 7 | </div> | |
| 8 | 8 | <div class="col-sm-12"> | |
| 9 | - <form class="form" name="loginForm" (ngSubmit)="login()" novalidate #loginForm="ngForm"> | ||
| 9 | + <form class="form" name="loginForm" (ngSubmit)="login(loginForm)" novalidate #loginForm="ngForm"> | ||
| 10 | 10 | ||
| 11 | 11 | <div class="form-group"> | |
| 12 | 12 | <label>Email</label> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,14 +5,14 @@ import { StateService } from 'ui-router-ng2';<% } %> | |||
| 5 | 5 | <%_ if(filters.ngroute) { -%> | |
| 6 | 6 | import { Router } from '@angular/router';<% } %> | |
| 7 | 7 | import { AuthService } from '../../../components/auth/auth.service'; | |
| 8 | + import template from './signup.html'; | ||
| 8 | 9 | ||
| 9 | 10 | <%_ if(filters.flow) { -%> | |
| 10 | 11 | type User = { | |
| 11 | 12 | name: string; | |
| 12 | 13 | email: string; | |
| 13 | 14 | password: string; | |
| 14 | - };<% } %> | ||
| 15 | - <%_ if(filters.ts) { -%> | ||
| 15 | + };<% } %><%_ if(filters.ts) { -%> | ||
| 16 | 16 | interface User { | |
| 17 | 17 | name: string; | |
| 18 | 18 | email: string; | |
@@ -21,7 +21,7 @@ interface User { | |||
| 21 | 21 | ||
| 22 | 22 | @Component({ | |
| 23 | 23 | selector: 'signup', | |
| 24 | - template: require('./signup.<%=templateExt%>') | ||
| 24 | + template, | ||
| 25 | 25 | }) | |
| 26 | 26 | export class SignupComponent { | |
| 27 | 27 | user: User = { | |
@@ -61,19 +61,27 @@ export class SignupComponent { | |||
| 61 | 61 | <% if(filters.ngroute) { %>this.Router.navigateByUrl('/home');<% } -%> | |
| 62 | 62 | <% if(filters.uirouter) { %>this.StateService.go('main');<% } -%> | |
| 63 | 63 | }) | |
| 64 | - .catch(err => { | ||
| 65 | - err = err.data; | ||
| 66 | - this.errors = {};<% if(filters.mongooseModels) { %> | ||
| 64 | + .catch(err => {<% if(filters.mongooseModels) { %> | ||
| 65 | + this.errors = err.errors; | ||
| 66 | + | ||
| 67 | 67 | // Update validity of form fields that match the mongoose errors | |
| 68 | - err.errors.forEach((error, field) => { | ||
| 68 | + Object.entries(err.errors).forEach(([field, error]) => { | ||
| 69 | 69 | this.errors[field] = error.message; | |
| 70 | + | ||
| 71 | + if(field === 'email' && error.kind === 'user defined') { | ||
| 72 | + form.form.controls[field].setErrors({inUse: true}); | ||
| 73 | + } | ||
| 70 | 74 | });<% } %><% if(filters.sequelizeModels) { %> | |
| 75 | + this.errors = {}; | ||
| 76 | + | ||
| 71 | 77 | // Update validity of form fields that match the sequelize errors | |
| 72 | 78 | if(err.name) { | |
| 73 | 79 | err.fields.forEach(field => { | |
| 74 | 80 | this.errors[field] = err.message; | |
| 75 | 81 | }); | |
| 76 | 82 | }<% } %> | |
| 83 | + | ||
| 84 | + this.submitted = false; | ||
| 77 | 85 | }); | |
| 78 | 86 | } | |
| 79 | 87 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | <h1>Sign up</h1> | |
| 5 | 5 | </div> | |
| 6 | 6 | <div class="col-sm-12"> | |
| 7 | - <form class="form" name="form" (ngSubmit)="register(form)" novalidate #signupForm="ngForm"> | ||
| 7 | + <form class="form" name="form" (ngSubmit)="register(signupForm)" novalidate #signupForm="ngForm"> | ||
| 8 | 8 | ||
| 9 | 9 | <div class="form-group" | |
| 10 | 10 | [class.has-success]="name.valid && submitted" | |
@@ -26,11 +26,11 @@ <h1>Sign up</h1> | |||
| 26 | 26 | required | |
| 27 | 27 | mongoose-error | |
| 28 | 28 | #email="ngModel"> | |
| 29 | - <p class="help-block" [hidden]="email.valid || (email.pristine && !signupForm.submitted)"> | ||
| 29 | + <p class="help-block" [hidden]="email.valid || (email.pristine && !signupForm.submitted) || (email.errors ? email.errors.inUse : true)"> | ||
| 30 | 30 | Please enter a valid email address. | |
| 31 | 31 | </p> | |
| 32 | - <p class="help-block" [hidden]="!errors.email"> | ||
| 33 | - {{ errors.email }} | ||
| 32 | + <p class="help-block" [hidden]="email.errors ? !email.errors.inUse : true"> | ||
| 33 | + This email address is already in use. | ||
| 34 | 34 | </p> | |
| 35 | 35 | </div> | |
| 36 | 36 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ import { BrowserModule } from '@angular/platform-browser';<% if(filters.uirouter | |||
| 3 | 3 | import { UIRouterModule } from 'ui-router-ng2';<% } %><% if(filters.ngroute) { %> | |
| 4 | 4 | import { RouterModule, Routes } from '@angular/router';<% } %> | |
| 5 | 5 | import { AuthGuard } from '../../components/auth/auth-guard.service'; | |
| 6 | + import { AuthModule } from '../../components/auth/auth.module'; | ||
| 6 | 7 | import { AdminComponent } from './admin.component'; | |
| 7 | 8 | ||
| 8 | 9 | <%_ if(filters.uirouter) { -%> | |
@@ -16,6 +17,7 @@ const adminRoutes: Routes = [{ | |||
| 16 | 17 | ||
| 17 | 18 | @NgModule({ | |
| 18 | 19 | imports: [ | |
| 20 | + AuthModule, | ||
| 19 | 21 | BrowserModule, | |
| 20 | 22 | <%_ if(filters.ngroute) { _%> | |
| 21 | 23 | RouterModule.forChild(adminRoutes),<% } %> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,11 +3,13 @@ | |||
| 3 | 3 | import { NgModule } from '@angular/core'; | |
| 4 | 4 | import { AuthService } from './auth.service'; | |
| 5 | 5 | import { UserService } from './user.service'; | |
| 6 | + import { AuthGuard } from '../../components/auth/auth-guard.service'; | ||
| 6 | 7 | ||
| 7 | 8 | @NgModule({ | |
| 8 | 9 | providers: [ | |
| 9 | 10 | AuthService, | |
| 10 | - UserService | ||
| 11 | + UserService, | ||
| 12 | + AuthGuard, | ||
| 11 | 13 | ] | |
| 12 | 14 | }) | |
| 13 | 15 | export class AuthModule {} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -122,7 +122,8 @@ export class AuthService { | |||
| 122 | 122 | }) | |
| 123 | 123 | .catch(err => { | |
| 124 | 124 | this.logout(); | |
| 125 | - return safeCb(callback)(err); | ||
| 125 | + safeCb(callback)(err); | ||
| 126 | + return Promise.reject(err); | ||
| 126 | 127 | }); | |
| 127 | 128 | } | |
| 128 | 129 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - 'use strict'; | ||
| 1 | + // @flow | ||
| 2 | 2 | import { Injectable } from '@angular/core'; | |
| 3 | 3 | import { Response } from '@angular/http'; | |
| 4 | 4 | import { AuthHttp } from 'angular2-jwt'; | |
@@ -7,7 +7,6 @@ import 'rxjs/add/operator/map'; | |||
| 7 | 7 | import 'rxjs/add/operator/catch'; | |
| 8 | 8 | import 'rxjs/add/operator/toPromise'; | |
| 9 | 9 | ||
| 10 | - // @flow | ||
| 11 | 10 | type UserType = { | |
| 12 | 11 | // TODO: use Mongoose model | |
| 13 | 12 | id?: string; | |
@@ -17,7 +16,7 @@ type UserType = { | |||
| 17 | 16 | } | |
| 18 | 17 | ||
| 19 | 18 | function handleError(err) { | |
| 20 | - return Observable.throw(err.json().error || 'Server error'); | ||
| 19 | + return Observable.throw(err.json() || 'Server error'); | ||
| 21 | 20 | } | |
| 22 | 21 | ||
| 23 | 22 | @Injectable() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,10 +5,11 @@ import { StateService } from 'ui-router-ng2';<% } %> | |||
| 5 | 5 | <%_ if (filters.ngroute) { -%> | |
| 6 | 6 | import { Router } from '@angular/router';<% } %> | |
| 7 | 7 | import { AuthService } from '../auth/auth.service';<% } %> | |
| 8 | + import template from './navbar.html'; | ||
| 8 | 9 | ||
| 9 | 10 | @Component({ | |
| 10 | 11 | selector: 'navbar', | |
| 11 | - template: require('./navbar.<%=templateExt%>') | ||
| 12 | + template, | ||
| 12 | 13 | }) | |
| 13 | 14 | export class NavbarComponent { | |
| 14 | 15 | isCollapsed = true; | |
@@ -53,9 +54,10 @@ export class NavbarComponent { | |||
| 53 | 54 | } | |
| 54 | 55 | ||
| 55 | 56 | logout() { | |
| 56 | - let promise = this.AuthService.logout();<% if(filters.uirouter) { %> | ||
| 57 | - this.StateService.go('login');<% } %><% if(filters.ngroute) { %> | ||
| 58 | - this.Router.navigateByUrl('/home');<% } %> | ||
| 59 | - return promise; | ||
| 57 | + return this.AuthService.logout().then(() => {<% if(filters.uirouter) { %> | ||
| 58 | + this.StateService.go('login');<% } %><% if(filters.ngroute) { %> | ||
| 59 | + this.Router.navigateByUrl('/home');<% } %> | ||
| 60 | + this.reset(); | ||
| 61 | + }); | ||
| 60 | 62 | }<% } -%> | |
| 61 | 63 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,7 @@ | |||
| 26 | 26 | <li [hidden]="isLoggedIn" <% if(filters.uirouter) { %>uiSrefActive="active"<% } else { %>routerLinkActive="active"<% } %>><a <% if(filters.uirouter) { %>uiSref="login"<% } else { %>routerLink="/login"<% } %>>Login</a></li> | |
| 27 | 27 | <li [hidden]="!isLoggedIn"><p class="navbar-text">Hello {{ currentUser.name }}</p> </li> | |
| 28 | 28 | <li [hidden]="!isLoggedIn" <% if(filters.uirouter) { %>uiSrefActive="active"<% } else { %>routerLinkActive="active"<% } %>><a <% if(filters.uirouter) { %>uiSref="settings"<% } else { %>routerLink="/settings"<% } %>><span class="glyphicon glyphicon-cog"></span></a></li> | |
| 29 | - <li [hidden]="!isLoggedIn"><a (click)="logout()">Logout</a></li> | ||
| 29 | + <li [hidden]="!isLoggedIn"><a (click)="logout()" style="cursor: pointer;">Log out</a></li> | ||
| 30 | 30 | </ul><% } %> | |
| 31 | 31 | </div> | |
| 32 | 32 | </div> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments