-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
71 lines (57 loc) · 2.15 KB
/
auth.js
File metadata and controls
71 lines (57 loc) · 2.15 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
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.13.2/firebase-app.js';
import { getFirestore, collection, getDoc, doc, updateDoc} from 'https://www.gstatic.com/firebasejs/10.13.2/firebase-firestore.js';
import { getAuth, signInWithPopup, GoogleAuthProvider, onAuthStateChanged } from 'https://www.gstatic.com/firebasejs/10.13.2/firebase-auth.js';
//For people reviewing this. IT IS MEANT TO BE EXPOSED. The usage is restricted to a specific domain.
const firebaseConfig = {
apiKey: "AIzaSyC_sefxEBuN0jbzGj17ZWmcgI_4gAcrmfA",
authDomain: "schedule-management-85d32.firebaseapp.com",
projectId: "schedule-management-85d32",
storageBucket: "schedule-management-85d32.appspot.com",
messagingSenderId: "412719286176",
appId: "1:412719286176:web:78ad83815794e871bf06c5"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth();
const provider = new GoogleAuthProvider();
const firestore = getFirestore(app);
const usersCollection = collection(firestore, "/users");
export async function callAuth () {
try {
provider.setCustomParameters({
prompt: 'select_account',
hd: 'jstyle-yumekana.com',
});
const result = await signInWithPopup(auth, provider);
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
const user = result.user;
const userid = user.uid;
const email = user.email;
// Storing user data
const userDocID = "gWagi4j6Q3ntPZVTPu89";
const usersDocRef = doc(usersCollection, userDocID);
const docSnapshot = await getDoc(usersDocRef);
let resultToSend;
if (docSnapshot.exists) {
const data = docSnapshot.data();
const userProfile = {
email,
name: user.displayName,
auth_level: 'regular',
};
if (!data.userid) {
await updateDoc(usersDocRef, {
[userid]: userProfile,
});
}
resultToSend = result;
return true;
} else {
console.error("User document not found");
throw new Error("User document not found");
}
} catch (error) {
console.error("Error:", error);
throw error;
};
}