Skip to content

Instantly share code, notes, and snippets.

@lakshaygupta21
Created April 20, 2021 20:55
Show Gist options
  • Save lakshaygupta21/f127d314f9466b56e468037b1229fe3d to your computer and use it in GitHub Desktop.
Save lakshaygupta21/f127d314f9466b56e468037b1229fe3d to your computer and use it in GitHub Desktop.
router.post("/linkedin-sso-response", async (req, res) => {
try {
if (!req.body.redirectUri || !req.body.authCode) {
return res.json({ msg: "Some Error occured! Try again" });
}
const { authCode, redirectUri } = req.body;
const clientId = config.get("clientId");
const clientSecret = config.get("clientSecret");
var response = await axios.get(
`https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&client_id=${clientId}&client_secret=${clientSecret}&code=${authCode}&redirect_uri=${redirectUri}`
);
const accessToken = response.data ? response.data.access_token : "";
const configA = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
};
const userData = await axios.get("https://api.linkedin.com/v2/me", configA);
const name =
userData.data.localizedFirstName + userData.data.localizedLastName
? userData.data.localizedFirstName +
" " +
userData.data.localizedLastName
: "";
const profilePicture = await axios.get(
"https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))",
configA
);
const elements =
profilePicture.data.profilePicture["displayImage~"].elements;
const identifier =
elements && elements.length > 0
? elements[elements.length - 1].identifiers
: {};
const profilePic = identifier && identifier[0] && identifier[0].identifier;
const emailData = await axios.get(
"https://api.linkedin.com/v2/clientAwareMemberHandles?q=members&projection=(elements*(name,primary,type,handle~))",
configA
);
const emailUser =
emailData.data && emailData.data.elements && emailData.data.elements[0]
? emailData.data.elements[0]["handle~"].emailAddress
: "";
if (emailUser && name) {
return res.json({
name: name,
profilePic: profilePic,
email: emailUser,
});
} else {
return res.json({ msg: "Some Error occured! Try again" });
}
} catch (err) {
// console.log(err)
return res.status(500).json(err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment