This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
register.component.ts
96 lines (88 loc) · 3.13 KB
/
register.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Component, Input, OnInit, TemplateRef } from '@angular/core';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Account } from '../shared/account';
import { Stormpath, LoginFormModel, RegistrationFormModel } from '../stormpath/stormpath.service';
@Component({
selector: 'register-form',
template: `<ng-template #defaultTemplate>
<form *ngIf="!registered" (ngSubmit)="onSubmit()" class="form-horizontal" autocomplete="off">
<div class="form-group" *ngFor="let field of model?.form?.fields">
<label [attr.for]="field.name" class="col-sm-4 control-label">{{field.label}}</label>
<div class="col-sm-8">
<input class="form-control" [name]="field.name" [id]="field.name" [type]="field.type"
[(ngModel)]="formModel[field.name]" [placeholder]="field.placeholder" [disabled]="creating" [required]="field.required">
</div>
</div>
<div *ngIf="error" class="alert alert-danger">{{error}}</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
<p *ngIf="unverified" class="alert alert-success">
Your account has been created and requires verification.
Please check your email for a verification link.
</p>
<p class="alert alert-success" *ngIf="canLogin">
Your account has been created, you may now log in.
</p>
</ng-template>
<ng-template
[ngTemplateOutlet]="customTemplate || defaultTemplate">
</ng-template>`
})
@Injectable()
export class RegisterComponent implements OnInit {
/**
* A reference to a `<ng-template>` tag that if set will override this component's template. Use like so:
* ```
* <ng-template #customTemplate>
* // custom HTML with login form
* </ng-template>
* ```
* Then pass customTemplate to the `register-form` component like so `[customTemplate]="customTemplate"`
*/
@Input() customTemplate: TemplateRef<any>;
@Input() autoLogin: boolean;
protected model: Object;
protected error: string;
protected viewModel$: Observable<Object>;
protected formModel: RegistrationFormModel;
protected unverified: boolean;
protected canLogin: boolean;
protected registered: boolean;
constructor(public stormpath: Stormpath) {
this.unverified = false;
this.canLogin = false;
this.formModel = {
email: '',
surname: '',
givenName: '',
password: ''
};
}
ngOnInit(): void {
this.stormpath.getRegistrationViewModel()
.subscribe(model => {
this.model = model;
}, error =>
this.error = error.message
);
}
register(): void {
this.stormpath.register(this.formModel)
.subscribe((account: Account) => {
this.registered = true;
this.unverified = account.status === 'UNVERIFIED';
this.canLogin = account.status === 'ENABLED';
if (this.canLogin && this.autoLogin) {
let loginAttempt: LoginFormModel = {
login: this.formModel.email,
password: this.formModel.password
};
this.stormpath.login(loginAttempt);
}
}, error => this.error = error.message);
}
onSubmit(): void {
this.register();
}
}