v0.1.11 - 인증 UI 연결
This commit is contained in:
160
src/App.vue
160
src/App.vue
@@ -1,8 +1,17 @@
|
||||
<script setup>
|
||||
import { computed, reactive, ref, watch, nextTick } from 'vue'
|
||||
import { computed, onMounted, reactive, ref, watch, nextTick } from 'vue'
|
||||
import AuthDialog from './components/AuthDialog.vue'
|
||||
import MiniCalendar from './components/MiniCalendar.vue'
|
||||
import PlannerPage from './components/PlannerPage.vue'
|
||||
import StatsDashboard from './components/StatsDashboard.vue'
|
||||
import {
|
||||
clearAuthState,
|
||||
fetchCurrentUser,
|
||||
login,
|
||||
persistAuthState,
|
||||
readAuthState,
|
||||
signup,
|
||||
} from './lib/authClient'
|
||||
import {
|
||||
createInitialPlannerRecords,
|
||||
persistPlannerState,
|
||||
@@ -12,10 +21,21 @@ import {
|
||||
const screenMode = ref('planner')
|
||||
const viewMode = ref('focus')
|
||||
const printLayout = ref('single')
|
||||
const authDialogOpen = ref(false)
|
||||
const authMode = ref('login')
|
||||
const authBusy = ref(false)
|
||||
const authMessage = ref('')
|
||||
const authToken = ref('')
|
||||
const currentUser = ref(null)
|
||||
const selectedDate = ref(new Date())
|
||||
const calendarViewDate = ref(new Date(selectedDate.value))
|
||||
const statsRangeStart = ref(toKey(new Date(new Date().setDate(new Date().getDate() - 6))))
|
||||
const statsRangeEnd = ref(toKey(new Date()))
|
||||
const authForm = reactive({
|
||||
nickname: '',
|
||||
email: '',
|
||||
password: '',
|
||||
})
|
||||
|
||||
const hours = [
|
||||
'6', '7', '8', '9', '10', '11', '12',
|
||||
@@ -271,6 +291,8 @@ const markedDateKeys = computed(() =>
|
||||
.map(([key]) => key),
|
||||
)
|
||||
|
||||
const isAuthenticated = computed(() => Boolean(authToken.value && currentUser.value))
|
||||
|
||||
const filledTasks = computed(() =>
|
||||
planner.value.tasks.filter((task) => task.title.trim()),
|
||||
)
|
||||
@@ -540,6 +562,93 @@ function clearTaskLabels(record) {
|
||||
})
|
||||
}
|
||||
|
||||
function resetAuthForm() {
|
||||
authForm.nickname = ''
|
||||
authForm.email = ''
|
||||
authForm.password = ''
|
||||
}
|
||||
|
||||
function openAuthDialog(mode = 'login') {
|
||||
authMode.value = mode
|
||||
authMessage.value = ''
|
||||
authDialogOpen.value = true
|
||||
}
|
||||
|
||||
function closeAuthDialog() {
|
||||
authDialogOpen.value = false
|
||||
authMessage.value = ''
|
||||
resetAuthForm()
|
||||
}
|
||||
|
||||
function updateAuthField({ field, value }) {
|
||||
authForm[field] = value
|
||||
}
|
||||
|
||||
function applyAuthSuccess(data) {
|
||||
authToken.value = data.token
|
||||
currentUser.value = data.user
|
||||
persistAuthState({
|
||||
token: data.token,
|
||||
user: data.user,
|
||||
})
|
||||
closeAuthDialog()
|
||||
}
|
||||
|
||||
async function submitAuthForm() {
|
||||
authBusy.value = true
|
||||
authMessage.value = ''
|
||||
|
||||
try {
|
||||
const result =
|
||||
authMode.value === 'login'
|
||||
? await login({
|
||||
email: authForm.email,
|
||||
password: authForm.password,
|
||||
})
|
||||
: await signup({
|
||||
nickname: authForm.nickname,
|
||||
email: authForm.email,
|
||||
password: authForm.password,
|
||||
})
|
||||
|
||||
applyAuthSuccess(result)
|
||||
} catch (error) {
|
||||
authMessage.value = error.message || '인증 처리 중 문제가 발생했습니다.'
|
||||
} finally {
|
||||
authBusy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreAuthSession() {
|
||||
const savedAuth = readAuthState()
|
||||
|
||||
if (!savedAuth.token) {
|
||||
return
|
||||
}
|
||||
|
||||
authToken.value = savedAuth.token
|
||||
currentUser.value = savedAuth.user ?? null
|
||||
|
||||
try {
|
||||
const result = await fetchCurrentUser(savedAuth.token)
|
||||
currentUser.value = result.user
|
||||
persistAuthState({
|
||||
token: savedAuth.token,
|
||||
user: result.user,
|
||||
})
|
||||
} catch (error) {
|
||||
authToken.value = ''
|
||||
currentUser.value = null
|
||||
clearAuthState()
|
||||
}
|
||||
}
|
||||
|
||||
function logout() {
|
||||
authToken.value = ''
|
||||
currentUser.value = null
|
||||
clearAuthState()
|
||||
}
|
||||
|
||||
function applyPrintPageStyle(layout) {
|
||||
if (typeof document === 'undefined') {
|
||||
return
|
||||
@@ -566,6 +675,10 @@ async function printSelectedPlanner(layout = 'single') {
|
||||
await nextTick()
|
||||
window.print()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
restoreAuthSession()
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -583,6 +696,39 @@ async function printSelectedPlanner(layout = 'single') {
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex flex-wrap items-center gap-3">
|
||||
<div class="inline-flex items-center gap-2 rounded-full border border-stone-200 bg-white px-2 py-2">
|
||||
<template v-if="isAuthenticated">
|
||||
<div class="px-2">
|
||||
<p class="text-[10px] font-bold tracking-[0.16em] text-stone-500">SIGNED IN</p>
|
||||
<p class="text-sm font-semibold tracking-[0.02em] text-stone-900">
|
||||
{{ currentUser.nickname }}
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-full border border-stone-200 px-3 py-2 text-xs font-bold tracking-[0.14em] text-stone-600 transition hover:border-stone-400 hover:text-ink"
|
||||
@click="logout"
|
||||
>
|
||||
LOGOUT
|
||||
</button>
|
||||
</template>
|
||||
<template v-else>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-full border border-stone-200 px-3 py-2 text-xs font-bold tracking-[0.14em] text-stone-600 transition hover:border-stone-400 hover:text-ink"
|
||||
@click="openAuthDialog('login')"
|
||||
>
|
||||
LOGIN
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-full border border-stone-900 bg-stone-900 px-3 py-2 text-xs font-bold tracking-[0.14em] text-white transition hover:bg-stone-700"
|
||||
@click="openAuthDialog('signup')"
|
||||
>
|
||||
SIGN UP
|
||||
</button>
|
||||
</template>
|
||||
</div>
|
||||
<div class="inline-flex rounded-full border border-stone-200 bg-stone-100 p-1">
|
||||
<button
|
||||
type="button"
|
||||
@@ -912,5 +1058,17 @@ async function printSelectedPlanner(layout = 'single') {
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<AuthDialog
|
||||
:open="authDialogOpen"
|
||||
:mode="authMode"
|
||||
:form="authForm"
|
||||
:busy="authBusy"
|
||||
:message="authMessage"
|
||||
@close="closeAuthDialog"
|
||||
@submit="submitAuthForm"
|
||||
@switch-mode="authMode = $event; authMessage = ''"
|
||||
@update:field="updateAuthField"
|
||||
/>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
129
src/components/AuthDialog.vue
Normal file
129
src/components/AuthDialog.vue
Normal file
@@ -0,0 +1,129 @@
|
||||
<script setup>
|
||||
const props = defineProps({
|
||||
open: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
mode: {
|
||||
type: String,
|
||||
default: 'login',
|
||||
},
|
||||
form: {
|
||||
type: Object,
|
||||
required: true,
|
||||
},
|
||||
busy: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
message: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
})
|
||||
|
||||
const emit = defineEmits([
|
||||
'close',
|
||||
'submit',
|
||||
'switch-mode',
|
||||
'update:field',
|
||||
])
|
||||
|
||||
function updateField(field, event) {
|
||||
emit('update:field', {
|
||||
field,
|
||||
value: event.target.value,
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-if="open"
|
||||
class="fixed inset-0 z-50 flex items-center justify-center bg-stone-900/45 px-4 py-8 backdrop-blur-sm"
|
||||
>
|
||||
<div class="w-full max-w-md rounded-[28px] border border-white/60 bg-[#f6f1e8] p-6 shadow-2xl sm:p-7">
|
||||
<div class="flex items-start justify-between gap-4">
|
||||
<div class="space-y-2">
|
||||
<p class="text-[11px] font-bold uppercase tracking-[0.24em] text-stone-500">Account</p>
|
||||
<h2 class="text-2xl font-semibold tracking-[-0.04em] text-stone-900">
|
||||
{{ mode === 'login' ? '로그인' : '회원가입' }}
|
||||
</h2>
|
||||
<p class="text-sm leading-6 text-stone-600">
|
||||
{{ mode === 'login' ? '저장된 플래너를 다시 이어서 볼 수 있습니다.' : '사용자별 기록과 통계를 연결하기 위한 계정을 만듭니다.' }}
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-full border border-stone-300 px-3 py-2 text-[10px] font-bold tracking-[0.16em] text-stone-500 transition hover:border-stone-500 hover:text-stone-900"
|
||||
@click="emit('close')"
|
||||
>
|
||||
CLOSE
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<form class="mt-6 space-y-4" @submit.prevent="emit('submit')">
|
||||
<div v-if="mode === 'signup'" class="space-y-2">
|
||||
<label class="text-[11px] font-bold tracking-[0.16em] text-stone-600">닉네임</label>
|
||||
<input
|
||||
:value="form.nickname"
|
||||
type="text"
|
||||
class="w-full rounded-2xl border border-stone-300 bg-white px-4 py-3 text-sm font-semibold text-stone-800 outline-none transition focus:border-stone-500"
|
||||
placeholder="닉네임을 입력해 주세요."
|
||||
@input="updateField('nickname', $event)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="space-y-2">
|
||||
<label class="text-[11px] font-bold tracking-[0.16em] text-stone-600">이메일</label>
|
||||
<input
|
||||
:value="form.email"
|
||||
type="email"
|
||||
class="w-full rounded-2xl border border-stone-300 bg-white px-4 py-3 text-sm font-semibold text-stone-800 outline-none transition focus:border-stone-500"
|
||||
placeholder="zenn@example.com"
|
||||
@input="updateField('email', $event)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="space-y-2">
|
||||
<label class="text-[11px] font-bold tracking-[0.16em] text-stone-600">비밀번호</label>
|
||||
<input
|
||||
:value="form.password"
|
||||
type="password"
|
||||
class="w-full rounded-2xl border border-stone-300 bg-white px-4 py-3 text-sm font-semibold text-stone-800 outline-none transition focus:border-stone-500"
|
||||
placeholder="8자 이상 입력해 주세요."
|
||||
@input="updateField('password', $event)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<p
|
||||
v-if="message"
|
||||
class="rounded-2xl border border-stone-300 bg-white/80 px-4 py-3 text-sm font-semibold leading-6 text-stone-700"
|
||||
>
|
||||
{{ message }}
|
||||
</p>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
class="w-full rounded-full bg-stone-900 px-5 py-3 text-xs font-bold tracking-[0.18em] text-white transition hover:bg-stone-700 disabled:cursor-not-allowed disabled:bg-stone-400"
|
||||
:disabled="busy"
|
||||
>
|
||||
{{ busy ? '처리 중...' : mode === 'login' ? 'LOGIN' : 'SIGN UP' }}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="mt-5 flex items-center justify-between gap-3 rounded-2xl border border-stone-300 bg-white/70 px-4 py-3">
|
||||
<p class="text-sm font-semibold text-stone-600">
|
||||
{{ mode === 'login' ? '아직 계정이 없나요?' : '이미 계정이 있나요?' }}
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
class="text-xs font-bold tracking-[0.16em] text-stone-900 underline underline-offset-4"
|
||||
@click="emit('switch-mode', mode === 'login' ? 'signup' : 'login')"
|
||||
>
|
||||
{{ mode === 'login' ? '회원가입' : '로그인' }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
81
src/lib/authClient.js
Normal file
81
src/lib/authClient.js
Normal file
@@ -0,0 +1,81 @@
|
||||
const AUTH_STORAGE_KEY = 'ten-minute-planner-auth'
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:3001'
|
||||
|
||||
function buildHeaders(token, extraHeaders = {}) {
|
||||
return {
|
||||
'Content-Type': 'application/json',
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
...extraHeaders,
|
||||
}
|
||||
}
|
||||
|
||||
async function request(path, { method = 'GET', token, body } = {}) {
|
||||
const response = await fetch(`${API_BASE_URL}${path}`, {
|
||||
method,
|
||||
headers: buildHeaders(token),
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
})
|
||||
|
||||
const data = await response.json().catch(() => ({}))
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || '요청 처리 중 문제가 발생했습니다.')
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
export function readAuthState() {
|
||||
if (typeof window === 'undefined') {
|
||||
return { token: '', user: null }
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(window.localStorage.getItem(AUTH_STORAGE_KEY) ?? '{"token":"","user":null}')
|
||||
} catch (error) {
|
||||
console.warn('저장된 인증 상태를 불러오지 못했습니다.', error)
|
||||
return { token: '', user: null }
|
||||
}
|
||||
}
|
||||
|
||||
export function persistAuthState({ token, user }) {
|
||||
if (typeof window === 'undefined') {
|
||||
return
|
||||
}
|
||||
|
||||
window.localStorage.setItem(
|
||||
AUTH_STORAGE_KEY,
|
||||
JSON.stringify({
|
||||
token,
|
||||
user,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
export function clearAuthState() {
|
||||
if (typeof window === 'undefined') {
|
||||
return
|
||||
}
|
||||
|
||||
window.localStorage.removeItem(AUTH_STORAGE_KEY)
|
||||
}
|
||||
|
||||
export async function signup({ email, password, nickname }) {
|
||||
return request('/api/auth/signup', {
|
||||
method: 'POST',
|
||||
body: { email, password, nickname },
|
||||
})
|
||||
}
|
||||
|
||||
export async function login({ email, password }) {
|
||||
return request('/api/auth/login', {
|
||||
method: 'POST',
|
||||
body: { email, password },
|
||||
})
|
||||
}
|
||||
|
||||
export async function fetchCurrentUser(token) {
|
||||
return request('/api/auth/me', {
|
||||
token,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user