Building a Cross-Platform Authentication Service using Node.js and Express

Building a Cross-Platform Authentication Service using Node.js and Express

Hey there, fellow developers! Today, we’re going to dive into the exciting world of authentication services. Specifically, we’ll be exploring how to build a robust, cross-platform authentication service using Node.js and Express. This isn’t just any old tutorial – we’re going to roll up our sleeves and get our hands dirty with some real-world, practical code. So, grab your favorite beverage, fire up your code editor, and let’s get started on this adventure!

Why Build Your Own Authentication Service?

Before we jump into the nitty-gritty, let’s talk about why you might want to build your own authentication service in the first place. After all, there are plenty of third-party solutions out there, right? Well, yes, but hear me out.

Building your own authentication service gives you complete control over your users’ data and the authentication process. You’re not relying on a third-party service that could change its terms, pricing, or worse, shut down unexpectedly. Plus, you get to tailor the service to your specific needs, integrating it seamlessly with your existing systems. And let’s be honest – there’s a certain satisfaction in creating something from scratch, isn’t there?

But don’t worry if this sounds daunting. We’re going to break it down step by step, and by the end of this post, you’ll have a solid foundation for a custom authentication service that you can extend and modify to your heart’s content.

Setting Up Our Project

Alright, let’s get our hands dirty! First things first, we need to set up our project. We’ll be using Node.js and Express, so make sure you have Node.js installed on your machine. If you don’t, head over to the official Node.js website and download the appropriate version for your operating system.

Once you’ve got Node.js installed, open up your terminal and let’s create a new project:

mkdir cross-platform-auth-service
cd cross-platform-auth-service
npm init -y

This creates a new directory for our project, moves us into that directory, and initializes a new Node.js project with default settings.

Next, let’s install the packages we’ll need:

npm install express bcrypt jsonwebtoken dotenv mongoose

Here’s a quick rundown of what each of these packages does:

  • express: Our web application framework
  • bcrypt: For hashing passwords
  • jsonwebtoken: For creating and verifying JWTs (JSON Web Tokens)
  • dotenv: For managing environment variables
  • mongoose: For interacting with our MongoDB database

Great! Now let’s create our main application file:

touch app.js

Open up app.js in your favorite code editor and let’s start building our authentication service!

Setting Up Express and Basic Routes

Let’s start by setting up our Express application and defining some basic routes. Here’s what our app.js file should look like to start:

const express = require('express');
const dotenv = require('dotenv');

dotenv.config();

const app = express();
const port = process.env.PORT || 3000;

app.use(express.json());

