From c5c2a25c7211ec9cf405d6c6b1f61312df679ca4 Mon Sep 17 00:00:00 2001
From: Fiore Basile <fiore.basile@gmail.com>
Date: Thu, 10 Oct 2019 12:37:59 +0200
Subject: [PATCH] Added sign-in from session token

---
 quasar.conf.js            |  2 +-
 src/boot/auth.js          | 15 +++++++++++++++
 src/store/auth/actions.js | 21 ++++++++++++++++++++-
 3 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 src/boot/auth.js

diff --git a/quasar.conf.js b/quasar.conf.js
index 35515ea..6053026 100644
--- a/quasar.conf.js
+++ b/quasar.conf.js
@@ -6,7 +6,7 @@ module.exports = function(ctx) {
   return {
     // app boot file (/src/boot)
     // --> boot files are part of "main.js"
-    boot: ["axios", "firebase"],
+    boot: ["axios", "firebase", "auth"],
 
     css: ["app.styl"],
 
diff --git a/src/boot/auth.js b/src/boot/auth.js
new file mode 100644
index 0000000..4af3e10
--- /dev/null
+++ b/src/boot/auth.js
@@ -0,0 +1,15 @@
+// import something here
+import firebase from "firebase/app";
+import "firebase/auth";
+// "async" is optional
+export default async ({ router, store /* app, router, Vue, ... */ }) => {
+  // something to do
+
+  firebase.auth().onAuthStateChanged(user => {
+    store.dispatch("auth/signInFromSession", user);
+  });
+
+  router.beforeEach((to, from, next) => {
+    return next();
+  });
+};
diff --git a/src/store/auth/actions.js b/src/store/auth/actions.js
index d593d73..95f87d6 100644
--- a/src/store/auth/actions.js
+++ b/src/store/auth/actions.js
@@ -79,8 +79,27 @@ export function signInWithGoogle({ commit }) {
   });
 }
 
+export function signInFromSession({ commit, dispatch }, user) {
+  if (!user) {
+    return dispatch("signOut");
+  }
+
+  commit("setStatus", "loading");
+  return new Promise((resolve, reject) => {
+    commit("setUser", {
+      name: user.displayName,
+      email: user.email,
+      photoURL: user.photoURL,
+      uid: user.uid
+    });
+    commit("setStatus", "success");
+    commit("setError", null);
+    resolve(user);
+  });
+}
+
 export function signOut({ commit }) {
-  firebase
+  return firebase
     .auth()
     .signOut()
     .then(function() {
-- 
GitLab