Created
December 10, 2015 20:15
-
-
Save nickgartmann/ea761b75ed9d0f44296a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{Model, Attributes} = require('nylas-exports') | |
_ = require('remote').require('underscore') | |
request = require('remote').require('request') | |
Q = require('../vendor/q') | |
CLIENT_ID = "REDACTED" | |
CLIENT_SECRET = "REDACTED" | |
REDIRECT_URI = "http://localhost:9999" | |
class Credential extends Model | |
@attributes: _.extend {}, Model.attributes, | |
'id': Attributes.String | |
queryable: true | |
modelKey: 'id' | |
'authToken': Attributes.String | |
modelKey: 'authToken' | |
'refreshToken': Attributes.String | |
modelKey: 'refreshToken' | |
verify: () => | |
deferred = Q.defer() | |
request | |
url: "https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=#{@authToken}" | |
method: "GET" | |
, (err, resp, body) => | |
if err || JSON.parse(body).error | |
deferred.reject(body) | |
else | |
deferred.resolve(@) | |
return deferred.promise | |
refresh: () => | |
querystring = require('querystring') | |
form = querystring.stringify | |
client_id: CLIENT_ID | |
client_secret: CLIENT_SECRET | |
grant_type: "refresh_token" | |
refresh_token: @refreshToken | |
deferred = Q.defer() | |
request | |
url: "https://www.googleapis.com/oauth2/v4/token" | |
method: "POST" | |
body: form | |
headers: | |
'Content-Type': 'application/x-www-form-urlencoded' | |
'Content-Length': form.length | |
, (err, resp, body) => | |
json = JSON.parse(body) | |
console.log(json) | |
if err || json.error | |
deferred.reject(err) | |
else | |
@accessToken = json.access_token | |
deferred.resolve(@) | |
deferred.promise | |
@exchangeAuthorizationToken: (authorizationToken) -> | |
querystring = require('querystring') | |
form = querystring.stringify | |
client_id: CLIENT_ID | |
client_secret: CLIENT_SECRET | |
grant_type: "authorization_code" | |
code: authorizationToken | |
redirect_uri: REDIRECT_URI | |
deferred = Q.defer() | |
request | |
url: "https://www.googleapis.com/oauth2/v4/token" | |
method: "POST" | |
body: form | |
headers: | |
'Content-Type': 'application/x-www-form-urlencoded' | |
'Content-Length': form.length | |
, (err, resp, body) => | |
json = JSON.parse(body) | |
console.log(json) | |
if err || json.error | |
deferred.reject(err) | |
else | |
deferred.resolve(new Credential({ | |
authToken: json.access_token, | |
refreshToken: json.refresh_token, | |
clientId: json.refresh_token | |
})) | |
deferred.promise | |
module.exports = Credential |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Credential = require('./Credential') | |
{AccountStore, DatabaseStore} = require 'nylas-exports' | |
class SampleButton extends React.Component | |
componentDidMount: => | |
credential = DatabaseStore.findBy(Credential, accountId: AccountStore.current().id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment