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 🙏

© 2026 – Pkg Stats / Ryan Hefner

better-auth-social

v1.0.0

Published

A social network plugin for Better Auth with friends, friend requests, chat, and group chat features.

Readme

Better Auth Social Network Plugin

Un plugin Better Auth pour les réseaux sociaux qui permet la gestion des amis, des demandes d'amis, des chats privés et des groupes de chat.

Installation

npm install better-auth-social

Fonctionnalités

  • Gestion des amis : Envoyer, accepter, refuser et supprimer des amis
  • Demandes d'amis : Système complet de demandes d'amis avec statuts
  • Chats privés : Conversations en tête-à-tête entre amis
  • Groupes de chat : Créer et gérer des groupes de discussion avec plusieurs membres
  • Messages : Envoyer et recevoir des messages dans les chats et groupes

Configuration

Schéma de base de données

Le plugin ajoute automatiquement les tables suivantes à votre schéma Better Auth :

  • friend_request - Demandes d'amis
  • friend - Relations d'amitié
  • chat - Chats privés entre deux utilisateurs
  • group_chat - Groupes de chat
  • group_chat_member - Membres des groupes
  • message - Messages dans les chats et groupes

Setup

import { socialNetwork } from "better-auth-social";
import { betterAuth } from "better-auth";

const auth = betterAuth({
  // ... votre configuration
  plugins: [
    socialNetwork({
      allowSelfFriendRequest: false, // Par défaut: false
      maxGroupSize: 50, // Taille maximale des groupes (optionnel)
      // ou une fonction async
      maxGroupSize: async () => {
        // Logique dynamique pour déterminer la taille maximale
        return 100;
      },
      hooks: {
        onFriendRequestSend: async (data) => {
          console.log("Friend request sent:", data);
        },
        onFriendRequestAccept: async (data) => {
          console.log("Friend request accepted:", data);
        },
        onGroupChatJoin: async (data) => {
          console.log("User joined group:", data);
        },
        // ... autres hooks
      },
    }),
  ],
});

Options

allowSelfFriendRequest

  • Type: boolean
  • Défaut: false
  • Description: Permet aux utilisateurs d'envoyer des demandes d'amis à eux-mêmes.

maxGroupSize

  • Type: number | (() => Promise<number>)
  • Défaut: undefined (pas de limite)
  • Description: Taille maximale des membres dans un groupe de chat. Peut être un nombre strict ou une fonction async qui retourne un nombre.

hooks

  • Type: SocialNetworkHooks
  • Description: Hooks pour intercepter les événements du réseau social.

Hooks disponibles

Tous les hooks sont optionnels et peuvent être des fonctions async ou synchrones :

  • onFriendRequestSend : Appelé lorsqu'une demande d'ami est envoyée

    onFriendRequestSend?: (data: { senderId: string; receiverId: string; requestId: string }) => Promise<void> | void;
  • onFriendRequestAccept : Appelé lorsqu'une demande d'ami est acceptée

    onFriendRequestAccept?: (data: { senderId: string; receiverId: string; requestId: string }) => Promise<void> | void;
  • onFriendRequestReject : Appelé lorsqu'une demande d'ami est refusée

    onFriendRequestReject?: (data: { senderId: string; receiverId: string; requestId: string }) => Promise<void> | void;
  • onFriendRemove : Appelé lorsqu'un ami est supprimé

    onFriendRemove?: (data: { userId: string; friendId: string }) => Promise<void> | void;
  • onChatCreate : Appelé lorsqu'un chat privé est créé

    onChatCreate?: (data: { chatId: string; user1Id: string; user2Id: string }) => Promise<void> | void;
  • onChatMessageSend : Appelé lorsqu'un message est envoyé dans un chat privé

    onChatMessageSend?: (data: { messageId: string; chatId: string; senderId: string; content: string }) => Promise<void> | void;
  • onGroupChatCreate : Appelé lorsqu'un groupe de chat est créé

    onGroupChatCreate?: (data: { groupChatId: string; createdById: string; name: string }) => Promise<void> | void;
  • onGroupChatJoin : Appelé lorsqu'un utilisateur rejoint un groupe

    onGroupChatJoin?: (data: { groupChatId: string; userId: string; addedBy?: string }) => Promise<void> | void;
  • onGroupChatLeave : Appelé lorsqu'un utilisateur quitte ou est retiré d'un groupe

    onGroupChatLeave?: (data: { groupChatId: string; userId: string; removedBy?: string }) => Promise<void> | void;
  • onGroupChatMessageSend : Appelé lorsqu'un message est envoyé dans un groupe

    onGroupChatMessageSend?: (data: { messageId: string; groupChatId: string; senderId: string; content: string }) => Promise<void> | void;

