UNPKG

3.65 kBPlain TextView Raw
1import {Component, NgZone} from '@angular/core';
2import {NgIf, NgFor} from '@angular/common';
3import {Accounts} from 'meteor/accounts-base';
4import {Tracker} from 'meteor/tracker';
5import {Meteor} from 'meteor/meteor';
6
7export interface LoginCredentials {
8 email : string;
9 password : string;
10}
11
12const LOGIN_TEMPLATE = `{LOGIN_TEMPLATE}`;
13
14@Component({
15 selector: 'login-buttons',
16 moduleId: Meteor.absoluteUrl(module.id),
17 template: LOGIN_TEMPLATE,
18 directives: [NgIf, NgFor]
19})
20export class LoginButtons {
21 autorunComputation:Tracker.Computation;
22 currentUser:Object;
23 currentUserId:string;
24 isLoggingIn:boolean;
25 isLoggedIn:boolean;
26 services:Array<any>;
27 credentials:LoginCredentials;
28 errors:Array<string>;
29 isPasswordRecovery:boolean;
30 isSignup:boolean;
31 isDropdownOpen:boolean;
32 message:string;
33
34 constructor(private zone:NgZone) {
35 this._initAutorun();
36 this.services = this._getLoginServices();
37 this.resetErrors();
38 this.isPasswordRecovery = false;
39 this.isSignup = false;
40 this.isDropdownOpen = false;
41 this._resetCredentialsFields();
42 }
43
44 _resetCredentialsFields() {
45 this.credentials = {email: '', password: ''};
46 }
47
48 resetErrors() {
49 this.errors = [];
50 this.message = "";
51 }
52
53 singleService():Object {
54 let services = this._getLoginServices();
55
56 return services[0];
57 }
58
59 displayName():string {
60 let user = this.currentUser;
61
62 if (!user)
63 return '';
64
65 if (user.profile && user.profile.name)
66 return user.profile.name;
67
68 if (user.username)
69 return user.username;
70
71 if (user.emails && user.emails[0] && user.emails[0].address)
72 return user.emails[0].address;
73
74 return '';
75 };
76
77 login():void {
78 this.resetErrors();
79
80 let email:string = this.credentials.email;
81 let password:string = this.credentials.password;
82
83 Meteor.loginWithPassword(email, password, (error) => {
84 if (error) {
85 this.errors.push(error.reason || "Unknown error");
86 }
87 else {
88 this.isDropdownOpen = false;
89 this._resetCredentialsFields();
90 }
91 });
92 }
93
94 recover() {
95 this.resetErrors();
96
97 Accounts.forgotPassword({email: this.credentials.email}, (error) => {
98 if (error) {
99 this.errors.push(error.reason || "Unknown error");
100 }
101 else {
102 this.message = "You will receive further instruction to you email address!";
103 this.isDropdownOpen = false;
104 this._resetCredentialsFields();
105 }
106 });
107 }
108
109 logout():void {
110 Meteor.logout();
111 this.isDropdownOpen = false;
112 }
113
114 signup():void {
115 this.resetErrors();
116
117 Accounts.createUser(this.credentials, (error) => {
118 if (error) {
119 this.errors.push(error.reason || "Unknown error");
120 }
121 else {
122 this.isDropdownOpen = false;
123 this._resetCredentialsFields();
124 }
125 });
126 }
127
128 _hasPasswordService():boolean {
129 return !!Package['accounts-password'];
130 }
131
132 _getLoginServices():Array<any> {
133 let services = Package['accounts-oauth'] ? Accounts.oauth.serviceNames() : [];
134 services.sort();
135
136 if (this._hasPasswordService())
137 services.push('password');
138
139 return _.map(services, function (name) {
140 return {name: name};
141 });
142 }
143
144 dropdown():boolean {
145 return this._hasPasswordService() || this._getLoginServices().length > 1;
146 }
147
148 _initAutorun():void {
149 this.autorunComputation = Tracker.autorun(() => {
150 this.zone.run(() => {
151 this.currentUser = Meteor.user();
152 this.currentUserId = Meteor.userId();
153 this.isLoggingIn = Meteor.loggingIn();
154 this.isLoggedIn = !!Meteor.user();
155 })
156 });
157 }
158}