├── scripts ├── index.js ├── firebase.js ├── signin.js ├── signup.js └── home.js ├── index.html ├── home.html ├── signin.html └── signup.html /scripts/index.js: -------------------------------------------------------------------------------- 1 | import { renderBlogs } from "./home.js"; 2 | renderBlogs(); -------------------------------------------------------------------------------- /scripts/firebase.js: -------------------------------------------------------------------------------- 1 | import { initializeApp } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-app.js"; 2 | import { getAuth } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-auth.js"; 3 | import { getFirestore } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-firestore.js"; 4 | 5 | // My web app's Firebase configuration 6 | const firebaseConfig = { 7 | apiKey: "AIzaSyAu5yHHFVN6n8qk_mWJioo1btbrDBJl1Nw", 8 | authDomain: "blogging-site-c7224.firebaseapp.com", 9 | projectId: "blogging-site-c7224", 10 | storageBucket: "blogging-site-c7224.appspot.com", 11 | messagingSenderId: "288811689646", 12 | appId: "1:288811689646:web:e5fa5b9fb51f2760646f3f", 13 | measurementId: "G-GFZJRRT55C" 14 | }; 15 | 16 | // Initialize Firebase 17 | const app = initializeApp(firebaseConfig); 18 | export const auth = getAuth(app); 19 | export const db = getFirestore(app); -------------------------------------------------------------------------------- /scripts/signin.js: -------------------------------------------------------------------------------- 1 | import { signInWithEmailAndPassword } from 'https://www.gstatic.com/firebasejs/10.13.0/firebase-auth.js' 2 | import { auth } from './firebase.js' 3 | 4 | // For Getting User Details From HTML 5 | const signIn = document.querySelector("#sign-in"); 6 | const email = document.querySelector("#email"); 7 | const password = document.querySelector("#password"); 8 | 9 | // Functionality For Login 10 | signIn.addEventListener("submit", (event) => { 11 | event.preventDefault(); 12 | // Checking If User Enters a Details Or Not 13 | if (email.value && password.value) { 14 | signInWithEmailAndPassword(auth, email.value, password.value) 15 | .then((userCredential) => { 16 | // For Opening Index Page If Successfull Login 17 | window.open("home.html", "_self"); 18 | }) 19 | .catch((error) => { 20 | const errorCode = error.code; 21 | const errorMessage = error.message; 22 | alert(errorCode, errorMessage); 23 | }); 24 | 25 | // For Empty Input 26 | email.value = ""; 27 | password.value = ""; 28 | } else { 29 | // For User Not Enters Any Details 30 | my_modal_1.showModal(); 31 | } 32 | }) 33 | 34 | -------------------------------------------------------------------------------- /scripts/signup.js: -------------------------------------------------------------------------------- 1 | import { createUserWithEmailAndPassword } from 'https://www.gstatic.com/firebasejs/10.13.0/firebase-auth.js' 2 | import { auth } from './firebase.js' 3 | 4 | // For Getting User Details From HTML 5 | const signUpForm = document.querySelector("#signup-form"); 6 | const username = document.querySelector("#name"); 7 | const email = document.querySelector("#email"); 8 | const password = document.querySelector("#password"); 9 | 10 | // Functionality For Registration 11 | signUpForm.addEventListener("submit", (event) => { 12 | event.preventDefault(); 13 | // Checking If User Enters a Details Or Not 14 | if (email.value && password.value) { 15 | createUserWithEmailAndPassword(auth, email.value, password.value) 16 | .then((userCredential) => { 17 | const user = userCredential.user; 18 | console.log(user.uid); 19 | }) 20 | .catch((error) => { 21 | const errorCode = error.code; 22 | const errorMessage = error.message; 23 | alert(errorCode, errorMessage); 24 | }); 25 | 26 | // For Empty Input 27 | username.value = ""; 28 | email.value = ""; 29 | password.value = ""; 30 | 31 | // For Showing Popup Of Successfull Registration 32 | my_modal_2.showModal(); 33 | } else { 34 | // For User Not Enters Any Details 35 | my_modal_1.showModal(); 36 | } 37 | }) 38 | -------------------------------------------------------------------------------- /scripts/home.js: -------------------------------------------------------------------------------- 1 | import { signOut, onAuthStateChanged } from 'https://www.gstatic.com/firebasejs/9.22.2/firebase-auth.js'; 2 | import { collection, addDoc, query, orderBy, onSnapshot, deleteDoc, doc } from 'https://www.gstatic.com/firebasejs/10.13.0/firebase-firestore.js'; 3 | import { auth, db } from './firebase.js'; 4 | 5 | const blogsContainer = document.getElementById('blogsContainer'); 6 | 7 | export function renderBlogs(blogs) { 8 | blogsContainer.innerHTML = ''; 9 | blogs.forEach(blog => { 10 | const blogDiv = document.createElement('div'); 11 | blogDiv.className = 'bg-gray-50 p-4 rounded-lg shadow-md mb-5'; 12 | blogDiv.innerHTML = ` 13 |

