From ea80e96afcede5657ac244e827ac10049bc83e50 Mon Sep 17 00:00:00 2001 From: Chris Vanderloo Date: Sat, 30 Oct 2021 12:27:18 -0400 Subject: [PATCH] locals and api changes --- locals-example.json | 3 ++- server/api.js | 8 ++++++-- server/app.js | 29 +++++++++++++++++++---------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/locals-example.json b/locals-example.json index ecbd916..1315edd 100644 --- a/locals-example.json +++ b/locals-example.json @@ -3,6 +3,7 @@ "alpha_vantage": "", "robinhood": { "user": "", - "pass": "" + "pass": "", + "device_token": "" } } \ No newline at end of file diff --git a/server/api.js b/server/api.js index b060f80..130c70f 100644 --- a/server/api.js +++ b/server/api.js @@ -10,11 +10,15 @@ module.exports = { robinhood: { login: (user, pass, next) => { request({ - uri: "https://api.robinhood.com/api-token-auth/", + uri: "https://api.robinhood.com/oauth2/token/", method: "POST", form: { username: user, - password: pass + password: pass, + grant_type: 'password', + scope: 'internal', + client_id: 'c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS', + device_token: locals.device_token, } }, (err, res, body) => next(err, res, body)); }, diff --git a/server/app.js b/server/app.js index 8b469d8..f86d4c6 100644 --- a/server/app.js +++ b/server/app.js @@ -36,7 +36,7 @@ function verifyAuth(req,res,next) { app.get("/", verifyAuth, (req,res) => robinhood.user(req.cookies.token, (err, apiRes, body) => { if (err) res.status(403).send(err); else { - res.cookie("account_id",JSON.parse(body).id); + res.cookie("account_id", JSON.parse(body).id); res.render('index', { user: JSON.parse(body) }); @@ -49,16 +49,25 @@ app.get("/login", (req,res) => res.render('login')); app.post("/login", (req,res) => robinhood.login(req.body.user, req.body.pass, (err, apiRes, body) => { + console.log(body) if (err) res.status(403).send(err); - if (!JSON.parse(body).token) - res.status(401).render('login',{ - error: JSON.parse(body).non_field_errors - }); - else { - robinhood.get(JSON.parse(body).token, "https://api.robinhood.com/accounts/", (errAc, apiResAc, bodyAc) => { - res.cookie("userID", JSON.parse(bodyAc).results[0].account_number); - res.cookie("token",JSON.parse(body).token); - res.redirect("/"); + try { + if (!JSON.parse(body).access_token) + res.status(401).render('login',{ + error: JSON.parse(body).non_field_errors + }); + else { + robinhood.get(JSON.parse(body).token, "https://api.robinhood.com/accounts/", (errAc, apiResAc, bodyAc) => { + console.log(bodyAc) + res.cookie("token",JSON.parse(body).access_token); + res.cookie("refresh_token",JSON.parse(body).refresh_token); + res.redirect("/"); + }); + } + } + catch (err) { + res.status(500).render('login',{ + error: 'Internal Server Error: Failed to parse request' }); } })); -- 2.51.2