sign in and up with email confirmation

This commit is contained in:
2018-09-12 18:10:30 +02:00
parent 59beca4846
commit a1f7493ddf
7 changed files with 317 additions and 165 deletions

View File

@@ -5,7 +5,7 @@ import alertify from 'alertify.js';
var firebase = require('./fb').fb
var firebase_orig = require('./fb').firebase
// Initialize Cloud Firestore through Firebase
var db = firebase.firestore();
//var db = firebase.firestore();
class AuthStore {
@@ -36,9 +36,10 @@ class AuthStore {
if (user) {
//Logged in
console.info("Logged in as " + user.uid);
console.info("Signed in as " + user.uid);
Object.assign(this.userData, {
uid: user.uid
uid: user.uid,
userData: user
});
console.info("AuthStore loaded");
@@ -47,7 +48,7 @@ class AuthStore {
} else {
//Logged out
console.info("Logged out");
console.info("Signed out");
Object.assign(this.userData, {
uid: null
});
@@ -59,40 +60,46 @@ class AuthStore {
}
signIn(){
//Verify phone number and then sign in
signIn(email, password){
//Sign in user with email and password
return(
new Promise((resolve, reject) => {
firebase.auth().signInWithEmailAndPassword(email, password).then(function(){
alertify.success("Signed in");
resolve();
}).catch(function(error) {
var errorCode = error.code;
if(errorCode === 'auth/wrong-password') {
//Wrong password
alertify.error("Password incorrect!");
reject(error);
}
if(errorCode === 'auth/user-not-found') {
//User not signed up yet. Sign up user now
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(){
alertify.success("Signed in");
console.info("Verify phone number");
var user = firebase.auth().currentUser;
const phoneNumber = this.userData.phoneCode + this.userData.phoneNumber;
alertify.log("....Sending verification code....");
firebase_orig.auth().signInWithPhoneNumber(phoneNumber, window.recaptchaVerifier).then(function (confirmationResult) {
//Code verification prompt
alertify.log("You will receive a SMS with a verification code soon.");
alertify.prompt("You will receive a SMS with a verification code soon. Please check your SMS on your phone. Enter the code below:",
function (val, ev) {
ev.preventDefault();
//Confirm code
confirmationResult.confirm(val).then(function (result){
alertify.success("Successfully signed in");
}).catch(function () {
alertify.error("Wrong code!");
});
},
function(ev) {
ev.preventDefault();
alertify.error("You've cancelled the phone verification");
}
);
})
.catch(function (error) {
window.recaptchaVerifier.reset(window.recaptchaWidgetId);
alertify.delay(0).error("Base Web doesn't support signing up yet. Please install the mobile app to sign up! Refresh Base Web after!");
console.error(error.code);
});
user.sendEmailVerification().then(function() {
// Confirmation email sent.
resolve();
}).catch(function(error) {
// An error happened.
reject(error);
});
}).catch(function(error) {
alertify.error("Something went wrong. Please check your internet connection");
reject(error);
});
}
});
})
);
}
@@ -100,7 +107,7 @@ class AuthStore {
//Signes user out
firebase_orig.auth().signOut().then(function(){
console.log('User signed out');
}).catch(function(error){
console.error(error);
});

View File

@@ -9,21 +9,13 @@ require("firebase/storage");
var config = {
apiKey: "AIzaSyCjWyrIzayUmEBcMcQyPvcXjNDkQONfJA8",
authDomain: "base-6d44c.firebaseapp.com",
databaseURL: "https://base-6d44c.firebaseio.com",
projectId: "base-6d44c",
storageBucket: "base-6d44c.appspot.com"
apiKey: "AIzaSyDpuphZnuu8U-5CDm1xxdiAFmXukOFav_g",
authDomain: "homepagelogin-d83db.firebaseapp.com",
databaseURL: "https://homepagelogin-d83db.firebaseio.com",
projectId: "homepagelogin-d83db",
storageBucket: "homepagelogin-d83db.appspot.com"
};
/*var config = {
apiKey: "AIzaSyDHnDObdPUAOFJfRpgC1tOG59NhheQYUmk",
authDomain: "netwoko-staging.firebaseapp.com",
databaseURL: "https://netwoko-staging.firebaseio.com",
projectId: "netwoko-staging",
storageBucket: "netwoko-staging.appspot.com"
};*/
var fb;
try {