Type something to search...
Testing GitHub OAuth Authentication Locally in Astro Build with Lucia and ngrok

Testing GitHub OAuth Authentication Locally in Astro Build with Lucia and ngrok

Setting Up Lucia for Astro Build: Testing GitHub Authentication Locally Using ngrok

Introduction

In this article, we will walk through the steps to set up a secure authentication system with Lucia and an Astro.build project. We’ll focus on integrating GitHub OAuth authentication and demonstrate how to test it locally using ngrok.

Prerequisites

  • Astro Build Project: Ensure your Astro build project is set up.
  • Lucia Authentication Library: Installed in your project.
  • GitHub OAuth App: Created with the necessary credentials: Client ID and Client Secret you can get here.

Step-by-Step Guide

1. Setting Up Lucia Configuration

First, let’s create a basic configuration for Lucia in auth.ts.

import { Lucia } from "lucia";
import { BetterSqlite3Adapter } from "@lucia-auth/adapter-sqlite";
import { db } from "./db";
import type { DatabaseUser } from "./db";

const adapter = new BetterSqlite3Adapter(db, {
 user: "user",
 session: "session"
});

export const lucia = new Lucia(adapter, {
 sessionCookie: {
  attributes: {
   secure: import.meta.env.PROD
  }
 },
 getUserAttributes: (attributes) => {
  return {
   username: attributes.username,
   githubId: attributes.github_id
  };
 }
});

declare module "lucia" {
 interface Register {
  Lucia: typeof lucia;
  DatabaseUserAttributes: Omit<DatabaseUser, "id">;
 }
}

export const github = new GitHub(
 import.meta.env.GITHUB_CLIENT_ID,
 import.meta.env.GITHUB_CLIENT_SECRET
);

2. Setting Up Environment Variables

Create a .env file to store your environment-specific variables.

# For production
GITHUB_CLIENT_ID=your_production_client_id
GITHUB_CLIENT_SECRET=your_production_client_secret

# For development
VITE_GITHUB_CLIENT_ID=your_development_client_id
VITE_GITHUB_CLIENT_SECRET=your_development_client_secret

3. Setting Up Nginx (Optional)

If you’re using Nginx as a reverse proxy, configure it to handle the OAuth callback.

