Skip to content

Commit 2787d2c

Browse files
committed
readme, sample.html refactoring
1 parent f9daa9f commit 2787d2c

File tree

3 files changed

+27
-85
lines changed

3 files changed

+27
-85
lines changed

README.md

Lines changed: 23 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# vue-google-oauth2
22
Handling Google sign-in and sign-out for Vue.js applications.
3-
43
Forked from https://github.com/TinyNova/vue-google-oauth
54

6-
Same as fork but allows you to use google-oauth2.
75

86
## Installation
97
```
@@ -19,10 +17,21 @@ Vue.use(GAuth, {clientId: '4XXXXXXXX93-2gqknkvdjfkdfkvb8uja2k65sldsms7qo9.apps.g
1917

2018
```
2119

22-
## Usage - Sign-in
23-
### (a) Handling Google sign-in, getting the one-time authorization code from Google
24-
25-
#### Frontend-side(Vue.js)
20+
## Property
21+
| Property | Description | Type |
22+
|--------------|--------------------|----------|
23+
| GoogleAuth | return of gapi.auth2.getAuthInstance() | Object |
24+
| isAuthorized | Whether or not you have auth | Boolean |
25+
| isInit | Whether or not api init | Boolean |
26+
| isLoaded | Whether or not api init | Function |
27+
| signIn | function for sign-in | Function |
28+
| getAuthCode | function for getting authCode | Function |
29+
| signOut | function for sign-out | Function |
30+
31+
## Usage - Getting authorization code
32+
>The `authCode` that is being returned is the `one-time code` that you can send to your backend server, so that the server can exchange for its own access_token and refresh_token.
33+
34+
### Frontend-side(Vue.js)
2635
```javascript
2736
this.$gAuth.getAuthCode()
2837
.then(authCode => {
@@ -36,9 +45,9 @@ this.$gAuth.getAuthCode()
3645
//on fail do something
3746
})
3847
```
39-
The `authCode` that is being returned is the `one-time code` that you can send to your backend server, so that the server can exchange for its own access token and refresh token.
4048

41-
#### Backend-side(Golang)
49+
50+
### Backend-side(Golang)
4251
```go
4352
auth_code := ac.Code //from front-end side
4453
// generate a config of oauth
@@ -64,39 +73,21 @@ Note, ```RedirectURL``` must be ```postmessage```!!
6473

6574

6675

67-
### (b) Alternatively, if you would like to directly get back the access_token and id_token
76+
## Usage - Directly get back the access_token and id_token or use api request
6877

6978
```javascript
7079
this.$gAuth.signIn()
7180
.then(GoogleUser => {
72-
//on success do something
73-
console.log('GoogleUser', GoogleUser)
81+
// On success do something, refer to https://developers.google.com/api-client-library/javascript/reference/referencedocs#googleusergetid
82+
console.log('user', GoogleUser)
83+
this.isSignIn = this.$gAuth.isAuthorized
7484
})
7585
.catch(error => {
7686
//on fail do something
7787
})
7888
```
7989

80-
The `googleUser` object that is being returned will be:
81-
```javascript
82-
{
83-
"token_type": "Bearer",
84-
"access_token": "xxx",
85-
"scope": "xxx",
86-
"login_hint": "xxx",
87-
"expires_in": 3600,
88-
"id_token": "xxx",
89-
"session_state": {
90-
"extraQueryParams": {
91-
"authuser": "0"
92-
}
93-
},
94-
"first_issued_at": 1234567891011,
95-
"expires_at": 1234567891011,
96-
"idpId": "google"
97-
}
98-
```
99-
refer to [google signIn reference : users](https://developers.google.com/identity/sign-in/web/reference#users)
90+
refer to [google signIn reference : GoogleUser](https://developers.google.com/api-client-library/javascript/reference/referencedocs#googleusergetid)
10091

10192
## Usage - Sign-out
10293
Handling Google sign-out
@@ -111,48 +102,8 @@ this.$gAuth.signOut()
111102
})
112103
```
113104

114-
## Usage - Check api loaded
115-
Handling Check Google api loaded
116-
```html
117-
<template>
118-
<div>
119-
<h1>Test</h1>
120-
<button @click="handleClickGetAuth" :disabled="!isLoaded">get auth code</button>
121-
</div>
122-
</template>
123-
<script>
124-
data () {
125-
return {
126-
isLoaded: false
127-
}
128-
},
129-
methods: {
130-
handleClickGetAuth(){
131-
this.$gAuth.getAuthCode()
132-
.then(authCode => {
133-
//on success
134-
return this.$http.post('http://your-backend-server.com/auth/google', { code: authCode, redirect_uri: 'postmessage' })
135-
})
136-
.then(response => {
137-
//and then
138-
})
139-
.catch(error => {
140-
//on fail do something
141-
})
142-
},
143-
},
144-
mounted(){
145-
let that = this
146-
let checkGauthLoad = setInterval(function(){
147-
that.isLoaded = that.$gAuth.isLoaded()
148-
if(that.isLoaded) clearInterval(checkGauthLoad)
149-
}, 1000);
150-
}
151-
</script>
152-
```
153-
154105
## Additional Help
155106
- [sample login page HTML file](https://github.com/guruahn/vue-google-oauth2/blob/master/sample.html).
156-
- [sign-in web reference](https://developers.google.com/identity/sign-in/web/reference)
107+
- [Google API Client Libraries : Methods and Classes](https://developers.google.com/api-client-library/javascript/reference/referencedocs)
157108
- If you are curious of how the entire Google sign-in flow works, please refer to the diagram below
158109
![Google Sign-in Flow](http://i.imgur.com/BQPXKyT.png)

index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ var googleAuth = (function () {
3232
function Auth(){
3333
if(!(this instanceof Auth))
3434
return new Auth()
35-
this.Gapi = null /* window.gapi */
3635
this.GoogleAuth = null /* window.gapi.auth2.getAuthInstance() */
3736
this.isAuthorized = false
38-
this.config = null
3937
this.isInit = false
4038

4139
this.isLoaded = function(){
@@ -45,15 +43,14 @@ var googleAuth = (function () {
4543
};
4644

4745
this.load = (config) => {
48-
this.config = config
4946
installClient()
5047
.then(() => {
5148
return initClient(config)
5249
})
5350
.then((gapi) => {
54-
this.Gapi = gapi
5551
this.GoogleAuth = gapi.auth2.getAuthInstance()
5652
this.isInit = true
53+
5754
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
5855
})
5956
};

sample.html

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h1>Test</h1>
44
<button @click="handleClickGetAuth" :disabled="!isInit">get auth code</button>
55
<button @click="handleClickSignIn" v-if="!isSignIn" :disabled="!isInit">signIn</button>
6-
<button @click="handleClickSignOut" v-if="isSignIn" :disabled="!isInit">sign out</button>
6+
<button @click="handleClickSignOut" v-if="isSignIn" :disabled="!isInit">signOout</button>
77
</div>
88
</template>
99

@@ -42,14 +42,8 @@ <h1>Test</h1>
4242
handleClickSignIn(){
4343
this.$gAuth.signIn()
4444
.then(user => {
45-
// On success do something, refer to https://developers.google.com/identity/sign-in/web/reference#users
46-
console.log('user', user)
47-
// You can execute API, refer to https://developers.google.com/identity/protocols/googlescopes
48-
var request = this.Gapi.client.request({
49-
'method': 'GET',
50-
'path': '/drive/v3/about',
51-
'params': {'fields': 'user'}
52-
});
45+
// On success do something, refer to https://developers.google.com/api-client-library/javascript/reference/referencedocs#googleusergetid
46+
console.log('user', GoogleUser)
5347
this.isSignIn = this.$gAuth.isAuthorized
5448
})
5549
.catch(error => {

0 commit comments

Comments
 (0)