-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathgithub-popup.ts
97 lines (90 loc) · 2.98 KB
/
github-popup.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
import { initializeApp } from 'firebase/app';
import {
GithubAuthProvider,
connectAuthEmulator,
getAuth,
onAuthStateChanged,
signInWithPopup,
signOut,
} from 'firebase/auth';
import { firebaseConfig } from './config';
initializeApp(firebaseConfig);
const auth = getAuth();
if (window.location.hostname === 'localhost') {
connectAuthEmulator(auth, 'http://127.0.0.1:9099');
}
const oauthToken = document.getElementById(
'quickstart-oauthtoken',
)! as HTMLDivElement;
const signInButton = document.getElementById(
'quickstart-sign-in',
)! as HTMLButtonElement;
const signInStatus = document.getElementById(
'quickstart-sign-in-status',
)! as HTMLSpanElement;
const accountDetails = document.getElementById(
'quickstart-account-details',
)! as HTMLDivElement;
/**
* Function called when clicking the Login/Logout button.
*/
function toggleSignIn() {
if (!auth.currentUser) {
const provider = new GithubAuthProvider();
provider.addScope('repo');
signInWithPopup(auth, provider)
.then(function (result) {
const credential = GithubAuthProvider.credentialFromResult(result);
// This gives you a GitHub Access Token. You can use it to access the GitHub API.
const token = credential?.accessToken;
// The signed-in user info.
const user = result.user;
oauthToken.textContent = token ?? '';
})
.catch(function (error) {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The firebase.auth.AuthCredential type that was used.
const credential = error.credential;
if (errorCode === 'auth/account-exists-with-different-credential') {
alert(
'You have already signed up with a different auth provider for that email.',
);
// If you are using multiple auth providers on your app you should handle linking
// the user's accounts here.
} else {
console.error(error);
}
});
} else {
signOut(auth);
}
signInButton.disabled = true;
}
// Listening for auth state changes.
onAuthStateChanged(auth, function (user) {
if (user) {
// User is signed in.
const displayName = user.displayName;
const email = user.email;
const emailVerified = user.emailVerified;
const photoURL = user.photoURL;
const isAnonymous = user.isAnonymous;
const uid = user.uid;
const providerData = user.providerData;
signInStatus.textContent = 'Signed in';
signInButton.textContent = 'Sign out';
accountDetails.textContent = JSON.stringify(user, null, ' ');
} else {
// User is signed out.
signInStatus.textContent = 'Signed out';
signInButton.textContent = 'Sign in with GitHub';
accountDetails.textContent = 'null';
oauthToken.textContent = 'null';
}
signInButton.disabled = false;
});
signInButton.addEventListener('click', toggleSignIn, false);