From 797c40b9a078809a9f3b80954867bd11a095876d Mon Sep 17 00:00:00 2001 From: AMAICDAX Date: Wed, 3 Sep 2025 10:37:15 +0200 Subject: [PATCH] Adding routes & linkedin auth --- README.md | 38 ++++++++++++++++++++++++ app.js | 20 +++++++++++++ controllers/linkedin.js | 66 +++++++++++++++++++++++++++++++++++++++++ package.json | 16 ++++++++++ routes/api.routes.js | 16 ++++++++++ routes/front.routes.js | 2 ++ 6 files changed, 158 insertions(+) create mode 100644 README.md create mode 100644 app.js create mode 100644 controllers/linkedin.js create mode 100644 package.json create mode 100644 routes/api.routes.js create mode 100644 routes/front.routes.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..5c95683 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# CVGen Project + +## Overview +CVGen is a Node.js application that connects to the LinkedIn API to retrieve user profile information. The application is designed to display this information on a blank page without the need for HTML or CSS. + +## Project Structure +``` +cvgen +├── controllers +│ └── linkedin.js # Handles LinkedIn API connection and user profile retrieval +├── routes +│ ├── api.routes.js # Defines API routes for LinkedIn authentication and data fetching +│ └── front.routes.js # Defines front-end routes (currently not utilized) +├── views # Currently empty, not needed for this project +├── public +│ └── img # Contains images (not utilized in this project) +├── app.js # Entry point of the application, sets up server and routes +├── package.json # Configuration file for npm, lists project dependencies +└── README.md # Documentation for the project +``` + +## Setup Instructions +1. Clone the repository to your local machine. +2. Navigate to the project directory. +3. Run `npm install` to install the required dependencies. +4. Configure your LinkedIn API credentials in the `linkedin.js` controller. +5. Start the application by running `node app.js`. + +## Usage +- The application will connect to the LinkedIn API and retrieve user profile information. +- The retrieved information will be displayed on a blank page. + +## Dependencies +- Express: A web framework for Node.js. +- LinkedIn API libraries: For connecting and interacting with the LinkedIn API. + +## License +This project is licensed under the MIT License. \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..af9704d --- /dev/null +++ b/app.js @@ -0,0 +1,20 @@ +const express = require("express"); +const bodyParser = require("body-parser"); +const apiRoutes = require("./routes/api.routes"); +const frontRoutes = require("./routes/front.routes"); + +const app = express(); +const PORT = process.env.PORT || 3000; + +// Middleware +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); + +// Routes +app.use("/api", apiRoutes); +app.use("/", frontRoutes); + +// Start the server +app.listen(PORT, () => { + console.log(`Server is running on http://localhost:${PORT}`); +}); diff --git a/controllers/linkedin.js b/controllers/linkedin.js new file mode 100644 index 0000000..cac9651 --- /dev/null +++ b/controllers/linkedin.js @@ -0,0 +1,66 @@ +// filepath: c:\Users\amaizy\Desktop\cvgen\controllers\linkedin.js + +const axios = require("axios"); +const querystring = require("querystring"); + +const clientId = "780w7gsy8eysmj"; +const clientSecret = "WPL_AP1.w6OTTkAndAdT3PYF.UZEcwQ=="; +const redirectUri = "http://localhost:3000/api/auth/linkedin/callback"; +const scope = "r_liteprofile r_emailaddress"; + +let accessToken = ""; + +const authenticateUser = (req, res) => { + const authUrl = `https://www.linkedin.com/oauth/v2/authorization?${querystring.stringify( + { + response_type: "code", + client_id: clientId, + redirect_uri: redirectUri, + scope: scope, + } + )}`; + res.redirect(authUrl); +}; + +const callback = async (req, res) => { + const { code } = req.query; + + try { + const tokenResponse = await axios.post( + "https://www.linkedin.com/oauth/v2/accessToken", + querystring.stringify({ + grant_type: "authorization_code", + code: code, + redirect_uri: redirectUri, + client_id: clientId, + client_secret: clientSecret, + }) + ); + + accessToken = tokenResponse.data.access_token; + res.send( + "Authentication successful! You can now retrieve your profile information." + ); + } catch (error) { + res.status(500).send("Error retrieving access token."); + } +}; + +const getUserProfile = async (req, res) => { + try { + const profileResponse = await axios.get("https://api.linkedin.com/v2/me", { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }); + res.json(profileResponse.data); + } catch (error) { + res.status(500).send("Error retrieving user profile."); + } +}; + +module.exports = { + authenticateUser, + callback, + getUserProfile, +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..1a3a121 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "cvgen", + "version": "1.0.0", + "description": "A project to connect to the LinkedIn API and retrieve user profile information.", + "main": "app.js", + "scripts": { + "start": "node app.js" + }, + "dependencies": { + "express": "^4.17.1", + "axios": "^0.21.1", + "dotenv": "^8.2.0" + }, + "author": "", + "license": "ISC" +} \ No newline at end of file diff --git a/routes/api.routes.js b/routes/api.routes.js new file mode 100644 index 0000000..bded0fd --- /dev/null +++ b/routes/api.routes.js @@ -0,0 +1,16 @@ +// filepath: c:\Users\amaizy\Desktop\cvgen\routes\api.routes.js + +const express = require("express"); +const router = express.Router(); +const linkedinController = require("../controllers/linkedin"); + +// Route for LinkedIn authentication +router.get("/auth/linkedin", linkedinController.authenticate); + +// Callback route for LinkedIn OAuth2 +router.get("/auth/linkedin/callback", linkedinController.callback); + +// Route for fetching user profile information +router.get("/profile", linkedinController.getProfile); + +module.exports = router; diff --git a/routes/front.routes.js b/routes/front.routes.js new file mode 100644 index 0000000..8e37a7c --- /dev/null +++ b/routes/front.routes.js @@ -0,0 +1,2 @@ +// filepath: c:\Users\amaizy\Desktop\cvgen\routes\front.routes.js +// This file is intentionally left blank. \ No newline at end of file