Skip to content

Instantly share code, notes, and snippets.

@xtiannyeto
Last active September 25, 2021 19:56
Show Gist options
  • Save xtiannyeto/f8e510a48c1e7296bd76e1b158a19743 to your computer and use it in GitHub Desktop.
Save xtiannyeto/f8e510a48c1e7296bd76e1b158a19743 to your computer and use it in GitHub Desktop.
Google Vue component
<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