Files
Community_Nursing_Atlas/web/static/js/login.js
T
jsahn 4e943700fc feat: add common utilities and feedback handling scripts
- Introduced common.js for shared functions including API calls, error reporting, and UI helpers.
- Implemented feedback.js for the feedback reporting page, including form handling and displaying feedback status.
- Added layout.js for managing galaxy and section layouts in the visualization.
- Created login.js for user authentication with session management.
- Developed search.js for term searching based on user input and synonyms.
- Implemented signup.js for user registration with validation and immediate login.
2026-10-06 23:14:45 +09:00

44 lines
2.2 KiB
JavaScript

// 로그인: 학생·교수자 공용. 교수자 아이디면 관리 화면으로.
import { api, esc, toast } from "./common.js";
const main = document.getElementById("main");
const params = new URLSearchParams(location.search);
const next = params.get("next") || "";
const safe = (u) => (u && u.startsWith("/") && !u.startsWith("//") ? u : "");
main.innerHTML = `<h1>로그인</h1>
<p class="acc-note">사례와 문제는 회원만 볼 수 있습니다.</p>
<form class="card" id="f">
<label class="field"><span>아이디</span><input class="input" name="username" autocomplete="username" required autofocus></label>
<label class="field"><span>비밀번호</span><input class="input" name="password" type="password" autocomplete="current-password" required></label>
<button class="btn primary" type="submit">로그인</button>
</form>
<p class="auth-alt">아직 회원이 아니신가요? <a href="/signup${next ? "?next=" + encodeURIComponent(next) : ""}">가입 코드로 회원 가입</a></p>
<p class="auth-alt" style="font-size:12px">아이디·비밀번호를 잊었다면 담당 교수자에게 문의해 주세요.</p>`;
document.getElementById("f").addEventListener("submit", async (e) => {
e.preventDefault();
const v = Object.fromEntries(new FormData(e.target).entries());
const btn = e.target.querySelector("button");
btn.disabled = true;
try {
const r = await api("/api/login", { method: "POST", body: v });
if (r.kind === "admin") {
const n = safe(next);
location.href = n.startsWith("/admin") || n.startsWith("/members") ? n : "/admin";
} else {
location.href = safe(next) || "/";
}
} catch (err) {
toast(err.status === 429 ? "시도가 너무 많습니다. 잠시 후 다시 해 주세요." : err.message, true);
btn.disabled = false;
}
});
// 이미 로그인한 상태면 알려 줌
api("/api/members/me").then((me) => {
if (me.logged_in || me.admin) {
main.insertAdjacentHTML("afterbegin", `<p class="acc-note">이미 ${esc(me.logged_in ? me.name : me.admin)} 님으로 로그인되어 있습니다. <a href="${me.admin ? "/admin" : "/account"}">${me.admin ? "관리 화면" : "내 정보"}</a></p>`);
}
}).catch(() => {});