-
Notifications
You must be signed in to change notification settings - Fork 2
/
userAuth.js
142 lines (113 loc) · 4.02 KB
/
userAuth.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
//firebase API config/key
var mainApp = {};
let organization;
var address;
let userAccess = "law";
firebaseConfig = {
apiKey: "AIzaSyDQm4jUuneS38vvPSjwhCP7JnT4IxQUtT8",
databaseURL: "https://carnet-e1efe.firebaseio.com",
projectId: "carnet-e1efe",
storageBucket: "carnet-e1efe.appspot.com",
messagingSenderId: "1002956662880",
appId: "1:1002956662880:web:5f2597bf17eaa7319e3b05",
measurementId: "G-FSHPMG9M89"
};
//starts firebase
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
db = firebase.firestore();
//stores signIn and signUp buttons in respective vars to listen for click
var signIn = document.getElementById('signIn');
var signUp = document.getElementById('signUp');
var logoutB = document.getElementById('logout');
var signUp2 = document.getElementById('signUp2');
//When user clicks signUP button
function createUser(email,password) {
const auth = firebase.auth();
const promise = auth.createUserWithEmailAndPassword(email, password);
promise.catch(e => console.log(e.message));
}
signUp.addEventListener('click', e => {
var checkBox = document.getElementById('checkBox');
if(checkBox.checked===true) {
var nonLawEnforcement = document.getElementById('nonLawEnforcement');
const email = document.getElementById('user').value;
const password = document.getElementById('password').value;
createUser(email,password);
// Add a new document in collection "users"
db.collection("users").doc(email).set({
userAuth: "law"
})
.then(function() {
console.log("User successfully entered!");
})
.catch(function(error) {
console.error("Error writing user: ", error);
});
/*var fs = require('fs');
let rawdata = fs.readFileSync('marketData.json');
let data = JSON.parse(rawdata);
console.log(data);
marketData = data;
var loc = [-73.822990,42.846260];
var tempMarket = {
company: "organization",
location: loc
};
marketData.push(tempMarket);
var json = JSON.stringify(marketData);
fs.writeFile('marketData.json', json, function(err, result) {
if(err) console.log('error', err);
}); */
} else {
var nonLawEnforcement = document.getElementById('nonLawEnforcement');
const email = document.getElementById('user').value;
const password = document.getElementById('password').value;
organization = document.getElementById('organization').value;
createUser(email,password);
// Add a new document in collection "users"
db.collection("users").doc(email).set({
userAuth: "normal"
})
.then(function() {
console.log("User successfully entered!");
})
.catch(function(error) {
console.error("Error writing user: ", error);
});
db.collection("markers").doc(email).set({
company: organization,
location: address
})
.then(function() {
console.log("Marker successfully entered!");
})
.catch(function(error) {
console.error("Error writing marker: ", error);
});
}
});
//When user clicks signIn button
signIn.addEventListener('click', e => {
const email = document.getElementById('user').value;
const password = document.getElementById('password').value;
const auth = firebase.auth();
const promise = auth.signInWithEmailAndPassword(email,password);
promise.catch(e => console.log(e.message));
});
(function () {
logOut();
var uid = null;
firebase.auth().onAuthStateChanged(firebaseUser => {
if(firebaseUser) {
window.location.href='mainWindow.html';
} else {
console.log('not logged in');
}
});
function logOut(){
firebase.auth().signOut();
}
mainApp.logOut = logOut;
})();