-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.js
73 lines (64 loc) · 2.28 KB
/
database.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
$(function() {
var config = {
apiKey: "AIzaSyCKrjgs7TMiPDl-nqaqnBTAhigNrNszQPk",
authDomain: "swamphacks-a6338.firebaseapp.com",
databaseURL: "https://swamphacks-a6338.firebaseio.com",
storageBucket: "swamphacks-a6338.appspot.com",
messagingSenderId: "949283718048"
};
var actionCode = "verifyEmail";
var app = firebase.initializeApp(config);
var auth = app.auth();
function verifyEmail(auth, actionCode) {
// Try to apply the email verification code.
auth.applyActionCode(actionCode).then(function(resp) {
// Email address has been verified.
console.log('it is lit');
// You could also provide the user with a link back to the app.
}).catch(function(error) {
// Code is invalid or expired. Ask the user to verify their email address
// again.
});
}
// :(
function makePass () {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 7; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
$('#subscribe').click( event => {
event.preventDefault();
var email = $("#email").val();
var pass = makePass();
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(email == "" || !email.match(re)) {
$('.submitted').text( "Please enter an e-mail address." );
}
else {
firebase.database().ref('subscribed-users').push({
email
}).then(() =>{
$('.submitted').text( "Thanks for subscribing! Check your e-mail for confirmation." ).show();
$('#subscribe').css(
//'color', '#fff',
'background-color', '#4d94ff'
);
}).then(() => {
auth.createUserWithEmailAndPassword(email, pass);
}).catch(e => { console.log(e); });
firebase.auth().onAuthStateChanged(firebaseUser => {
if (firebaseUser) {
firebaseUser.sendEmailVerification();
console.log('the email sent');
firebase.auth().signOut();
console.log('signed out');
} else {
console.log('something is wrong');
}
});
}
});
//check if the page is refreshed, log the current user out
});