From 08680af42d68f33f355645b961b711d806b79453 Mon Sep 17 00:00:00 2001 From: Marco Endrizzi <me00531@surrey.ac.uk> Date: Thu, 1 Apr 2021 10:14:43 -0700 Subject: [PATCH] Added Firebase authentication Co-authored-by: Ericsauce <Ericsauce@users.noreply.github.com> --- quasar.conf.js | 1 + src/boot/firebase.js | 14 ++++----- src/store/database.js | 17 ----------- src/store/firebase.js | 67 +++++++++++++++++++++++++++++++++++++++++++ src/store/index.js | 4 +-- 5 files changed, 77 insertions(+), 26 deletions(-) delete mode 100644 src/store/database.js create mode 100644 src/store/firebase.js diff --git a/quasar.conf.js b/quasar.conf.js index b8f3fd4..304aaf8 100644 --- a/quasar.conf.js +++ b/quasar.conf.js @@ -22,6 +22,7 @@ module.exports = configure(function (/* ctx */) { // --> boot files are part of "main.js" // https://quasar.dev/quasar-cli/boot-files boot: [ + 'firebase' ], // https://quasar.dev/quasar-cli/quasar-conf-js#Property%3A-css diff --git a/src/boot/firebase.js b/src/boot/firebase.js index 1df3f6f..a9b7fd5 100644 --- a/src/boot/firebase.js +++ b/src/boot/firebase.js @@ -13,13 +13,13 @@ import "firebase/database"; // Your web app's Firebase configuration // For Firebase JS SDK v7.20.0 and later, measurementId is optional var firebaseConfig = { - apiKey: "AIzaSyBPUdoB3wV6A9L-H1-J5POiQRmgqqcL9Bk", - authDomain: "nightlyfe-308016.firebaseapp.com", - projectId: "nightlyfe-308016", - storageBucket: "nightlyfe-308016.appspot.com", - messagingSenderId: "469882119194", - appId: "1:469882119194:web:4ead2d00c69be60ec41da3", - measurementId: "G-E6SZFDYGCD" + apiKey: "AIzaSyB9Dav9F3qIlHcu9s4zuYbkt5mYBdrHJws", + authDomain: "nightlyfe-117a4.firebaseapp.com", + projectId: "nightlyfe-117a4", + storageBucket: "nightlyfe-117a4.appspot.com", + messagingSenderId: "928276199112", + appId: "1:928276199112:web:a8f1a82e5a2b272eb917fd", + measurementId: "G-Q0NX15EQ0Z" }; // Initialize Firebase diff --git a/src/store/database.js b/src/store/database.js deleted file mode 100644 index 31f0f40..0000000 --- a/src/store/database.js +++ /dev/null @@ -1,17 +0,0 @@ -import { firebaseAuth, firebaseDb } from 'boot/firebase' - -const state = {} -const mutations = {} -const actions = { - registerUser() { - console.log('registerUser') - } -} -const getters = {} - -export default { - state, - mutations, - actions, - getters -} \ No newline at end of file diff --git a/src/store/firebase.js b/src/store/firebase.js new file mode 100644 index 0000000..5b70a98 --- /dev/null +++ b/src/store/firebase.js @@ -0,0 +1,67 @@ +import { firebaseAuth, firebaseDb } from 'boot/firebase' + +const state = { + userDetails: {} +} + +const mutations = { + setUserDetails(state, payload) { + state.userDetails = payload + } +} + +const actions = { + loginUser({}, payload) { + firebaseAuth.signInWithEmailAndPassword(payload.email, payload.password) + .then(response => { + console.log(response) + }) + .catch(error => { + console.log(error.message) + }) + }, + registerUser({}, payload) { + firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password) + .then(response => { + let userId = firebaseAuth.currentUser.uid + firebaseDb.ref('users/' + userId).set({ + username: payload.username, + email: payload.email, + online: true + }) + }) + .catch(error => { + console.log(error.message) + }) + }, + handleAuthStateChanged({ commit }) { + firebaseAuth.onAuthStateChanged(user => { + if(user) { + // User logged in + let userId = firebaseAuth.currentUser.uid + firebaseDb.ref('users/' + userId).once('value', snapshot => { + let userDetails = snapshot.val() + commit('setUserDetails', { + name: userDetails.username, + email: userDetails.email, + userId: userId + }) + }) + } + else { + // User logged out + commit('setUserDetails', {}) + } + }) + } +} + +const getters = {} + +export default { + namespaced: true, + state, + mutations, + actions, + getters +} \ No newline at end of file diff --git a/src/store/index.js b/src/store/index.js index f632499..e590963 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -3,7 +3,7 @@ import { createStore } from 'vuex' import mapStyles from './mapStyles' import menuList from './menuList' -import database from './database' +import firebase from './firebase' // import example from './module-example' @@ -23,7 +23,7 @@ export default store(function (/* { ssrContext } */) { menuList: menuList['data'] }, modules: { - database + firebase }, // enable strict mode (adds overhead!) -- GitLab