forked from guruahn/vue-google-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.html
75 lines (70 loc) · 1.93 KB
/
sample.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<template>
<div>
<h1>Test</h1>
<button @click="handleClickGetAuth" :disabled="!isInit">get auth code</button>
<button @click="handleClickSignIn" v-if="!isSignIn" :disabled="!isInit">signIn</button>
<button @click="handleClickSignOut" v-if="isSignIn" :disabled="!isInit">signOout</button>
</div>
</template>
<script>
/**
* You should first need to place these 2 lines of code in your APP ENTRY file, e.g. src/main.js
*
* import GAuth from 'vue-google-oauth2'
* Vue.use(GAuth, {clientId: '4584XXXXXXXX-2gqknkvdjfkdfkvb8uja2k65sldsms7qo9.apps.googleusercontent.com'})
*
*/
export default {
name: 'test',
data () {
return {
isInit: false,
isSignIn: false
}
},
methods: {
handleClickGetAuth(){
this.$gAuth.getAuthCode()
.then(authCode => {
// On success
return this.$http.post('http://your-backend-server.com/auth/google', { code: authCode, redirect_uri: 'postmessage' })
})
.then(response => {
// And then
})
.catch(error => {
// On fail do something
})
},
handleClickSignIn(){
this.$gAuth.signIn()
.then(user => {
// On success do something, refer to https://developers.google.com/api-client-library/javascript/reference/referencedocs#googleusergetid
console.log('user', GoogleUser)
this.isSignIn = this.$gAuth.isAuthorized
})
.catch(error => {
// On fail do something
})
},
handleClickSignOut(){
this.$gAuth.signOut()
.then(() => {
// On success do something
this.isSignIn = this.$gAuth.isAuthorized
})
.catch(error => {
// On fail do something
})
}
},
mounted(){
let that = this
let checkGauthLoad = setInterval(function(){
that.isInit = that.$gAuth.isInit
that.isSignIn = that.$gAuth.isAuthorized
if(that.isInit) clearInterval(checkGauthLoad)
}, 1000);
}
}
</script>