|
1 |
| -import { Component, OnInit } from '@angular/core'; |
| 1 | +import { Component, OnInit, Input } from '@angular/core'; |
| 2 | +import { TargetState, StateService } from 'ui-router-core'; |
| 3 | +import { AuthService } from './global/auth.service'; |
| 4 | +import { AppConfigService } from './global/app-config.service'; |
2 | 5 |
|
| 6 | +/** |
| 7 | + * This component renders a faux authentication UI |
| 8 | + * |
| 9 | + * It prompts for the username/password (and gives hints with bouncy arrows) |
| 10 | + * It shows errors if the authentication failed for any reason. |
| 11 | + */ |
3 | 12 | @Component({
|
4 | 13 | selector: 'app-login',
|
5 | 14 | template: `
|
6 |
| - <p> |
7 |
| - login Works! |
8 |
| - </p> |
9 |
| - `, |
| 15 | + <div class="container"> |
| 16 | + <div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2"> |
| 17 | + <h3>Log In</h3> |
| 18 | + <p> |
| 19 | + (This login screen is for demonstration only... |
| 20 | + just pick a username, enter 'password' and click <b>"Log in"</b>)</p> |
| 21 | + <hr> |
| 22 | + |
| 23 | + <div> |
| 24 | + <label for="username">Username:</label> |
| 25 | + <select class="form-control" name="username" id="username" |
| 26 | + [(ngModel)]="credentials.username" ng-options="username for username in $ctrl.usernames"> |
| 27 | + <option *ngFor="let username of usernames" [value]="username">{{username}}</option> |
| 28 | + </select> |
| 29 | + |
| 30 | + <i style="position: relative; bottom: 1.8em; margin-left: 10em; height: 0" |
| 31 | + [hidden]="credentials.username" class="fa fa-arrow-left bounce-horizontal"> Choose </i> |
| 32 | + </div> |
| 33 | + <br> |
| 34 | + |
| 35 | + <div> |
| 36 | + <label for="password">Password:</label> |
| 37 | + <input class="form-control" type="password" name="password" [(ngModel)]="credentials.password"> |
| 38 | + <i style="position: relative; bottom: 1.8em; margin-left: 5em; height: 0" |
| 39 | + [hidden]="!credentials.username || credentials.password == 'password'" class="fa fa-arrow-left bounce-horizontal"> |
| 40 | + Enter '<b>password</b>' here |
| 41 | + </i> |
| 42 | + </div> |
| 43 | + |
| 44 | + <div [hidden]="!errorMessage" class="well error">{{ errorMessage }}</div> |
| 45 | + |
| 46 | + <hr> |
| 47 | + <div> |
| 48 | + <button class="btn btn-primary" type="button" [disabled]="authenticating" (click)="login(credentials)"> |
| 49 | + <i class="fa fa-spin fa-spinner" [hidden]="!authenticating"></i> <span>Log in</span> |
| 50 | + </button> |
| 51 | + <i [hidden]="!credentials.username && credentials.password == 'password'" |
| 52 | + style="position: relative;" class="fa fa-arrow-left bounce-horizontal"> Click Me!</i> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | +`, |
10 | 57 | styles: []
|
11 | 58 | })
|
12 |
| -export class LoginComponent implements OnInit { |
| 59 | +export class LoginComponent { |
| 60 | + @Input() returnTo: TargetState; |
13 | 61 |
|
14 |
| - constructor() { } |
| 62 | + usernames: string[]; |
| 63 | + credentials = { username: null, password: null }; |
| 64 | + authenticating: boolean; |
| 65 | + errorMessage: string; |
15 | 66 |
|
16 |
| - ngOnInit() { |
| 67 | + constructor(appConfig: AppConfigService, |
| 68 | + private authService: AuthService, |
| 69 | + private $state: StateService |
| 70 | + ) { |
| 71 | + this.usernames = authService.usernames; |
| 72 | + |
| 73 | + this.credentials = { |
| 74 | + username: appConfig.emailAddress, |
| 75 | + password: 'password' |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * The controller for the `login` component |
| 81 | + * |
| 82 | + * The `login` method validates the credentials. |
| 83 | + * Then it sends the user back to the `returnTo` state, which is provided as a resolve data. |
| 84 | + */ |
| 85 | + login(credentials) { |
| 86 | + this.authenticating = true; |
| 87 | + |
| 88 | + const returnToOriginalState = () => { |
| 89 | + const state = this.returnTo.state(); |
| 90 | + const params = this.returnTo.params(); |
| 91 | + const options = Object.assign({}, this.returnTo.options(), { reload: true }); |
| 92 | + this.$state.go(state, params, options); |
| 93 | + }; |
| 94 | + |
| 95 | + const showError = (errorMessage) => |
| 96 | + this.errorMessage = errorMessage; |
| 97 | + |
| 98 | + const stop = () => this.authenticating = false; |
| 99 | + this.authService.authenticate(credentials.username, credentials.password) |
| 100 | + .then(returnToOriginalState) |
| 101 | + .catch(showError) |
| 102 | + .then(stop, stop); |
17 | 103 | }
|
18 | 104 |
|
19 | 105 | }
|
0 commit comments