Last active
September 25, 2021 19:56
-
-
Save xtiannyeto/f8e510a48c1e7296bd76e1b158a19743 to your computer and use it in GitHub Desktop.
Google Vue component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<button @click="signIn">login</button> | |
<button @click="signOut">log out</button> | |
<div>{{ user }}</div> | |
</template> | |
<script lang="ts"> | |
import { defineComponent, onMounted, ref } from '@vue/runtime-core'; | |
import { installGoogleAuth } from './GoogleAuth'; | |
export default defineComponent({ | |
name: 'SignIn', | |
setup(props, { emit }) { | |
let gAuth: any; | |
const user = ref({}); | |
const options = { | |
clientId: `${process.env.GOOGLE_CLIENT_ID}.apps.googleusercontent.com`, | |
scope: 'profile email', | |
prompt: 'select_account' | |
}; | |
function signIn(): void { | |
if (!gAuth) return; | |
gAuth | |
.signIn() | |
.then((googleUser: any) => { | |
user.value = googleUser; | |
}) | |
.catch((e: any) => { | |
console.log('error', e); | |
}); | |
} | |
function signOut(): void { | |
if (!gAuth) return; | |
gAuth.signOut(); | |
} | |
onMounted(async () => { | |
gAuth = installGoogleAuth(options); | |
}); | |
return { user, signIn, signOut }; | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment