276 lines
9.9 KiB
HTML
276 lines
9.9 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>PocketBase Frontend Template</title>
|
|
<script src="https://unpkg.com/pocketbase@latest/dist/pocketbase.umd.js"></script>
|
|
<style>
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
max-width: 800px;
|
|
margin: 50px auto;
|
|
padding: 20px;
|
|
}
|
|
.card {
|
|
border: 1px solid #ddd;
|
|
border-radius: 8px;
|
|
padding: 20px;
|
|
margin: 20px 0;
|
|
}
|
|
.hidden {
|
|
display: none;
|
|
}
|
|
input, textarea, button {
|
|
width: 100%;
|
|
padding: 10px;
|
|
margin: 5px 0;
|
|
box-sizing: border-box;
|
|
}
|
|
button {
|
|
background: #007bff;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
}
|
|
button:hover {
|
|
background: #0056b3;
|
|
}
|
|
button:disabled {
|
|
background: #ccc;
|
|
cursor: not-allowed;
|
|
}
|
|
.error {
|
|
color: red;
|
|
margin: 10px 0;
|
|
}
|
|
.success {
|
|
color: green;
|
|
margin: 10px 0;
|
|
}
|
|
#posts {
|
|
margin-top: 20px;
|
|
}
|
|
.post {
|
|
border-left: 3px solid #007bff;
|
|
padding-left: 15px;
|
|
margin: 15px 0;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>PocketBase Frontend Template</h1>
|
|
|
|
<!-- Auth Section -->
|
|
<div id="auth-section" class="card">
|
|
<h2>Authentication</h2>
|
|
<div id="login-form">
|
|
<h3>Login</h3>
|
|
<input type="email" id="login-email" placeholder="Email" required>
|
|
<input type="password" id="login-password" placeholder="Password" required>
|
|
<button onclick="login()">Login</button>
|
|
<div id="login-message"></div>
|
|
</div>
|
|
|
|
<div id="register-form" style="margin-top: 20px;">
|
|
<h3>Register</h3>
|
|
<input type="email" id="register-email" placeholder="Email" required>
|
|
<input type="text" id="register-name" placeholder="Name" required>
|
|
<input type="password" id="register-password" placeholder="Password" required>
|
|
<input type="password" id="register-password-confirm" placeholder="Confirm Password" required>
|
|
<button onclick="register()">Register</button>
|
|
<div id="register-message"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- User Info Section -->
|
|
<div id="user-section" class="card hidden">
|
|
<h2>Welcome, <span id="user-email"></span></h2>
|
|
<button onclick="logout()">Logout</button>
|
|
</div>
|
|
|
|
<!-- Create Post Section -->
|
|
<div id="create-section" class="card hidden">
|
|
<h2>Create Post</h2>
|
|
<input type="text" id="post-title" placeholder="Post Title" required>
|
|
<textarea id="post-content" placeholder="Post Content" rows="5" required></textarea>
|
|
<button onclick="createPost()">Create Post</button>
|
|
<div id="create-message"></div>
|
|
</div>
|
|
|
|
<!-- Posts Section -->
|
|
<div id="posts" class="card hidden">
|
|
<h2>Posts</h2>
|
|
<button onclick="loadPosts()">Refresh Posts</button>
|
|
<div id="posts-list"></div>
|
|
</div>
|
|
|
|
<script>
|
|
// Initialize PocketBase client
|
|
const pb = new PocketBase('http://127.0.0.1:8090');
|
|
|
|
// Check if user is already logged in
|
|
checkAuth();
|
|
|
|
async function checkAuth() {
|
|
if (pb.authStore.isValid) {
|
|
const user = pb.authStore.model;
|
|
document.getElementById('auth-section').classList.add('hidden');
|
|
document.getElementById('user-section').classList.remove('hidden');
|
|
document.getElementById('create-section').classList.remove('hidden');
|
|
document.getElementById('posts').classList.remove('hidden');
|
|
document.getElementById('user-email').textContent = user.email;
|
|
loadPosts();
|
|
}
|
|
}
|
|
|
|
async function login() {
|
|
const email = document.getElementById('login-email').value;
|
|
const password = document.getElementById('login-password').value;
|
|
const messageDiv = document.getElementById('login-message');
|
|
|
|
try {
|
|
const authData = await pb.collection('users').authWithPassword(email, password);
|
|
messageDiv.className = 'success';
|
|
messageDiv.textContent = 'Login successful!';
|
|
checkAuth();
|
|
} catch (e) {
|
|
messageDiv.className = 'error';
|
|
messageDiv.textContent = e.data?.message || 'Login failed. Please check your credentials.';
|
|
}
|
|
}
|
|
|
|
async function register() {
|
|
const email = document.getElementById('register-email').value;
|
|
const name = document.getElementById('register-name').value;
|
|
const password = document.getElementById('register-password').value;
|
|
const passwordConfirm = document.getElementById('register-password-confirm').value;
|
|
const messageDiv = document.getElementById('register-message');
|
|
|
|
if (password !== passwordConfirm) {
|
|
messageDiv.className = 'error';
|
|
messageDiv.textContent = 'Passwords do not match.';
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const authData = await pb.collection('users').create({
|
|
email: email,
|
|
password: password,
|
|
passwordConfirm: passwordConfirm,
|
|
name: name
|
|
});
|
|
|
|
// Auto-login after registration
|
|
await pb.collection('users').authWithPassword(email, password);
|
|
|
|
messageDiv.className = 'success';
|
|
messageDiv.textContent = 'Registration successful!';
|
|
checkAuth();
|
|
} catch (e) {
|
|
messageDiv.className = 'error';
|
|
messageDiv.textContent = e.data?.message || 'Registration failed.';
|
|
}
|
|
}
|
|
|
|
function logout() {
|
|
pb.authStore.clear();
|
|
location.reload();
|
|
}
|
|
|
|
async function createPost() {
|
|
const title = document.getElementById('post-title').value;
|
|
const content = document.getElementById('post-content').value;
|
|
const messageDiv = document.getElementById('create-message');
|
|
|
|
if (!pb.authStore.isValid) {
|
|
messageDiv.className = 'error';
|
|
messageDiv.textContent = 'You must be logged in to create a post.';
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const data = await pb.collection('posts').create({
|
|
title: title,
|
|
content: content,
|
|
author: pb.authStore.model.id,
|
|
status: 'published'
|
|
});
|
|
|
|
messageDiv.className = 'success';
|
|
messageDiv.textContent = 'Post created successfully!';
|
|
document.getElementById('post-title').value = '';
|
|
document.getElementById('post-content').value = '';
|
|
loadPosts();
|
|
} catch (e) {
|
|
messageDiv.className = 'error';
|
|
messageDiv.textContent = e.data?.message || 'Failed to create post.';
|
|
}
|
|
}
|
|
|
|
async function loadPosts() {
|
|
const postsList = document.getElementById('posts-list');
|
|
postsList.innerHTML = '<p>Loading...</p>';
|
|
|
|
try {
|
|
const records = await pb.collection('posts').getList(1, 50, {
|
|
sort: '-created',
|
|
expand: 'author'
|
|
});
|
|
|
|
if (records.items.length === 0) {
|
|
postsList.innerHTML = '<p>No posts yet. Be the first to create one!</p>';
|
|
return;
|
|
}
|
|
|
|
postsList.innerHTML = records.items.map(post => {
|
|
const author = post.expand?.author;
|
|
return `
|
|
<div class="post">
|
|
<h3>${escapeHtml(post.title)}</h3>
|
|
<p>${escapeHtml(post.content)}</p>
|
|
<small>By ${author?.name || 'Unknown'} on ${new Date(post.created).toLocaleString()}</small>
|
|
${post.author === pb.authStore.model?.id ? `
|
|
<div style="margin-top: 10px;">
|
|
<button onclick="editPost('${post.id}')" style="background: #28a745;">Edit</button>
|
|
<button onclick="deletePost('${post.id}')" style="background: #dc3545; margin-left: 5px;">Delete</button>
|
|
</div>
|
|
` : ''}
|
|
</div>
|
|
`;
|
|
}).join('');
|
|
} catch (e) {
|
|
postsList.innerHTML = `<p class="error">Failed to load posts: ${e.message}</p>`;
|
|
}
|
|
}
|
|
|
|
async function deletePost(id) {
|
|
if (!confirm('Are you sure you want to delete this post?')) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await pb.collection('posts').delete(id);
|
|
loadPosts();
|
|
} catch (e) {
|
|
alert('Failed to delete post: ' + (e.data?.message || e.message));
|
|
}
|
|
}
|
|
|
|
function escapeHtml(text) {
|
|
const div = document.createElement('div');
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
}
|
|
|
|
// Set up real-time subscription (if supported)
|
|
pb.collection('posts').subscribe('*', function (e) {
|
|
console.log('Real-time event:', e);
|
|
// Reload posts when changes occur
|
|
loadPosts();
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|