A clean, modern Last.fm authentication plugin for BetterAuth. https://www.npmjs.com/package/@ley0x/better-auth-lastfm
Find a file
2025-08-15 17:28:05 +02:00
.github/workflows Add GitHub Packages workflow 2025-08-12 22:57:55 +02:00
src fix : error with null dates in database schema 2025-08-15 17:27:51 +02:00
.gitignore feat : better auth plugin for lastfm 2025-08-12 21:58:05 +02:00
CLAUDE.md feat : better auth plugin for lastfm 2025-08-12 21:58:05 +02:00
eslint.config.js fix : linting configuration and basic stuffs 2025-08-12 22:43:24 +02:00
LICENSE refactor + updating endpoint in client + new LICENSE file 2025-08-15 14:02:58 +02:00
package.json 1.1.6 2025-08-15 17:28:05 +02:00
pnpm-lock.yaml refactor + updating endpoint in client + new LICENSE file 2025-08-15 14:02:58 +02:00
pnpm-workspace.yaml fix : linting configuration and basic stuffs 2025-08-12 22:43:24 +02:00
README.md chore : updating readme and package.json since i deleted the react hook from my package 2025-08-12 23:05:53 +02:00
tsconfig.json feat : better auth plugin for lastfm 2025-08-12 21:58:05 +02:00
tsup.config.ts updating entrypoint 2025-08-15 17:15:14 +02:00

BetterAuth Last.fm Plugin

A clean, modern Last.fm authentication plugin for BetterAuth.

Features

  • 🔐 Complete Last.fm authentication flow implementation
  • 🎵 Session key storage for Last.fm API calls
  • 🌟 TypeScript support with full type safety
  • 🛠️ Modern architecture following BetterAuth patterns
  • 🚀 Zero additional dependencies (except peer dependencies)

Installation

npm install @ley0x/better-auth-lastfm
# or
pnpm add @ley0x/better-auth-lastfm
# or
yarn add @ley0x/better-auth-lastfm

Setup

1. Get Last.fm API Credentials

  1. Visit Last.fm API Account Creation
  2. Create an application to get your API Key and Shared Secret

2. Server Configuration

import { betterAuth } from 'better-auth'
import { lastfmPlugin } from '@ley0x/better-auth-lastfm'

export const auth = betterAuth({
  // ... your other config
  plugins: [
    lastfmPlugin({
      apiKey: process.env.LASTFM_API_KEY!,
      sharedSecret: process.env.LASTFM_SHARED_SECRET!,
      // Optional: customize redirect URL after successful auth
      redirectTo: '/dashboard', // default: '/dashboard'
      // Optional: set base URL (usually auto-detected)
      baseUrl: process.env.BETTER_AUTH_URL
    })
  ]
})

3. Client Configuration

import { createAuthClient } from 'better-auth/react'
import { lastfmClientPlugin } from '@ley0x/better-auth-lastfm/client'

export const authClient = createAuthClient({
  plugins: [lastfmClientPlugin()]
})

export const { useSession, signOut, signIn, signUp } = authClient

Usage

Basic Sign-In

import { authClient } from './auth-client'

// Redirect to Last.fm authentication
await authClient.signInWithLastfm()

React Component Example

import { authClient } from './auth-client'

function LoginButton() {
  const handleSignIn = async () => {
    await authClient.signInWithLastfm()
  }

  return (
    <button onClick={handleSignIn}>
      Sign in with Last.fm
    </button>
  )
}

Getting Last.fm Session Key

After authentication, you can retrieve the Last.fm session key to make API calls:

import { authClient } from './auth-client'

// Get the Last.fm session key for API calls
const sessionKey = await authClient.getLastfmSessionKey()

if (sessionKey) {
  // Use session key to call Last.fm API
  // Example: get user's recent tracks, scrobble, etc.
}

Making Last.fm API Calls

import { createLastfmApiSignature } from '@ley0x/better-auth-lastfm'

async function getRecentTracks(username: string, sessionKey: string) {
  const params = {
    method: 'user.getRecentTracks',
    user: username,
    api_key: process.env.LASTFM_API_KEY!,
    sk: sessionKey,
    format: 'json'
  }

  const signature = createLastfmApiSignature(params, process.env.LASTFM_SHARED_SECRET!)
  
  const url = new URL('https://ws.audioscrobbler.com/2.0/')
  Object.entries({ ...params, api_sig: signature }).forEach(([key, value]) => {
    url.searchParams.set(key, value)
  })

  const response = await fetch(url)
  return response.json()
}

Environment Variables

Add these to your .env file:

LASTFM_API_KEY=your_api_key_here
LASTFM_SHARED_SECRET=your_shared_secret_here
BETTER_AUTH_URL=http://localhost:3000  # Your app's base URL
BETTER_AUTH_SECRET=your_secret_here    # BetterAuth secret

Database Schema

The plugin uses BetterAuth's standard user and account tables. Last.fm session keys are stored in the accessToken field of the account record.

API Reference

lastfmPlugin(options)

Server-side plugin configuration.

Options

  • apiKey (required): Your Last.fm API key
  • sharedSecret (required): Your Last.fm shared secret
  • baseUrl (optional): Your app's base URL for callbacks
  • redirectTo (optional): Path to redirect after successful auth

lastfmClientPlugin()

Client-side plugin for browser environments.

Client Methods

  • signInWithLastfm(): Initiates Last.fm authentication flow
  • getLastfmSessionKey(): Returns the user's Last.fm session key

TypeScript Support

Full TypeScript support with exported types:

import type { 
  LastfmPluginOptions,
  LastfmSession,
  LastfmAuthResponse,
  LastfmUserProfile 
} from '@ley0x/better-auth-lastfm'

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT