Skip to content

Commit 53bcb11

Browse files
authored
Merge pull request #32 from guruahn/pr/31
README.md file updated - nuxt and other explanation - improved sample code - version up
2 parents c70a060 + 4f3bd19 commit 53bcb11

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

README.md

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Handling Google sign-in and sign-out for Vue.js applications.
55
![GitHub](https://img.shields.io/github/license/guruahn/vue-google-oauth2.svg)
66
![vue-google-oauth2](https://img.shields.io/npm/dt/vue-google-oauth2.svg)
77

8-
We support [TypeScript](https://www.typescriptlang.org/) 😎 but not [Nuxt](https://ko.nuxtjs.org/). 😢
8+
We support [TypeScript](https://www.typescriptlang.org/) and [Nuxt](https://ko.nuxtjs.org/). 😎
99

1010
[Front-end Demo](https://stupefied-darwin-da9533.netlify.com/)
1111
## Installation
@@ -33,7 +33,37 @@ Vue.use(GAuth, gauthOption)
3333
```
3434
Please Don't use `plus.login` scope. [It will be deprecated.](https://developers.google.com/identity/sign-in/web/quick-migration-guide)
3535

36-
### Options
36+
### Initialization for Nuxt
37+
1. creates plug-in file for nuxt
38+
39+
```javascript
40+
// plugins/vue-google-oauth2.js
41+
// file name can be changed to whatever you want
42+
import Vue from 'vue'
43+
import GAuth from 'vue-google-oauth2'
44+
45+
const gauthOption = {
46+
clientId: 'CLIENT_ID.apps.googleusercontent.com',
47+
scope: 'profile email',
48+
prompt: 'select_account'
49+
}
50+
Vue.use(GAuth, gauthOption)
51+
52+
```
53+
54+
2. adds plugin to nuxt config file
55+
```javascript
56+
...
57+
plugins: [
58+
...
59+
'./plugins/vue-google-oauth2'
60+
],
61+
62+
...
63+
64+
```
65+
66+
## Options
3767
| Property | Type | Required | Description |
3868
|--------------|----------|-----------------|-----------------|
3969
| clientId | String | Required. | The app's client ID, found and created in the Google Developers Console. |
@@ -52,54 +82,36 @@ Please Don't use `plus.login` scope. [It will be deprecated.](https://developers
5282
| getAuthCode | function for getting authCode | Function |
5383
| signOut | function for sign-out | Function |
5484

55-
## Usage - Getting authorization code
56-
>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.
85+
86+
## Usages
87+
### Getting authorization code
88+
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.
89+
90+
The `access_token` and `refresh_token` can be saved in backend storage for reuse and refresh. In this way, you can avoid exposing your api key or secret key whenever you need to use various google APIs.
5791

5892
```javascript
59-
this.$gAuth.getAuthCode()
60-
.then(authCode => {
61-
//on success
62-
return this.$http.post('http://your-backend-server.com/auth/google', { code: authCode, redirect_uri: 'postmessage' })
63-
})
64-
.then(response => {
65-
//after ajax
66-
})
67-
.catch(error => {
68-
//on fail do something
69-
})
93+
const authCode = await this.$gAuth.getAuthCode()
94+
const response = await this.$http.post('http://your-backend-server-api-to-use-authcode', { code: authCode, redirect_uri: 'postmessage' })
7095
```
7196

72-
## Usage - Directly get back the `access_token` and `id_token` or use api request
97+
### Sign-in: Directly get back the `access_token` and `id_token`
7398

7499
```javascript
75-
this.$gAuth.signIn()
76-
.then(GoogleUser => {
77-
// On success do something, refer to https://developers.google.com/api-client-library/javascript/reference/referencedocs#googleusergetid
78-
console.log('user', GoogleUser)
79-
// GoogleUser.getId() : Get the user's unique ID string.
80-
// GoogleUser.getBasicProfile() : Get the user's basic profile information.
81-
// GoogleUser.getAuthResponse() : Get the response object from the user's auth session. access_token and so on
82-
this.isSignIn = this.$gAuth.isAuthorized
83-
})
84-
.catch(error => {
85-
//on fail do something
86-
})
100+
const googleUser = await this.$gAuth.signIn()
101+
// googleUser.getId() : Get the user's unique ID string.
102+
// googleUser.getBasicProfile() : Get the user's basic profile information.
103+
// googleUser.getAuthResponse() : Get the response object from the user's auth session. access_token and so on
104+
this.isSignIn = this.$gAuth.isAuthorized
105+
87106
```
88107

89108
refer to [google signIn reference : GoogleUser](https://developers.google.com/api-client-library/javascript/reference/referencedocs#googleusergetid)
90109

91110

92-
## Usage - Sign-out
111+
### Sign-out
93112
Handling Google sign-out
94113
```javascript
95-
//you can use promise from v1.1.0 also
96-
this.$gAuth.signOut()
97-
.then(() => {
98-
// things to do when sign-out succeeds
99-
})
100-
.catch(error => {
101-
// things to do when sign-out fails
102-
})
114+
const response = await sthis.$gAuth.signOut()
103115
```
104116

105117
## Extra - Directly get `access_token` and `refresh_token` on Server-side
@@ -124,3 +136,11 @@ curl -d "client_id=YOUR_CLIENT_ID&\
124136
- [Google API Client Libraries : Methods and Classes](https://developers.google.com/api-client-library/javascript/reference/referencedocs)
125137
- If you are curious of how the entire Google sign-in flow works, please refer to the diagram below
126138
![Google Sign-in Flow](http://i.imgur.com/BQPXKyT.png)
139+
140+
141+
## FAQ
142+
### The failure of initialization happens
143+
You can check the brower console to check errors which occur during initialization.
144+
The most of errors are from inproper setting of google oauth2 credentials setting in Google Developer Console.
145+
After changing the settings, you have to do hard refresh to clear your caches.
146+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-google-oauth2",
3-
"version": "1.4.3",
3+
"version": "1.5.3",
44
"description": "Handling Google Auth2 sign-in and sign-out",
55
"main": "index.js",
66
"types": "index.ts",

0 commit comments

Comments
 (0)