-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
174 lines (148 loc) · 4.61 KB
/
index.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
let user = localStorage.getItem('user');
if (user) {
user = JSON.parse(user);
signIn(user);
} else {
screen('signin');
}
if (navigator.credentials && navigator.credentials.preventSilentAccess) {
(async () => {
if (user) return;
const credentials = await navigator.credentials.get({
password: true,
federated: {
providers: [
'https://accounts.google.com',
'https://facebook.com'
]
}
});
if (credentials) {
if (credentials.type === 'password') {
signIn(credentials);
} else if (credentials.type === 'federated'
&& credentials.provider === 'https://accounts.google.com') {
signInWithGoogle(credentials);
} else if (credentials.type === 'federated'
&& credentials.provider === 'https://facebook.com') {
signInWithFacebook(credentials);
}
}
})();
}
$('form').addEventListener('submit', (e) => {
e.preventDefault();
const form = e.target;
const name = form.name.value;
const email = form.email.value;
const password = form.password.value;
const credentials = new PasswordCredential({
id: email,
name: name,
password: password
});
signIn(credentials);
form.name.value = '';
form.email.value = '';
form.password.value = '';
});
$('.profile a').addEventListener('click', (e) => {
e.preventDefault();
signOut();
});
function handleGoogleSignIn(user) {
const profile = user.getBasicProfile();
const credentials = new FederatedCredential({
id: profile.getEmail(),
name: profile.getName(),
iconURL: profile.getImageUrl(),
provider: 'https://accounts.google.com'
});
signIn(credentials);
}
function handleFacebookSignIn() {
FB.api('/me?fields=name,email,picture', (profile) => {
const credentials = new FederatedCredential({
id: profile.email,
name: profile.name,
iconURL: profile.picture.data.url,
provider: 'https://facebook.com'
});
signIn(credentials);
});
}
async function signIn(credentials) {
$('.profile img').src = credentials.iconURL || 'avatar.png';
$('.profile h3').textContent = credentials.name || '';
$('.profile p').textContent = credentials.id || '';
user = only(credentials, ['id', 'name', 'password', 'iconURL', 'provider']);
localStorage.setItem('user', JSON.stringify(user));
screen('profile');
if (credentials instanceof Credential) {
await navigator.credentials.store(credentials);
}
}
function signInWithGoogle(credentials) {
gapi.load('client:auth2', async () => {
const auth = gapi.auth2.getAuthInstance();
if (auth.isSignedIn.get()) {
const user = auth.currentUser.get();
if (user.getBasicProfile().getEmail() === credentials.id) {
return handleGoogleSignIn(user);
}
}
const user = await auth.signIn({ login_hint: credentials.id });
handleGoogleSignIn(user);
});
}
async function signInWithFacebook(credentials) {
const onFacebookSdkLoad = () => {
FB.getLoginStatus((response) => {
if (response.status === 'connected') {
return handleFacebookSignIn();
}
FB.login(handleFacebookSignIn);
});
};
if (window.fbAsyncInit.hasRun) {
onFacebookSdkLoad();
} else {
const originalFbAsyncInit = window.fbAsyncInit;
window.fbAsyncInit = () => {
originalFbAsyncInit();
onFacebookSdkLoad();
};
}
}
async function signOut() {
switch (user.provider) {
case 'https://accounts.google.com':
await signOutWithGoogle();
break;
case 'https://facebook.com':
await signOutWithFacebook();
break;
}
localStorage.removeItem('user');
screen('signin');
await navigator.credentials.preventSilentAccess();
}
async function signOutWithGoogle() {
const auth = gapi.auth2.getAuthInstance();
await auth.signOut();
}
function signOutWithFacebook() {
FB.logout();
}
function $(selector, context = document) {
return context.querySelector(selector);
}
function only(object, keys) {
return keys.reduce((filtered, key) => (
Object.assign(filtered, { [key]: object[key] })
), {});
}
function screen(name) {
$(`section[id="${name}"]`).classList.remove('hidden');
$(`section:not([id="${name}"])`).classList.add('hidden');
}