npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-kinde

v2.4.1

Published

Astro integration module for Kinde authentication

Downloads

56

Readme

Astro Kinde Integration

Build Status

This package provides an Astro integration for Kinde, simplifying the process of adding authentication to your Astro application using Kinde's OAuth 2.0 flow.

Features

  • Easy setup and configuration
  • Automatically injects authentication routes
  • Provides authentication state in Astro pages
  • Integrates with Kinde Management SDK for user and application management

Installation

  1. Create a Kinde back-end web account and set the following environment variables:
KINDE_MANAGEMENT_CLIENT_ID=your_management_client_id
KINDE_MANAGEMENT_CLIENT_SECRET=your_management_client_secret
KINDE_DOMAIN=your_kinde_domain
  1. Install the package and its dependencies:
npm install astro-kinde dotenv

Note: The dotenv module is required for loading environment variables.

  1. Add the integration to your astro.config.mjs:
import { defineConfig } from "astro/config";
import kinde from "astro-kinde";

export default defineConfig({
    integrations: [kinde()],
});
  1. Setup your env.d.ts:
/// <reference types="astro/client" />

declare namespace App {
    interface Locals {
        isAuthenticated: boolean;
        accessToken: string | undefined;
    }
}

Configuration

Configure the integration by passing options to the kinde function in astro.config.mjs.

import { defineConfig } from "astro/config";
import kinde from "astro-kinde";
import dotenv from "dotenv";

dotenv.config();

export default defineConfig({
    output: "server", // Necessary for Astro headers
    integrations: [
        kinde({
            clientId: process.env.KINDE_MANAGEMENT_CLIENT_ID,
            clientSecret: process.env.KINDE_MANAGEMENT_CLIENT_SECRET,
            domain: process.env.KINDE_DOMAIN,
            callbackUri: "http://localhost:4321/api/kinde/callback",
            signedInUri: "http://localhost:4321",
            signedOutUri: "http://localhost:4321",
            sessionMaxAge: 3600,
        }),
    ],
});

You can set the uris based on your NODE_ENV to handle development and production environments.

Usage

The integration automatically injects the following routes:

  • /api/kinde/login: Redirects to the Kinde login page
  • /api/kinde/register: Redirects to the Kinde registration page
  • /api/kinde/callback: Handles the OAuth callback
  • /api/kinde/signout: Handles user sign-out
  • /api/kinde/isAuthenticated: Checks if the user is authenticated

Checking Authentication Status

You can check the authentication status in two ways:

  1. Using Astro.locals:
---
const isAuthenticated = Astro.locals.isAuthenticated;
---
  1. Using the isAuthenticated route:
---
const isAuthenticated = await fetch('/api/kinde/isAuthenticated').then(res => res.json());
---

Redirecting Authenticated Users

---
if (isAuthenticated) {
return Astro.redirect('/example');
}
---

Conditional Rendering Based on Authentication

{isAuthenticated ? (
    <a href="/api/kinde/signout">Sign Out</a>
) : (
    <a href="/api/kinde/login">Login</a>
)}

Registration with Pre-filled Email

<a href="/api/kinde/[email protected]">Register</a>

You can pass through query parameters to the login/register routes to control the behaviour of the Kinde flow. See what they are on the Kinde site. For example:

Management SDK

Kinde provides a Management SDK that you can use to manage your users and applications. To use the Management SDK:

  1. If using separately - remember to store the env variables from the previous steps.

  2. Install the SDK:

npm install @kinde-oss/kinde-management-api-js
  1. Initialise the SDK in astro.config.mts:
import { init } from "@kinde/management-api-js";

// Run the Kinde initialisation
init();
  1. Use the SDK in your Astro pages:
---
import { Oauth, OpenAPI } from "@kinde/management-api-js";

const isAuthenticated = Astro.locals.isAuthenticated;
const accessToken = Astro.locals.accessToken;

if (isAuthenticated) {
  OpenAPI.TOKEN = accessToken;
  const userProfile = await Oauth.getUserProfileV2();
  console.log(userProfile);
}
---

That's it! You now have Kinde authentication set up in your Astro project and can use the Management SDK to manage your users and applications.

Contributing

If you'd like to contribute to this project, please follow these steps:

  1. Fork the repository.
  2. Create a new branch.
  3. Make your changes.
  4. Submit a pull request.

License

By contributing to Astro-Kinde, you agree that your contributions will be licensed under its MIT License.