API Routes

Demandes d'amis

Envoyer une demande d'ami

POST /api/auth/social/friend-request/send
Body: { receiverId: string }

Accepter une demande d'ami

POST /api/auth/social/friend-request/accept
Body: { requestId: string }

Refuser une demande d'ami

POST /api/auth/social/friend-request/reject
Body: { requestId: string }

Lister les demandes d'amis

GET /api/auth/social/friend-request/list
Response: { sent: FriendRequest[], received: FriendRequest[] }

Amis

Lister les amis

GET /api/auth/social/friends/list
Response: { friends: Friend[] }

Supprimer un ami

POST /api/auth/social/friends/remove
Body: { friendId: string }

Chats privés

Créer un chat

POST /api/auth/social/chat/create
Body: { friendId: string }
Response: { chat: Chat }

Lister les chats

GET /api/auth/social/chat/list
Response: { chats: Chat[] }

Obtenir les messages d'un chat

GET /api/auth/social/chat/messages?chatId=xxx
Response: { messages: Message[] }

Envoyer un message dans un chat

POST /api/auth/social/chat/send-message
Body: { chatId: string, content: string }
Response: { message: Message }

Groupes de chat

Créer un groupe

POST /api/auth/social/group-chat/create
Body: { 
  name: string, 
  description?: string, 
  memberIds?: string[] 
}
Response: { groupChat: GroupChat }

Lister les groupes

GET /api/auth/social/group-chat/list
Response: { groupChats: GroupChat[] }

Ajouter un membre

POST /api/auth/social/group-chat/add-member
Body: { groupChatId: string, userId: string }

Retirer un membre

POST /api/auth/social/group-chat/remove-member
Body: { groupChatId: string, userId: string }

Obtenir les messages d'un groupe

GET /api/auth/social/group-chat/messages?groupChatId=xxx
Response: { messages: Message[] }

Envoyer un message dans un groupe

POST /api/auth/social/group-chat/send-message
Body: { groupChatId: string, content: string }
Response: { message: Message }

Types TypeScript

export interface FriendRequest {
  id: string;
  senderId: string;
  receiverId: string;
  status: 'pending' | 'accepted' | 'rejected';
  createdAt: Date;
  updatedAt: Date;
}

export interface Friend {
  id: string;
  userId: string;
  friendId: string;
  createdAt: Date;
}

export interface Chat {
  id: string;
  user1Id: string;
  user2Id: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface GroupChat {
  id: string;
  name: string;
  description: string | null;
  createdById: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface Message {
  id: string;
  content: string;
  senderId: string;
  chatId: string | null;
  groupChatId: string | null;
  createdAt: Date;
  updatedAt: Date;
}

Exemple d'utilisation

import { socialNetwork } from "better-auth-social";
import { betterAuth } from "better-auth";

const auth = betterAuth({
  database: {
    // votre adaptateur de base de données
  },
  plugins: [
    socialNetwork({
      allowSelfFriendRequest: false,
    }),
  ],
});

// Utilisation côté client
import { socialNetworkClient } from "better-auth-social/client";
import { createAuthClient } from "better-auth/client";

const client = createAuthClient({
  baseURL: "http://localhost:3000",
  plugins: [socialNetworkClient()],
});

// Envoyer une demande d'ami
await client.fetch("/social/friend-request/send", {
  method: "POST",
  body: { receiverId: "user-id" },
});

// Accepter une demande
await client.fetch("/social/friend-request/accept", {
  method: "POST",
  body: { requestId: "request-id" },
});

// Créer un chat
const { chat } = await client.fetch("/social/chat/create", {
  method: "POST",
  body: { friendId: "friend-id" },
});

// Envoyer un message
await client.fetch("/social/chat/send-message", {
  method: "POST",
  body: { chatId: chat.id, content: "Hello!" },
});

Licence

MIT