Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Replaced sign in with popup to sign in with redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
plusk committed Mar 19, 2020
1 parent 944b0bc commit 6bfb0b7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 55 deletions.
55 changes: 3 additions & 52 deletions src/components/LoginButton.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<button @click="socialLogin()" class="button_style">
<button @click="signInWithGoogle()" class="button_style">
<img alt="Google Logo" src="../assets/google-logo.svg" />
<span>
Logg inn med Google
Expand All @@ -9,62 +9,13 @@

<script>
import firebase from "firebase";
import fb from "@/firebaseConfig.js";
export default {
name: "LoginButton",
methods: {
socialLogin() {
signInWithGoogle() {
const provider = new firebase.auth.GoogleAuthProvider();
firebase
.auth()
.signInWithPopup(provider)
.then(() => {
const { currentUser } = firebase.auth();
fb.additionalUserInfoCollection
.doc(currentUser.uid)
.get()
.then(userInfo => {
if (userInfo.data() && userInfo.data().phoneNumber) {
this.$store.dispatch("SET_CURRENT_USER", {
...currentUser,
...userInfo.data()
});
this.$router.replace("home");
} else {
this.$dialog
.prompt({
title: "Telefonnummer",
body: "Skriv inn telefonnummeret ditt uten landskode",
promptHelp: ""
})
.then(dialog => {
fb.additionalUserInfoCollection
.doc(currentUser.uid)
.set({ phoneNumber: dialog.data });
this.$store.dispatch("SET_CURRENT_USER", {
...currentUser,
phoneNumber: dialog.data
});
this.$router.replace("home");
})
.catch(() => {
firebase
.auth()
.signOut()
.then(() => {
this.$store.dispatch("SET_CURRENT_USER", null);
})
.catch(error => {
console.log(`something went wrong ${error.message}`);
});
});
}
});
})
.catch(err => {
alert(`Oops. ${err.message}`);
});
firebase.auth().signInWithRedirect(provider);
}
}
};
Expand Down
45 changes: 45 additions & 0 deletions src/helpers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import firebase from "firebase";
import fb from "@/firebaseConfig.js";

export default (context, user) => {
fb.additionalUserInfoCollection
.doc(user.uid)
.get()
.then(userInfo => {
if (userInfo.data() && userInfo.data().phoneNumber) {
context.$store.dispatch("SET_CURRENT_USER", {
...user,
...userInfo.data()
});
context.$router.replace("home");
} else {
context.$dialog
.prompt({
title: "Telefonnummer",
body: "Skriv inn telefonnummeret ditt uten landskode",
promptHelp: ""
})
.then(dialog => {
fb.additionalUserInfoCollection
.doc(user.uid)
.set({ phoneNumber: dialog.data });
context.$store.dispatch("SET_CURRENT_USER", {
...user,
phoneNumber: dialog.data
});
context.$router.replace("home");
})
.catch(() => {
firebase
.auth()
.signOut()
.then(() => {
context.$store.dispatch("SET_CURRENT_USER", null);
})
.catch(error => {
console.log(`something went wrong ${error.message}`);
});
});
}
});
};
5 changes: 3 additions & 2 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import MyRequestsView from "@/views/MyRequestsView.vue"; // eslint-disable
import MyAssignedRequestsView from "@/views/MyAssignedRequestsView.vue"; // eslint-disable
import EditRequestView from "@/views/EditRequestView.vue";
import MyPageView from "@/views/MyPageView.vue";
import firebase from "firebase";

import store from "@/store/index";

Vue.use(VueRouter);

Expand Down Expand Up @@ -97,7 +98,7 @@ const router = new VueRouter({

router.beforeEach((to, from, next) => {
const requiresAuth = to.matched.some(x => x.meta.requiresAuth);
const { currentUser } = firebase.auth();
const { currentUser } = store.getters;
if (requiresAuth && !currentUser) {
next("/login");
} else if (requiresAuth && currentUser) {
Expand Down
2 changes: 1 addition & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const store = new Vuex.Store({
}
});

// Firebase auth state change
// Firebase auth state change, login and logout
fb.auth.onAuthStateChanged(user => {
if (user) {
fb.additionalUserInfoCollection
Expand Down
34 changes: 34 additions & 0 deletions src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,46 @@
</template>

<script>
import firebase from "firebase";
import handleSignedIn from "@/helpers/auth";
import LoginButton from "@/components/LoginButton.vue";
export default {
name: "login",
components: {
LoginButton
},
methods: {
googleSetUpSignInCompleteListener() {
firebase
.auth()
.getRedirectResult()
.then(result => {
const { user } = result;
if (user) {
handleSignedIn(this, user);
}
})
.catch(error => {
console.log(`something went wrong ${error.message}`);
/* TODO: We should have a handler here for when we add Vipps etc
if (errorCode === "auth/account-exists-with-different-credential") {
alert(
"You have already signed up with a different auth provider for that email."
);
// If you are using multiple auth providers on your app you should handle linking
// the user's accounts here.
} else {
console.error(error);
}
*/
});
}
},
created() {
this.googleSetUpSignInCompleteListener();
}
};
</script>
Expand Down

0 comments on commit 6bfb0b7

Please sign in to comment.