Skip to content
Permalink
Browse files

サインイン/サインアップ時にユーザーをFirestoreに保存

  • Loading branch information...
h-sakano committed Dec 3, 2018
1 parent f113196 commit eee5bc0775a8cd8679c63987ea84dd8b738f919c
Showing with 39 additions and 1 deletion.
  1. +10 −0 src/firestore.ts
  2. +7 −0 src/types/UserDataType.ts
  3. +22 −1 src/views/SignIn.vue
@@ -0,0 +1,10 @@
import firebase from 'firebase/app'
import 'firebase/firestore'

const firestore = firebase.firestore()
const settings = {
timestampsInSnapshots: true,
}
firestore.settings(settings)

export default firestore
@@ -0,0 +1,7 @@
import firebase from 'firebase/app'

export default interface UserDataType {
createdAt?: firebase.firestore.FieldValue
updatedAt?: firebase.firestore.FieldValue
profile?: string
}
@@ -8,9 +8,11 @@
<script lang="ts">
import firebase from 'firebase/app'
import 'firebase/auth'
import firestore from '@/firestore'
import firebaseui from 'firebaseui'
import 'firebaseui/dist/firebaseui.css'
import { Component, Vue } from 'vue-property-decorator'
import UserDataType from '@/types/UserDataType'
@Component
export default class SignIn extends Vue {
@@ -20,6 +22,15 @@ export default class SignIn extends Vue {
ui = new firebaseui.auth.AuthUI(firebase.auth())
}
ui.start('#firebaseui-auth-container', {
callbacks: {
signInSuccessWithAuthResult: (authResult, redirectUrl) => {
this.saveUser(authResult.user)
.then(() => {
this.$router.push('/mypage')
})
return false
},
},
signInFlow: 'redirect',
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
@@ -29,9 +40,19 @@ export default class SignIn extends Vue {
defaultCountry: 'JP',
},
],
signInSuccessUrl: '/mypage',
})
}
private async saveUser (user: firebase.User) {
const userRef = firestore.collection('users').doc(user.uid)
const userDoc = await userRef.get()
const userData: UserDataType = {}
if (!userDoc.exists) {
userData.createdAt = firebase.firestore.FieldValue.serverTimestamp()
}
userRef.set(userData, { merge: true })
}
}
</script>

0 comments on commit eee5bc0

Please sign in to comment.
You can’t perform that action at this time.