Search code examples
node.jsuber-api

Passing uber authentication from one route to another with nodejs


So I'm using the uber API in nodejs trying to use uber services such as price and time estimates, along with ride requests through my app.

I am able to authorize the user and return with a callback url, but I don't know how to pass the created access token or product_id to the specific /requests or /estimates routes.

Here's my server file:

//  Authenticate uber login with scopes
app.get('/v1.2/login', function(request, response) {
    var url = uber.getAuthorizeUrl(['profile', 'request', 'places', 
'all_trips', 'ride_widgets']);
    response.redirect(url);
    log(url);
    // User can manually go to authorization @ 
https://login.uber.com/oauth/v2/authorize?
client_id=LJGpana69PX47lPLFP5PpIdySYT5CT-G&response_type=code
});


//  Redirect script to authorize uber profile with oAuth 2.0
app.get('/v1.2/callback', function(request, response) {
    uber.authorizationAsync( {
        authorization_code: request.query.code
    })
    .spread(function(access_token, refresh_token, authorizedScopes, 
tokenExpiration) {
    // store the user id and associated access_token, refresh_token, 
scopes and token expiration date
        log('New access_token retrieved: ' + access_token);
        log('... token allows access to scopes: ' + authorizedScopes);
        log('... token is valid until: ' + tokenExpiration);
        log('... after token expiration, re-authorize using 
refresh_token: ' + refresh_token);

        var query = url.parse(request.url, true).query;
        return uber.products.getAllForLocationAsync(query.lat, 
query.lng);
    })
    .then(function(res) {
        log(res);
        // redirect the user back to your actual app
        response.redirect('../Client/index.html');
    })
    .error(function(err) {
        console.error(err);
    });
});


app.post('/v1.2/requests', function(request, response) {
    // extract the query from the request URL
    //var query = url.parse(request.url, true).query;

    uber.requests.createAsync({
        "fare_id": j,
        "start_latitude": request.query.lat,
        "start_longitude": request.query.lng,
        "end_latitude": request.query.goalat,
        "end_longitude": request.query.goalng
    })
    .then(function(res) { 
        log(res); 
        uber.requests.getCurrentAsync()
        .then(function(res) { 
            log(res); 
            res.send('got it');
        })
        .error(function(err) { 
            console.error(err); 
        });
    })
    .error(function(err) { 
        console.error(err); 
    });

});

Like I said before I am just at a loss of passing the access token and getting the product_id to the request route or others. The documentation doesn't really give any examples.

Thanks for the help in advance


Solution

  • For your first question, by executing uber.authorizationAsync, you already get the access_token. It's a property stored in the wrapper object and you can retrieve it with uber.access_token.

    As for your second question, you already obtain the available product IDs with uber.getAllForLocationAsync. However, you don't cache them for your requests call. I'd recommend to split out the product ID call as documented in the README for node-uber. With that, in the request's method, you call the product method and get the JSON response that includes the product IDs you need to make a request.