Overcoming Backend Challenges: My Journey and Aspirations with HNG Internship
Tags: backenddevelopment, hng, node, softwaredevelopment
Published: 2024-07-02 22:03:50



**Introduction**

Hello, I'm Kingsley, and I'm embarking on an exciting journey with the HNG Internship. As a budding backend developer, I recently encountered a particularly challenging problem that tested my skills and perseverance. In this blog post, I will walk you through the problem I faced, how I tackled it, and why I'm thrilled to be a part of the HNG Internship.

 The Problem

The problem emerged while working on a user authentication system for a web application. The application needed to securely register users, handle login requests, and manage sessions. The complexity was compounded by the requirement to integrate social media login options and ensure all data transfers were encrypted.

 Step-by-Step Solution

1. **Understanding the Requirements**:
   I started by outlining the key requirements:
   - Secure user registration and login.
   - Session management.
   - Social media login integration.
   - Data encryption.

2. **Choosing the Right Tools**:
   Based on the requirements, I chose:
   - **Node.js and Express.js** for the backend framework.
   - **Passport.js** for authentication and social media login integration.
   - **bcrypt** for password hashing.
   - **JWT (JSON Web Tokens)** for session management.
   - **HTTPS** for secure data transfer.

3. **Setting Up the Project**:
   I initialized a new Node.js project and installed the necessary dependencies:
   ```bash
   npm init -y
   npm install express passport bcrypt jsonwebtoken dotenv
   ```

4. **Implementing User Registration**:
   I created an endpoint for user registration that hashes the password using bcrypt before storing it in the database:
   ```javascript
   const bcrypt = require('bcrypt');
   const saltRounds = 10;

   app.post('/register', async (req, res) => {
       const { username, password } = req.body;
       try {
           const hashedPassword = await bcrypt.hash(password, saltRounds);
           // Store user with hashed password in the database
           res.status(201).send('User registered successfully');
       } catch (error) {
           res.status(500).send('Error registering user');
       }
   });
   ```

5. **Implementing Login and JWT Authentication**:
   I set up the login endpoint to validate the user and issue a JWT for session management:
   ```javascript
   const jwt = require('jsonwebtoken');
   const secretKey = 'your_secret_key';

   app.post('/login', async (req, res) => {
       const { username, password } = req.body;
       // Retrieve user from the database
       const user = {}; // assume this is the retrieved user
       const match = await bcrypt.compare(password, user.hashedPassword);
       if (match) {
           const token = jwt.sign({ username }, secretKey, { expiresIn: '1h' });
           res.json({ token });
       } else {
           res.status(401).send('Invalid credentials');
       }
   });
   ```

6. **Integrating Social Media Login**:
   I used Passport.js to integrate social media login options like Google and Facebook:
   ```javascript
   const passport = require('passport');
   const GoogleStrategy = require('passport-google-oauth20').Strategy;

   passport.use(new GoogleStrategy({
       clientID: 'your_client_id',
       clientSecret: 'your_client_secret',
       callbackURL: '/auth/google/callback'
   }, (token, tokenSecret, profile, done) => {
       // Find or create user in the database
       done(null, profile);
   }));

   app.get('/auth/google', passport.authenticate('google', { scope: ['profile'] }));
   app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/' }), (req, res) => {
       res.redirect('/');
   });
   ```

7. **Ensuring Secure Data Transfer**:
   Finally, I set up HTTPS to encrypt data transfer:
   ```javascript
   const https = require('https');
   const fs = require('fs');

   const options = {
       key: fs.readFileSync('key.pem'),
       cert: fs.readFileSync('cert.pem')
   };

   https.createServer(options, app).listen(port, () => {
       console.log(`Secure server running at https://localhost:${port}`);
   });
   ```

#### Why HNG Internship?

I am excited to be a part of the HNG Internship because it provides a unique opportunity to learn from industry experts, collaborate with fellow developers, and work on real-world projects. The internship offers a structured program that helps me improve my skills and gain valuable experience in backend development.

#### Conclusion

Solving this backend challenge was a significant milestone in my development journey. It reinforced the importance of understanding requirements, choosing the right tools, and implementing secure practices. As I continue to learn and grow with the HNG Internship, I look forward to tackling more complex problems and contributing to innovative projects.

Learn more about the HNG Internship and its benefits [here](https://hng.tech/internship) and [here](https://hng.tech/premium). If you're interested in hiring talented developers from the program, check out [this link](https://hng.tech/hire).