${blog.title}

14 |

${blog.body}

15 | ${blog.date.toDate().toLocaleString()} 16 |
17 | 18 |
19 | `; 20 | blogsContainer.appendChild(blogDiv); 21 | }); 22 | } 23 | 24 | onAuthStateChanged(auth, (user) => { 25 | if (user) { 26 | const q = query(collection(db, 'blogs'), orderBy('date', 'desc')); 27 | onSnapshot(q, (querySnapshot) => { 28 | const blogs = querySnapshot.docs.map(doc => ({ 29 | id: doc.id, 30 | ...doc.data() 31 | })); 32 | renderBlogs(blogs); 33 | }); 34 | } else { 35 | window.open("signin.html", "_self"); 36 | } 37 | }); 38 | 39 | document.getElementById('newBlogForm').addEventListener('submit', async (event) => { 40 | event.preventDefault(); 41 | 42 | const title = document.getElementById('title').value; 43 | const body = document.getElementById('body').value; 44 | 45 | try { 46 | await addDoc(collection(db, 'blogs'), { 47 | title, 48 | body, 49 | date: new Date(), 50 | userId: auth.currentUser.uid 51 | }); 52 | document.getElementById('newBlogForm').reset(); 53 | } catch (error) { 54 | console.error(error); 55 | } 56 | }); 57 | 58 | document.deleteBlog = async (id) => { 59 | try { 60 | await deleteDoc(doc(db, 'blogs', id)); 61 | } catch (error) { 62 | console.error(error); 63 | } 64 | }; 65 | 66 | // For Signing Out User 67 | const signOutUser = document.querySelector("#sign-out"); 68 | signOutUser.addEventListener("click", (event) => { 69 | event.preventDefault(); 70 | signOut(auth).then(() => { 71 | window.open("signin.html", "_self"); 72 | }).catch((error) => { 73 | alert(error); 74 | }); 75 | }) 76 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Blogging Site 8 | 9 | 10 | 11 | 12 | 13 | 14 | 62 | 63 | 64 |
65 |

All Blogs

66 |
67 |
68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Blogging Site 8 | 9 | 10 | 11 | 12 | 13 | 14 | 62 | 63 |
64 |
65 |

Post a New Blog

66 |
67 |
68 | 69 | 72 |
73 | 74 |
75 | 76 | 79 |
80 | 81 | 82 |
83 |
84 | 85 |
86 |

Your Blogs

87 |
88 |
89 |
90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /signin.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Blogging Site 8 | 9 | 10 | 11 | 12 | 13 | 14 | 56 | 57 | 58 |
59 |

Sign In

60 |
61 |
62 | 63 | 65 |
66 |
67 | 68 | 70 |
71 | 74 |

Don't have an account? Sign up

76 |
77 |
78 | 79 | 80 | 81 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /signup.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Blogging Site 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 57 | 58 | 59 |
60 |

Sign Up

61 |
62 |
63 | 64 | 66 |
67 |
68 | 69 | 71 |
72 |
73 | 74 | 76 |
77 | 80 |

Already have an account? Sign in

82 |
83 |
84 | 85 | 86 | 87 | 97 | 98 | 99 | 100 | 101 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | --------------------------------------------------------------------------------