Adding routes & linkedin auth

This commit is contained in:
AMAICDAX 2025-09-03 10:37:15 +02:00
parent a16843bea0
commit 797c40b9a0
6 changed files with 158 additions and 0 deletions

38
README.md Normal file
View file

@ -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.

20
app.js Normal file
View file

@ -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}`);
});

66
controllers/linkedin.js Normal file
View file

@ -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,
};

16
package.json Normal file
View file

@ -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"
}

16
routes/api.routes.js Normal file
View file

@ -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;

2
routes/front.routes.js Normal file
View file

@ -0,0 +1,2 @@
// filepath: c:\Users\amaizy\Desktop\cvgen\routes\front.routes.js
// This file is intentionally left blank.