A clean, modern Last.fm authentication plugin for BetterAuth.
https://www.npmjs.com/package/@ley0x/better-auth-lastfm
| .github/workflows | ||
| src | ||
| .gitignore | ||
| CLAUDE.md | ||
| eslint.config.js | ||
| LICENSE | ||
| package.json | ||
| pnpm-lock.yaml | ||
| pnpm-workspace.yaml | ||
| README.md | ||
| tsconfig.json | ||
| tsup.config.ts | ||
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
- Visit Last.fm API Account Creation
- 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 keysharedSecret(required): Your Last.fm shared secretbaseUrl(optional): Your app's base URL for callbacksredirectTo(optional): Path to redirect after successful auth
lastfmClientPlugin()
Client-side plugin for browser environments.
Client Methods
signInWithLastfm(): Initiates Last.fm authentication flowgetLastfmSessionKey(): 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