bdr-auth-client-vanilla
v0.1.0
Published
Vanilla JS authentication client for Supabase Auth — provides signIn, signOut, onAuthStateChange, and getAccessToken
Maintainers
Readme
bdr-auth-client-vanilla
Vanilla JavaScript authentication client for Supabase Auth. Provides singleton client initialization, sign-in/sign-out, auth state subscriptions, and access token retrieval as ES module exports.
Installation
npm install bdr-auth-client-vanillaSetup
Set environment variables (Vite example):
VITE_SUPABASE_URL=https://your-project.supabase.co
VITE_SUPABASE_ANON_KEY=your-anon-keyUsage
import { initSupabase, signIn, signOut, onAuthStateChange, getAccessToken } from 'bdr-auth-client-vanilla';
// Initialize (singleton — safe to call multiple times)
initSupabase();
// Subscribe to auth state changes
onAuthStateChange((user) => {
if (user) {
console.log('Signed in:', user.email);
} else {
console.log('Signed out');
}
});
// Sign in
await signIn('[email protected]', 'password');
// Get access token for API calls
const token = await getAccessToken();
fetch('/api/data', {
headers: { Authorization: `Bearer ${token}` }
});
// Sign out
await signOut();API
initSupabase()
Creates a singleton Supabase client from environment variables. Returns the client instance. Subsequent calls return the same instance.
signIn(email, password)
Authenticates via Supabase Auth. Returns the session object. Throws on failure with the Supabase error reason.
signOut()
Terminates the session. After calling, getAccessToken() returns null.
onAuthStateChange(callback)
Subscribes to auth state changes. The callback receives the user object or null.
getAccessToken()
Returns the current JWT access token string from the active session, or null if no session exists. The Supabase SDK handles automatic refresh.
Environment Variables
Reads configuration with this priority:
VITE_SUPABASE_URL/VITE_SUPABASE_ANON_KEY(Vite)SUPABASE_URL/SUPABASE_ANON_KEY(Node.js / generic)
License
MIT
