-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (105 loc) · 4.36 KB
/
script.js
File metadata and controls
116 lines (105 loc) · 4.36 KB
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
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyADpSoSf_rcv-LNUZw23czp9LeY4f6V6xg",
authDomain: "auth.attendance.masonhackclub.com",
databaseURL: "https://attendance-902c5.firebaseio.com",
projectId: "attendance-902c5",
storageBucket: "attendance-902c5.appspot.com",
messagingSenderId: "552343475963",
appId: "1:552343475963:web:6d6563fc36b32f8e"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var db = firebase.firestore();
//get the secret key that verifies that it came from a valid source
const urlParams = new URLSearchParams(window.location.search);
const skey = urlParams.get('skey');
const finalLink = urlParams.get('fl');
//once redirected back to mason
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
if(user.email.split('@')[1] === 'masonohioschools.com') {
// User is signed in.
document.getElementById("google-button").style.display = "none";
if (skey != null) {
// secret key is defined
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var isAnonymous = user.isAnonymous;
var uid = user.uid;
var providerData = user.providerData;
saveAttendanceData(user, skey);
//user should get redirected by this function. If it fails, it continues into the next bit
}
var alert = document.createElement('h1');
alert.innerText = "Please scan the QR Code with your phone";
document.getElementById('sign-in-buttons').prepend(alert);
} else {
// User is signed in with a non-mason account
user.delete().then(function () {
firebase.auth().signOut()
}).catch(function (error) {
console.log(error);
});
var alert = document.createElement('h1');
alert.innerText = "Please Sign In with your Mason Google Account";
document.getElementById('sign-in-buttons').prepend(alert);
}
} else {
//not signed in yet
}
});
var saveAttendanceData = function(user, secretKey) {
//update/save a copy of the user to the Userdata bucket
//should be able to look at the UserData collection for roster moves
//might be worth refactoring into pulling their data, analyzing it and merging manually
db.collection('Userdata').doc(user.email).set({
name : user.displayName,
email: user.email,
phoneNumer: user.phoneNumber,
uid: user.uid,
photo: user.photoURL,
providerData : user.providerData,
updated: firebase.firestore.FieldValue.serverTimestamp(),
},{merge:true}).then(function(){
console.log('userdata successed');
}).catch(function(error){
console.log(error);
});
//use the skey to create a collection based on a single meeting
db.collection('SignIns').doc(secretKey).collection('members').doc(user.email).set({
[user.email]: firebase.firestore.FieldValue.serverTimestamp(),
},{merge:true}).then(function(){
//redirect via JS to signed in page
window.location.href="signed-in/?fl="+finalLink;
}).catch(function(error){
console.log(error);
});
};
//user stays signed in for as log as they can
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.catch(function(error) {
// Handle Errors here.
console.log(error);
});
//sign in function
var initSignIn = function() {
firebase.auth().signInWithRedirect(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// I haven't done anything here. But you could send them emails or something else
}).catch(function(error) {
// Handle Errors here.
console.log(error);
});
};
//Now that everything is defined, make auth provider
var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
//Encourages people to use Mason account
provider.setCustomParameters({'hd':'masonohioschools.com'});