server {
    listen 80;
    server_name localhost;

    location /auth/github/callback {
        proxy_pass http://localhost:3000;  # Assuming your Astro app runs on port 3000
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

4. Testing Locally Using ngrok

To test your GitHub OAuth setup locally, you can use ngrok to expose your localhost to the internet.

  1. Install ngrok:

    • Download and install the appropriate version for Windows: Download ngrok
  2. Add Authtoken: Run the following command to add your authtoken to the default ngrok.yml configuration file.

    ngrok config add-authtoken 2mYdlXwJyGxEn7x1uxVl1ZHiPJK_EqUgoP4eV5r8nhqZtucu
    
  3. Deploy Your App: Run the following command to expose your local server on port 4321.

    ngrok http http://localhost:4321
    
  4. Update GitHub OAuth Settings: Go to the GitHub OAuth app settings and update the Authorization callback URL with the ngrok-generated public URL.

  5. Test Locally:

    • Open your browser and initiate the OAuth login flow from GitHub.
    • The OAuth process should redirect to http://localhost:4321/auth/github/callback via ngrok.

Full Example of auth.ts

Here is the full example of how you can set up auth.ts for both local development and production environments:

import { Lucia } from "lucia";
import { BetterSqlite3Adapter } from "@lucia-auth/adapter-sqlite";
import { db } from "./db";
import { GitHub } from "arctic";
import type { DatabaseUser } from "./db";

const adapter = new BetterSqlite3Adapter(db, {
 user: "user",
 session: "session"
});

export const lucia = new Lucia(adapter, {
 sessionCookie: {
  attributes: {
   secure: import.meta.env.PROD
  }
 },
 getUserAttributes: (attributes) => {
  return {
   username: attributes.username,
   githubId: attributes.github_id
  };
 }
});

declare module "lucia" {
 interface Register {
  Lucia: typeof lucia;
  DatabaseUserAttributes: Omit<DatabaseUser, "id">;
 }
}

export const github = new GitHub(
 import.meta.env.GITHUB_CLIENT_ID,
 import.meta.env.GITHUB_CLIENT_SECRET,
 {
  redirectUri: import.meta.env.PROD
   ? "https://ctrlman.dev/auth/github/callback" // Production URL
   : "http://localhost:4321/auth/github/callback" // Localhost URL
 }
);

Conclusion

By following the steps outlined in this article, you should be able to set up a secure GitHub OAuth authentication system for your Astro build project and test it locally using ngrok. This setup ensures that your application is both flexible and robust, providing a seamless development experience while maintaining security.

If you encounter any issues or have further questions, feel free to reach out! Happy coding!

Related Posts

A Beginner's Guide to Web Development: How to Integrate Bootstrap with Visual Studio Code - Part 1

A Beginner's Guide to Web Development: How to Integrate Bootstrap with Visual Studio Code - Part 1

A Beginner's Guide to Integrate Bootstrap with Visual Studio Code Bootstrap is a popular open-source CSS framework used for developing responsive and mobile-first websites. This guide will walk you…

Read more...
A Beginner's Guide to Web Development: Understanding Bootstrap and Responsive Design - Part 2

A Beginner's Guide to Web Development: Understanding Bootstrap and Responsive Design - Part 2

A Beginner's Guide to Web Development: Understanding Bootstrap and Responsive Design Web development can be a challenging field for beginners. One common issue that beginners often encounter involves…

Read more...
A Beginner's Guide to Web Development: CSS and Bootstrap - Part 3

A Beginner's Guide to Web Development: CSS and Bootstrap - Part 3

A Beginner's Guide to Web Development: CSS and Bootstrap Welcome to the world of web development! This guide is designed to help beginners understand the basics of CSS and Bootstrap, complete with…

Read more...
A Beginner's Guide to Web Development: Advanced Layouts with Bootstrap 5 - Part 4

A Beginner's Guide to Web Development: Advanced Layouts with Bootstrap 5 - Part 4

Getting Started with Bootstrap 5: A Beginner's Guide Welcome to the exciting world of web development! This beginner-friendly guide will introduce you to Bootstrap 5, the latest version of the world's…

Read more...
Building Your First Web App: A Beginner's Guide to Creating a To-Do List with Node.js and Express

Building Your First Web App: A Beginner's Guide to Creating a To-Do List with Node.js and Express

Building Your First Web App: A Beginner's Guide to Creating a To-Do List with Node.js and Express Introduction Embarking on your web development journey can be both exciting and overwhelming. With…

Read more...
Creating a Dynamic Blog with Node.js, Express, and EJS: A Comprehensive Guide - Part 1

Creating a Dynamic Blog with Node.js, Express, and EJS: A Comprehensive Guide - Part 1

Creating a Dynamic Blog with Node.js, Express, and EJS: A Comprehensive Guide (Part 1) Introduction In the ever-evolving landscape of web development, it's crucial to choose tools that are versatile,…

Read more...
Creating a Dynamic Blog with Node.js, Express, and EJS: A Comprehensive Guide - Part 2

Creating a Dynamic Blog with Node.js, Express, and EJS: A Comprehensive Guide - Part 2

Creating a Dynamic Blog with Node.js, Express, and EJS: A Comprehensive Guide (Part 2) Introduction Welcome back to our two-part series on building a dynamic blog using Node.js, Express, and EJS. In…

Read more...
Event Prevention in Web Development: A Comprehensive Guide

Event Prevention in Web Development: A Comprehensive Guide

Event Prevention in Web Development: A Comprehensive Guide Introduction Event prevention is a crucial concept in web development that allows developers to control and customize user interactions. This…

Read more...
Exploring OCaml: A Functional Approach to Web Development

Exploring OCaml: A Functional Approach to Web Development

Exploring OCaml: A Functional Approach to Web Development Introduction: Unveiling the Power of Functional Programming in Web Development In the ever-evolving landscape of web development, where…

Read more...
Implementing Authentication with the Lucia Library: Backend vs. Frontend Approaches

Implementing Authentication with the Lucia Library: Backend vs. Frontend Approaches

Implementing Authentication with the Lucia Library: Backend vs. Frontend Approaches Authentication is a crucial aspect of modern web applications, ensuring that users are who they claim to be and…

Read more...
Integrating Google reCAPTCHA for Enhanced Website Security

Integrating Google reCAPTCHA for Enhanced Website Security

Integrating Google reCAPTCHA for Enhanced Website Security Introduction In an era where cyber threats are increasingly sophisticated, protecting your website from automated attacks is crucial.…

Read more...
Secure Authentication: Integrating Lucia with Astro for Robust User Management

Secure Authentication: Integrating Lucia with Astro for Robust User Management

Integrating Lucia Authentication with Astro To integrate the Lucia authentication system for login functionality in your Astro project, follow these steps. This guide will help you structure your…

Read more...
Mastering HTML: Tips & Tricks for Stylish Web Pages

Mastering HTML: Tips & Tricks for Stylish Web Pages

Mastering HTML: Tips & Tricks for Stylish Web Pages Introduction HTML is the backbone of web development, providing the structure that powers nearly every website you visit. Whether you're creating…

Read more...
JavaScript Fundamentals: The Foundation for React Development

JavaScript Fundamentals: The Foundation for React Development

JavaScript Fundamentals: The Foundation for React Development Introduction: Why Learn JavaScript Before React? As you embark on your journey to learning web development, it's crucial to understand the…

Read more...
Introduction to React: Building on Your JavaScript Knowledge

Introduction to React: Building on Your JavaScript Knowledge

Introduction to React: Building on Your JavaScript Knowledge Transitioning to React React is a powerful library developed by Facebook, primarily used for building user interfaces. It builds on…

Read more...
Advanced React Development and Best Practices

Advanced React Development and Best Practices

Advanced React Development and Best Practices Advanced React Topics Refs and the useRef Hook Refs allow you to interact with the DOM directly from functional components: Example: import React, {…

Read more...
Mastering useCallback in React: Optimizing Function Management

Mastering useCallback in React: Optimizing Function Management

Mastering useCallback in React: A Beginner's Guide to Optimizing Function Management Introduction In the dynamic world of React development, performance optimization is key to creating smooth,…

Read more...
From Words to Web: Kickstart Your MERN + ANAi Stack Journey for Translators and Writers – Prerequisites

From Words to Web: Kickstart Your MERN + ANAi Stack Journey for Translators and Writers – Prerequisites

MERN + ANAi Stack Mastery: Prerequisites for AI-Enhanced Web Development Introduction Welcome to the MERN + ANAi Stack Mastery course, an intensive 10-weekends journey designed to elevate your web…

Read more...
MERN + ANAi Stack Mastery: Your Journey to AI-Driven Web Development – Overview

MERN + ANAi Stack Mastery: Your Journey to AI-Driven Web Development – Overview

Transitioning to AI-Driven Web Development: MERN Stack Journey Enhanced by ANAi Module Overview This 10-weekends comprehensive course equips you with the skills to build AI-enhanced web applications…

Read more...
The Necessity of Keeping Documentation Soup Repository Locally and Updated

The Necessity of Keeping Documentation Soup Repository Locally and Updated

Title: The Necessity of Keeping Documentation Soup Repository Locally and Updated Introduction In today's fast-paced technological landscape, developers rely on a vast array of libraries and…

Read more...
Node.js for Newbies: Mastering the Fundamentals

Node.js for Newbies: Mastering the Fundamentals

Node.js for Newbies: Mastering the Fundamentals Introduction Node.js is an influential runtime environment that leverages Chrome's V8 JavaScript engine. It empowers developers to craft server-side…

Read more...
OOP Concepts: Interview Questions and Answers for Junior Web Developers

OOP Concepts: Interview Questions and Answers for Junior Web Developers

OOP Concepts Answer Sheet for Junior Web Developers OOP Concepts: Interview Questions and Answers for Junior Web Developers 1. Encapsulation Q: What is encapsulation, and why is it important? A:…

Read more...
Securing Next.js API Endpoints: A Comprehensive Guide to Email Handling and Security Best Practices

Securing Next.js API Endpoints: A Comprehensive Guide to Email Handling and Security Best Practices

Securing Next.js API Endpoints: A Comprehensive Guide to Email Handling and Security Best Practices Introduction In the fast-paced world of web development, rapid code deployment is often necessary.…

Read more...
Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers

Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers

Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers Introduction Playing basketball might seem like an unlikely activity for web developers, but this fast-paced…

Read more...
A Comprehensive Guide to Troubleshooting Your Simple BMI Calculator

A Comprehensive Guide to Troubleshooting Your Simple BMI Calculator

A Comprehensive Guide to Troubleshooting Your Simple BMI Calculator Introduction Building a web application can be a complex endeavor, and ensuring smooth functionality is crucial. In this guide,…

Read more...
Understanding OOP Concepts: A Guide for Junior Web Developers

Understanding OOP Concepts: A Guide for Junior Web Developers

Understanding OOP Concepts: A Guide for Junior Web Developers As a junior web developer, one of the most crucial skills you need to develop is a strong understanding of Object-Oriented Programming…

Read more...
Understanding Server-Side Rendering (SSR) and Its SEO Benefits

Understanding Server-Side Rendering (SSR) and Its SEO Benefits

Understanding SSR and Its SEO Benefits Server-Side Rendering (SSR) involves rendering web pages on the server instead of the client's browser. This means that when a user (or a search engine bot)…

Read more...
Web Development Mastery: A Comprehensive Guide for Beginners

Web Development Mastery: A Comprehensive Guide for Beginners

Web Development Mastery: A Comprehensive Guide for Beginners Unlocking the World of Web Creation Welcome to the exciting realm of web development! Whether you're a coding novice or an experienced…

Read more...
Web Development for Beginners: A Comprehensive Guide Using Rust

Web Development for Beginners: A Comprehensive Guide Using Rust

Web Development for Beginners: A Comprehensive Guide Using Rust Introduction Web development is an exciting field filled with opportunities to create dynamic and engaging user experiences. Rust, a…

Read more...