Skip to content

Updated the getAuthObject method to get the current user's access_token #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ The user object corresponds to the [Google User Basic Profile](https://developer

 

---
> **Vue.$gapi.getAuthObject()**

Returns through an always resolved promise the current user auth session data in a readable format if one is connected, or `null` if no user is connected.
```javascript
this.$gapi.getAuthObject()
.then(authData => {
if (authData) {
console.log('Current user access_token is %s', authData.access_token)
} else {
console.log('No user is connected.')
}
})
```
The user auth session object corresponds to the [Google User Auth Response](https://developers.google.com/identity/sign-in/web/reference#gapiauth2authresponse) informations, in a friendly format :
```javascript
{
access_token: google_user_access_token_granted,
id_token: id_token_granted,
scope: given_name,
expires_in: seconds_until_access_token_expires,
first_issued_at: user_first_grant_request,
expires_at: timestamp_which_access_token_expire
}
```

 

---
> **Vue.$gapi.signIn()**

Expand Down
15 changes: 14 additions & 1 deletion src/core/gapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function _formatUser (guser) {
}
}

/** Get the response object from the user's auth session. */
function _formatAuthObject (guser) {
if (!guser.getAuthResponse) return null
return guser.getAuthResponse()
}

export default class GAPI {
/**
* The constructor expect as parameter the config object, containing
Expand Down Expand Up @@ -130,9 +136,16 @@ export default class GAPI {
})
}

/** returns asynchronously true if the user is signed in */
/** returns the current user auth data if signed in, undefined otherwise */
getAuthObject () {
return this._libraryInit('auth2')
.then(auth => {
if (auth.isSignedIn.get()) {
return Promise.resolve(_formatAuthObject(auth.currentUser.get()))
} else {
return Promise.resolve(null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason to return a null resolved promise in place of a rejected one with a not logged message?
A rejected promise should be more universal and more readable no?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vertcitron I'd say it is to remain consistent with the behavior of currentUser() defined below.

}
})
}

/** returns the current user if signed in, undefined otherwise */
Expand Down