app.get('/', (req, res) => {
  res.json({ message: 'Welcome to our authentication service!' });
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

This sets up a basic Express application that listens on the port specified in our environment variables (or 3000 if not specified). We’ve also added a simple route that responds to GET requests to the root path.

Connecting to MongoDB

Now that we have our basic Express setup, let’s connect to our MongoDB database. We’ll use Mongoose for this. First, make sure you have MongoDB installed and running on your machine. If you don’t, you can download it from the official MongoDB website.

Add the following code to your app.js file, just after the dotenv.config() line:

const mongoose = require('mongoose');

mongoose.connect(process.env.MONGODB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Could not connect to MongoDB', err));

Make sure to create a .env file in your project root and add your MongoDB connection string:

MONGODB_URI=mongodb://localhost:27017/authservice

Creating Our User Model

Now that we’re connected to our database, let’s create a User model. Create a new file called models/User.js:

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');

const userSchema = new mongoose.Schema({
  username: { type: String, required: true, unique: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  createdAt: { type: Date, default: Date.now },
});

userSchema.pre('save', async function(next) {
  if (!this.isModified('password')) return next();

  try {
    const salt = await bcrypt.genSalt(10);
    this.password = await bcrypt.hash(this.password, salt);
    next();
  } catch (error) {
    next(error);
  }
});

userSchema.methods.comparePassword = async function(candidatePassword) {
  return bcrypt.compare(candidatePassword, this.password);
};

module.exports = mongoose.model('User', userSchema);

This creates a User model with username, email, and password fields. We’ve also added a pre-save hook that automatically hashes the password before saving it to the database, and a method to compare passwords during login.

Implementing User Registration

Now that we have our User model, let’s implement user registration. Create a new file called routes/auth.js:

const express = require('express');
const jwt = require('jsonwebtoken');
const User = require('../models/User');

const router = express.Router();

router.post('/register', async (req, res) => {
  try {
    const { username, email, password } = req.body;

    // Check if user already exists
    const existingUser = await User.findOne({ $or: [{ username }, { email }] });
    if (existingUser) {
      return res.status(400).json({ message: 'Username or email already exists' });
    }

    // Create new user
    const user = new User({ username, email, password });
    await user.save();

    res.status(201).json({ message: 'User registered successfully' });
  } catch (error) {
    res.status(500).json({ message: 'Error registering user', error: error.message });
  }
});

module.exports = router;

Now, let’s add this route to our app.js:

const authRoutes = require('./routes/auth');
app.use('/auth', authRoutes);

Implementing User Login

Next, let’s implement user login. Add the following route to routes/auth.js:

router.post('/login', async (req, res) => {
  try {
    const { username, password } = req.body;

    // Find user
    const user = await User.findOne({ username });
    if (!user) {
      return res.status(400).json({ message: 'Invalid username or password' });
    }

    // Check password
    const isMatch = await user.comparePassword(password);
    if (!isMatch) {
      return res.status(400).json({ message: 'Invalid username or password' });
    }

    // Generate JWT
    const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: '1h' });

    res.json({ token });
  } catch (error) {
    res.status(500).json({ message: 'Error logging in', error: error.message });
  }
});

Don’t forget to add your JWT secret to your .env file:

JWT_SECRET=your_jwt_secret_here

Protecting Routes with Middleware

Now that we have user registration and login implemented, let’s create a middleware to protect certain routes. Create a new file called middleware/auth.js:

const jwt = require('jsonwebtoken');

module.exports = (req, res, next) => {
  const token = req.header('x-auth-token');

  if (!token) {
    return res.status(401).json({ message: 'No token, authorization denied' });
  }

  try {
    const decoded = jwt.verify(token, process.env.JWT_SECRET);
    req.user = decoded.id;
    next();
  } catch (error) {
    res.status(401).json({ message: 'Token is not valid' });
  }
};

Now we can use this middleware to protect routes. Let’s create a protected route in app.js:

const auth = require('./middleware/auth');

app.get('/protected', auth, (req, res) => {
  res.json({ message: 'This is a protected route', userId: req.user });
});

Implementing Password Reset

A crucial feature of any authentication service is the ability to reset passwords. Let’s implement this functionality. First, we’ll need to install the nodemailer package to send emails:

npm install nodemailer

Now, let’s add a resetToken field to our User model in models/User.js:

const userSchema = new mongoose.Schema({
  // ... existing fields
  resetToken: String,
  resetTokenExpiration: Date,
});

Next, let’s add routes for requesting a password reset and actually resetting the password. Add these to routes/auth.js:

const crypto = require('crypto');
const nodemailer = require('nodemailer');

// Request password reset
router.post('/forgot-password', async (req, res) => {
  try {
    const { email } = req.body;
    const user = await User.findOne({ email });

    if (!user) {
      return res.status(404).json({ message: 'User not found' });
    }

    const token = crypto.randomBytes(20).toString('hex');
    user.resetToken = token;
    user.resetTokenExpiration = Date.now() + 3600000; // 1 hour
    await user.save();

    const transporter = nodemailer.createTransport({
      // Configure your email service here
    });

    const mailOptions = {
      to: user.email,
      from: 'youremail@example.com',
      subject: 'Password Reset',
      text: `You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n
        Please click on the following link, or paste this into your browser to complete the process:\n\n
        http://${req.headers.host}/reset/${token}\n\n
        If you did not request this, please ignore this email and your password will remain unchanged.\n`,
    };

    await transporter.sendMail(mailOptions);

    res.json({ message: 'An email has been sent to ' + user.email + ' with further instructions.' });
  } catch (error) {
    res.status(500).json({ message: 'Error requesting password reset', error: error.message });
  }
});

// Reset password
router.post('/reset-password/:token', async (req, res) => {
  try {
    const { token } = req.params;
    const { password } = req.body;

    const user = await User.findOne({
      resetToken: token,
      resetTokenExpiration: { $gt: Date.now() },
    });

    if (!user) {
      return res.status(400).json({ message: 'Password reset token is invalid or has expired' });
    }

    user.password = password;
    user.resetToken = undefined;
    user.resetTokenExpiration = undefined;
    await user.save();

    res.json({ message: 'Password has been reset' });
  } catch (error) {
    res.status(500).json({ message: 'Error resetting password', error: error.message });
  }
});

Cross-Platform Considerations

Now that we have our basic authentication service up and running, let’s discuss some cross-platform considerations. Our service is already pretty flexible – it’s a RESTful API that can be consumed by any client that can make HTTP requests. However, there are a few things we can do to make it even more cross-platform friendly:

  1. CORS (Cross-Origin Resource Sharing): If our authentication service will be used by web applications hosted on different domains, we need to enable CORS. Let’s install the cors package:
npm install cors

And add it to our app.js:

const cors = require('cors');

app.use(cors());
  1. API Versioning: As our service evolves, we might need to make breaking changes. By versioning our API, we can ensure that existing clients continue to work while we develop new features. Let’s update our routes in app.js:
app.use('/api/v1/auth', authRoutes);
  1. Rate Limiting: To protect our service from abuse, we should implement rate limiting. The express-rate-limit package is great for this:
npm install express-rate-limit

Add it to app.js:

const rateLimit = require('express-rate-limit');

const limiter = rateLimit({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100 // limit each IP to 100 requests per windowMs
});

app.use(limiter);
  1. Secure Headers: Let’s add some security headers to our responses. We’ll use the helmet package for this:
npm install helmet

Add it to app.js:

const helmet = require('helmet');

app.use(helmet());

Testing Our Authentication Service

Now that we’ve built our authentication service, it’s crucial to test it thoroughly. Here’s a table summarizing the key endpoints we should test:

EndpointMethodDescriptionExpected Response
/api/v1/auth/registerPOSTRegister a new user201 Created
/api/v1/auth/loginPOSTLog in a user200 OK with JWT
/api/v1/auth/forgot-passwordPOSTRequest password reset200 OK
/api/v1/auth/reset-password/:tokenPOSTReset password200 OK
/protectedGETAccess protected route200 OK if authenticated

You can use tools like Postman or write automated tests using libraries like Jest to thoroughly test these endpoints.

Conclusion

Wow, we’ve covered a lot of ground! We’ve built a robust, cross-platform authentication service using Node.js and Express. We’ve implemented user registration, login, password reset, and protected routes. We’ve also considered cross-platform issues like CORS, API versioning, rate limiting, and security headers.

Remember, authentication is a critical component of any application. While this service provides a solid foundation, there’s always room for improvement. Consider adding features like:

  • Two-factor authentication
  • OAuth integration for social login
  • Account lockout after failed login attempts
  • Email verification for new accounts

Building your own authentication service gives you full control over your users’ data and the authentication process. It allows you to tailor the service to your specific needs and integrate it seamlessly with your existing systems.

I hope you’ve enjoyed this journey as much as I have. Happy coding, and may your tokens always be valid!

Disclaimer: This blog post is for educational purposes only. While we’ve covered many important aspects of building an authentication service, security is a complex and ever-evolving field. Always stay updated with the latest security best practices and consider consulting with security experts before deploying authentication systems in production environments. If you notice any inaccuracies in this post, please report them so we can correct them promptly.

Leave a Reply

Your email address will not be published. Required fields are marked *


Translate »