Loading Solobase...
Learn by example with these real-world code snippets and complete applications built with Solobase. Copy, paste, and customize for your own projects.
Complete user signup, login, and session management.
// Sign up new user
const { data, error } = await solobase.auth.signUp({
email: 'user@example.com',
password: 'securepassword123',
options: {
data: {
first_name: 'John',
last_name: 'Doe'
}
}
})
if (error) {
console.error('Signup failed:', error.message)
} else {
console.log('User created:', data.user)
}
CRUD operations with filtering and real-time updates.
// Create new post
const { data, error } = await solobase
.from('posts')
.insert([{
title: 'My First Post',
content: 'Hello Solobase!',
status: 'published',
user_id: user.id
}])
.select()
if (error) {
console.error('Error:', error.message)
} else {
console.log('Post created:', data[0])
}
A full authentication flow with React hooks and context.
import React, { createContext, useContext, useEffect, useState } from 'react'
import { createClient } from 'solobase-js'
const AuthContext = createContext()
const solobase = createClient(
process.env.REACT_APP_SOLOBASE_URL,
process.env.REACT_APP_SOLOBASE_ANON_KEY
)
export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null)
const [loading, setLoading] = useState(true)
useEffect(() => {
// Get initial session
solobase.auth.getSession().then(({ data: { session } }) => {
setUser(session?.user ?? null)
setLoading(false)
})
// Listen for auth changes
const { data: { subscription } } = solobase.auth.onAuthStateChange(
(_event, session) => {
setUser(session?.user ?? null)
setLoading(false)
}
)
return () => subscription.unsubscribe()
}, [])
const signUp = async (email, password, metadata = {}) => {
const { data, error } = await solobase.auth.signUp({
email,
password,
options: { data: metadata }
})
return { data, error }
}
const signIn = async (email, password) => {
const { data, error } = await solobase.auth.signInWithPassword({
email,
password
})
return { data, error }
}
const signOut = async () => {
const { error } = await solobase.auth.signOut()
return { error }
}
return (
<AuthContext.Provider value={{
user,
loading,
signUp,
signIn,
signOut,
solobase
}}>
{children}
</AuthContext.Provider>
)
}
export const useAuth = () => {
const context = useContext(AuthContext)
if (!context) {
throw new Error('useAuth must be used within AuthProvider')
}
return context
}
Handle file uploads, downloads, and management with progress tracking.
// File upload with progress
const uploadFile = async (file, onProgress) => {
const fileExt = file.name.split('.').pop()
const fileName = `${Math.random()}.${fileExt}`
const filePath = `uploads/${fileName}`
const { data, error } = await solobase.storage
.from('avatars')
.upload(filePath, file, {
cacheControl: '3600',
upsert: false,
onUploadProgress: (progress) => {
const percent = (progress.loaded / progress.total) * 100
onProgress(percent)
}
})
if (error) {
console.error('Upload failed:', error.message)
return null
}
// Get public URL
const { data: urlData } = solobase.storage
.from('avatars')
.getPublicUrl(filePath)
return {
path: data.path,
url: urlData.publicUrl
}
}