forked from CASAlabone/Loginsession_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_page.html
More file actions
199 lines (177 loc) · 5.34 KB
/
user_page.html
File metadata and controls
199 lines (177 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Page</title>
<link rel="stylesheet" href="styles.css">
<style>
body {
display: grid;
grid-template-rows: auto 1fr;
grid-template-columns: 1fr 2fr;
gap: 20px;
height: 100vh;
margin: 0;
}
#top {
grid-row: 1;
grid-column: 1 / span 2;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background-color: #f0f0f0;
}
#left {
grid-row: 2;
grid-column: 1;
padding: 20px;
background-color: #f9f9f9;
}
#right {
grid-row: 2;
grid-column: 2;
padding: 20px;
background-color: #ffffff;
}
.logout-btn {
padding: 10px 20px;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.logout-btn:hover {
background-color: #2980b9;
}
#newsfeed {
margin-bottom: 20px;
}
.post {
margin-bottom: 20px;
border: 1px solid #ccc;
padding: 10px;
border-radius: 5px;
}
.post-title {
font-weight: bold;
}
.post-body {
margin-top: 5px;
}
</style>
</head>
<body>
<div id="top">
<div class="user-box">
<h1 id="welcome-message"></h1>
<p id="user-email"></p>
</div>
<button class="logout-btn" id="logout-btn">로그아웃</button>
</div>
<div id="left">
<h2>뉴스피드</h2>
<div id="posts-list"></div>
</div>
<div id="right">
<h2>게시물 작성</h2>
<form id="post-form">
<label for="post-title">제목:</label><br>
<input type="text" id="post-title" name="post-title"><br>
<label for="post-body">내용:</label><br>
<textarea id="post-body" name="post-body"></textarea><br><br>
<button type="submit" id="post-submit">게시</button>
</form>
</div>
<script type="module">
// Import Firebase modules
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-app.js";
import { getAuth, onAuthStateChanged, signOut } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-auth.js";
import { getFirestore, collection, addDoc, getDocs } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-firestore.js";
// Initialize Firebase
const firebaseConfig = {
apiKey: "AIzaSyA2aoOXeoe8KXbqzo_Du1yfilCQkqi03wM",
authDomain: "besteam-e5d45.firebaseapp.com",
projectId: "besteam-e5d45",
storageBucket: "besteam-e5d45.appspot.com",
messagingSenderId: "1007108211259",
appId: "1:1007108211259:web:24136c60d32ffc5261521e",
measurementId: "G-KV7E75RSQZ"
};
// Initialize Firebase app
const firebaseApp = initializeApp(firebaseConfig);
// Get Auth object
const auth = getAuth(firebaseApp);
// Get Firestore object
const db = getFirestore(firebaseApp);
// Function to fetch and display posts
async function fetchAndDisplayPosts() {
const postsList = document.getElementById("posts-list");
postsList.innerHTML = ""; // Clear existing posts
const querySnapshot = await getDocs(collection(db, "posts"));
querySnapshot.forEach((doc) => {
const postData = doc.data();
const postElement = document.createElement("div");
postElement.classList.add("post");
postElement.innerHTML = `
<div class="post-title">${postData.title}</div>
<div class="post-body">${postData.body}</div>
`;
postsList.appendChild(postElement);
});
}
// Listen for authentication state changes
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, display welcome message
const displayName = user.displayName;
const email = user.email;
const msg = email + " 계정으로 로그인 됨";
document.getElementById("user-email").innerText = msg;
} else {
// User is signed out
// Redirect to login page
window.location.href = "login.html";
}
});
// Add event listener to logout button
const logoutBtn = document.getElementById("logout-btn");
logoutBtn.addEventListener("click", () => {
signOut(auth).then(() => {
console.log("User signed out successfully.");
// Redirect to login page
window.location.href = "login.html";
}).catch((error) => {
console.error("Error signing out:", error);
});
});
// Add event listener to post form
const postForm = document.getElementById("post-form");
postForm.addEventListener("submit", async (e) => {
e.preventDefault();
const title = document.getElementById("post-title").value;
const body = document.getElementById("post-body").value;
try {
const docRef = await addDoc(collection(db, "posts"), {
title,
body,
createdAt: new Date()
});
console.log("Document written with ID: ", docRef.id);
// Clear form fields
document.getElementById("post-title").value = "";
document.getElementById("post-body").value = "";
// Refresh the newsfeed
fetchAndDisplayPosts();
} catch (error) {
console.error("Error adding document: ", error);
}
});
// Initial fetch and display of posts
fetchAndDisplayPosts();
</script>
</body>
</html>