-
Notifications
You must be signed in to change notification settings - Fork 89
/
authentication.ts
116 lines (102 loc) · 2.61 KB
/
authentication.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Injectable } from '@angular/core';
import { AngularFire, AuthProviders, AngularFireDatabase } from 'angularfire2';
import { User } from '../models/user';
import { Observable } from 'rxjs/Observable';
import { environment } from '../../environments/environment';
import { ApiUrl } from '../app.api-url';
import { HttpService } from './http';
@Injectable()
export class AuthenticationService {
userAuth: Observable<any>;
constructor(
public af: AngularFire,
public db: AngularFireDatabase,
public http: HttpService,
) {
this.userAuth = this.af.auth.map(
user => this._changeState(user),
error => console.trace(error),
);
};
login(from: string) {
this.af.auth.login({
provider: this._getProvider(from)
});
return this.userAuth
}
logout() {
this.af.auth.logout();
localStorage.removeItem('access_token')
return this.userAuth;
}
authStatus() {
return this.userAuth;
}
findbyIds(ids: string[]) {
return this.http.get('users')
.map(res => res.json().data)
.map(users => users.filter(user => ids.indexOf(user.id) > -1))
.map(users => users.map(user => new User(user)))
}
findbyUID(id: string){
return this.http.get(`users/${id}`)
.map(res => res.json().data)
}
getAccessToken(userAuth) {
return this.http.post("users/auth", userAuth)
.map(res => res.json())
.map(data => {
let token = data.token;
localStorage.setItem('access_token', token);
return Object.assign(userAuth, {
access_token: token,
user: data.user
})
})
}
updateUserAuth(userAuth) {
let user = userAuth.user;
return this.findbyUID(user.id).map(
user => {
return Object.assign({}, userAuth, {
user: new User(user)
});
}
)
}
private _changeState(user: any = null) {
if(user) {
return {
user: this._getUserInfo(user),
isAuthenticated: true
}
}
else {
return {
user: null,
isAuthenticated: false
}
}
}
private _getUserInfo(user: any): any {
if(!user) {
return {};
}
let data = user.auth.providerData[0];
return {
name: data.displayName,
avatar: data.photoURL,
email: data.email,
provider: data.providerId,
uid: user.auth.uid
};
}
private _getProvider(from: string) {
switch(from){
case 'twitter': return AuthProviders.Twitter;
case 'facebook': return AuthProviders.Facebook;
case 'github': return AuthProviders.Github;
case 'google': return AuthProviders.Google;
}
}
}