Last active
March 11, 2018 23:47
-
-
Save mootrichard/ccf8115c1f82b0ec352d19d4b2a33a64 to your computer and use it in GitHub Desktop.
Callback for Square OAuth
This file contains hidden or 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
router.get("/callback", (req, res) => { | |
const tokenURL = "https://connect.squareup.com/oauth2/token"; | |
const redirectURI = "https://square-oauth-example.glitch.me/auth/callback"; | |
if (req.query.state === req.auth.state) { | |
axios | |
.post(tokenURL, { | |
client_id: CLIENT_ID, | |
client_secret: SECRET, | |
code: req.query.code, | |
redirect_uri: redirectURI | |
}) | |
.then((token) => { | |
axios | |
.get("https://connect.squareup.com/v1/me", { | |
"headers": { | |
"Authorization": `Bearer ${token.data.access_token}` | |
} | |
}) | |
.then((user) => { | |
User.create({ | |
name: user.data.name, | |
email: user.data.email, | |
squareId: user.data.id, | |
accessToken: User.encryptToken(token.data.access_token), | |
tokenExp: token.data.expires_at | |
}).then((user) => { | |
req.auth.isLoggedIn = true; | |
req.auth.userId = user.id; | |
res.location(`/authorized`); | |
res.redirect(301, `/authorized`); | |
}); | |
}) | |
.catch(error => { | |
console.log(error) | |
res.status(500).send(error.data); | |
}); | |
}) | |
.catch(error => { | |
console.log(error) | |
res.status(500).send(error.data); | |
}); | |
} else { | |
res.redirect("/"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment