регистрация, авторизация ок. стартовый экран и добавление привычек пока без функционала, но роуты работают

This commit is contained in:
Alexander Andreev 2026-01-03 14:50:36 +03:00
parent 367aef0d7a
commit 055515269b
4 changed files with 265 additions and 98 deletions

View File

@ -1,99 +1,63 @@
// /composables/useAuth.ts // /composables/useAuth.ts
// Define User interface
interface User { interface User {
id: string; id: string;
email: string; email: string;
nickname: string; nickname: string;
} }
/**
* Composable for authentication management.
* All state is managed by Nuxt's useState to ensure it's shared and SSR-safe.
*/
export function useAuth() { export function useAuth() {
// --- State --- // All Nuxt composables that require instance access MUST be called inside the setup function.
// All state is defined here, inside the composable function. // useState is how we create shared, SSR-safe state in Nuxt.
// Nuxt's useState ensures this state is a singleton across the app.
const user = useState<User | null>('user', () => null); const user = useState<User | null>('user', () => null);
const loading = useState('auth_loading', () => false);
const initialized = useState('auth_initialized', () => false); const initialized = useState('auth_initialized', () => false);
const loading = ref(false); // This is a local, non-shared loading ref for fetchMe's internal use
// --- Composables ---
const api = useApi(); const api = useApi();
// --- Computed Properties ---
const isAuthenticated = computed(() => !!user.value); const isAuthenticated = computed(() => !!user.value);
// --- Methods ---
/**
* Fetches the current user from the backend.
* Runs only once, guarded by the 'initialized' state.
*/
const fetchMe = async () => { const fetchMe = async () => {
if (initialized.value) { if (initialized.value) return;
return;
}
loading.value = true; loading.value = true;
initialized.value = true; initialized.value = true;
try { try {
const fetchedUser = await api<User>('/auth/me', { method: 'GET' }); user.value = await api<User>('/auth/me', { method: 'GET' });
user.value = fetchedUser;
} catch (error) { } catch (error) {
user.value = null; // Silently handle 401s or other errors user.value = null; // Silently set user to null
} finally { } finally {
loading.value = false; loading.value = false;
} }
}; };
/**
* Logs the user in and fetches their data on success.
*/
const login = async (email, password) => { const login = async (email, password) => {
loading.value = true; // The calling component is responsible for its own loading state.
try { // This function just performs the action.
await api('/auth/login', { await api('/auth/login', {
method: 'POST', method: 'POST',
body: { email, password }, body: { email, password },
}); });
// After a successful login, allow a re-fetch of the user state.
// We must re-fetch the user after logging in. initialized.value = false;
// Resetting 'initialized' allows fetchMe to run again. await fetchMe();
initialized.value = false;
await fetchMe();
await navigateTo('/');
} catch (error) {
console.error('Login failed:', error);
throw error; // Re-throw to allow the UI to handle it
} finally {
loading.value = false;
}
}; };
/**
* Logs the user out.
*/
const logout = async () => { const logout = async () => {
loading.value = true;
try { try {
await api('/auth/logout', { method: 'POST' }); await api('/auth/logout', { method: 'POST' });
} catch (error) {
console.error('Logout API call failed, proceeding with client-side logout:', error);
} finally { } finally {
// Always clear state and redirect, regardless of API call success.
user.value = null; user.value = null;
initialized.value = false; // Allow re-fetch on next app load/login initialized.value = false;
loading.value = false;
await navigateTo('/login'); await navigateTo('/login');
} }
}; };
// Expose the state and methods.
return { return {
user, user,
loading,
isAuthenticated, isAuthenticated,
initialized,
fetchMe, fetchMe,
login, login,
logout, logout,

View File

@ -1,45 +1,163 @@
<template> <template>
<div class="habits-container"> <div class="habits-container">
<h3>My Habits</h3> <h3>My Habits</h3>
<!-- Create Habit Form -->
<form @submit.prevent="createHabit" class="create-habit-form">
<h4>Create a New Habit</h4>
<div v-if="error" class="error-message">{{ error }}</div>
<input v-model="newHabitName" type="text" placeholder="e.g., Read for 15 minutes" required />
<div class="days-selector">
<label v-for="day in daysOfWeek" :key="day" class="day-label">
<input type="checkbox" :value="day" v-model="newHabitDays" />
<span>{{ day }}</span>
</label>
</div>
<button type="submit" :disabled="loading.create">
{{ loading.create ? 'Adding...' : 'Add Habit' }}
</button>
</form>
<!-- Habits List -->
<div class="habits-list"> <div class="habits-list">
<!-- Mock Habit Card 1 --> <p v-if="loading.fetch">Loading habits...</p>
<div class="habit-card"> <div v-for="habit in habits" :key="habit.id" class="habit-card">
<div class="habit-info"> <div class="habit-info">
<h4>Practice Nuxt</h4> <h4>{{ habit.name }}</h4>
<p>Active: Mon, Wed, Fri</p> <div class="habit-days">
<span v-for="day in habit.daysOfWeek" :key="day" class="day-chip">{{ day }}</span>
</div>
</div> </div>
<button class="complete-btn">Complete</button> <div class="habit-actions">
</div> <button
v-if="isActiveToday(habit) && !isCompleteToday(habit)"
<!-- Mock Habit Card 2 --> @click="completeHabit(habit.id)"
<div class="habit-card"> :disabled="loading.complete === habit.id"
<div class="habit-info"> class="complete-btn">
<h4>Walk 5,000 steps</h4> {{ loading.complete === habit.id ? '...' : 'Complete' }}
<p>Active: Everyday</p> </button>
<span v-if="isCompleteToday(habit)" class="completed-text">Done! </span>
</div> </div>
<button class="complete-btn">Complete</button>
</div>
<!-- Mock Habit Card 3 -->
<div class="habit-card">
<div class="habit-info">
<h4>Read a chapter</h4>
<p>Active: Tue, Thu, Sat, Sun</p>
</div>
<button class="complete-btn completed">Done</button>
</div> </div>
<p v-if="!loading.fetch && habits.length === 0">No habits yet. Add one above!</p>
</div> </div>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
// No logic, just visual placeholders import { ref, onMounted } from 'vue';
// --- Type Definitions ---
interface Habit {
id: number;
name: string;
daysOfWeek: string[];
completions: { id: number; date: string }[];
}
// --- Composables ---
const api = useApi();
// --- State ---
const habits = ref<Habit[]>([]);
const newHabitName = ref('');
const daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const newHabitDays = ref<string[]>([]);
const error = ref<string | null>(null);
const loading = ref({
fetch: false,
create: false,
complete: null as number | null,
});
// --- Helper Functions ---
const getTodayString = () => new Date().toISOString().split('T')[0];
const getTodayDay = () => {
const dayIndex = new Date().getDay(); // Sunday is 0
return daysOfWeek[(dayIndex + 6) % 7]; // Adjust to Mon = 0, Sun = 6 -> then map to our array
};
const isCompleteToday = (habit: Habit) => {
const today = getTodayString();
return habit.completions.some(c => c.date.startsWith(today));
};
const isActiveToday = (habit: Habit) => {
const today = getTodayDay();
return habit.daysOfWeek.includes(today);
};
// --- API Functions ---
const fetchHabits = async () => {
loading.value.fetch = true;
error.value = null;
try {
const data = await api<Habit[]>('/habits');
habits.value = data;
} catch (err: any) {
console.error('Failed to fetch habits:', err);
error.value = 'Could not load habits.';
} finally {
loading.value.fetch = false;
}
};
const createHabit = async () => {
if (!newHabitName.value || newHabitDays.value.length === 0) {
error.value = 'Please provide a name and select at least one day.';
return;
}
loading.value.create = true;
error.value = null;
try {
const newHabit = await api<Habit>('/habits', {
method: 'POST',
body: {
name: newHabitName.value,
daysOfWeek: newHabitDays.value,
},
});
habits.value.push(newHabit); // Optimistic update
// Reset form
newHabitName.value = '';
newHabitDays.value = [];
} catch (err: any) {
console.error('Failed to create habit:', err);
error.value = err.data?.message || 'Could not create habit.';
} finally {
loading.value.create = false;
}
};
const completeHabit = async (habitId: number) => {
loading.value.complete = habitId;
error.value = null;
try {
const completion = await api<{ id: number; date: string }>(`/habits/${habitId}/complete`, {
method: 'POST',
});
// Optimistic update
const habit = habits.value.find(h => h.id === habitId);
if (habit) {
habit.completions.push(completion);
}
} catch (err: any) {
console.error(`Failed to complete habit ${habitId}:`, err);
error.value = err.data?.message || 'Could not complete habit.';
} finally {
loading.value.complete = null;
}
};
// --- Lifecycle Hooks ---
onMounted(fetchHabits);
</script> </script>
<style scoped> <style scoped>
.habits-container { .habits-container {
max-width: 600px; max-width: 600px;
margin: 0 auto; margin: 0 auto;
padding-bottom: 40px; /* Space for form */
} }
h3 { h3 {
@ -47,6 +165,70 @@ h3 {
margin-bottom: 20px; margin-bottom: 20px;
} }
/* Create Form */
.create-habit-form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
margin-bottom: 30px;
}
.create-habit-form h4 {
margin-top: 0;
text-align: center;
}
.create-habit-form input[type="text"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin-bottom: 15px;
}
.days-selector {
display: flex;
justify-content: space-between;
margin-bottom: 15px;
}
.day-label {
cursor: pointer;
text-align: center;
}
.day-label input {
display: none;
}
.day-label span {
display: inline-block;
width: 35px;
line-height: 35px;
border: 1px solid #ccc;
border-radius: 50%;
font-size: 0.9em;
}
.day-label input:checked + span {
background-color: #81a1c1;
color: white;
border-color: #81a1c1;
}
.create-habit-form button {
width: 100%;
padding: 10px;
background-color: #5e81ac;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
/* Habits List */
.habits-list { .habits-list {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -64,14 +246,20 @@ h3 {
} }
.habit-info h4 { .habit-info h4 {
margin: 0 0 5px 0; margin: 0 0 10px 0;
font-size: 1.1em;
} }
.habit-info p { .habit-days {
margin: 0; display: flex;
color: #666; gap: 5px;
font-size: 0.9em; }
.day-chip {
background-color: #eceff4;
color: #4c566a;
padding: 2px 6px;
border-radius: 10px;
font-size: 0.8em;
} }
.complete-btn { .complete-btn {
@ -79,12 +267,21 @@ h3 {
border: none; border: none;
border-radius: 5px; border-radius: 5px;
background-color: #88c0d0; background-color: #88c0d0;
color: white; color: #2e3440;
cursor: pointer; cursor: pointer;
} }
.complete-btn.completed { .completed-text {
background-color: #a3be8c; color: #a3be8c;
cursor: not-allowed; font-weight: bold;
}
.error-message {
color: #bf616a;
background-color: #fbe2e5;
padding: 10px;
border-radius: 4px;
margin-bottom: 15px;
text-align: center;
} }
</style> </style>

View File

@ -1,6 +1,9 @@
<template> <template>
<div class="dashboard"> <div class="dashboard">
<div v-if="isAuthenticated && user"> <div v-if="!initialized">
<p>Loading session...</p>
</div>
<div v-else-if="isAuthenticated && user">
<h2>Welcome Back, {{ user.nickname }}!</h2> <h2>Welcome Back, {{ user.nickname }}!</h2>
<p>Your village and habits await.</p> <p>Your village and habits await.</p>
<div class="quick-links"> <div class="quick-links">
@ -8,21 +11,19 @@
<NuxtLink to="/habits">Check Habits</NuxtLink> <NuxtLink to="/habits">Check Habits</NuxtLink>
</div> </div>
</div> </div>
<!-- Redirect is handled in script setup, this part might not be seen -->
<div v-else> <div v-else>
<!-- You can show a loading indicator here if you want --> <p>Redirecting to login...</p>
<p>Loading...</p>
</div> </div>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
const { user, isAuthenticated } = useAuth(); const { user, isAuthenticated, initialized } = useAuth();
watchEffect(() => { watchEffect(() => {
// Wait until the initial fetch is done and then decide. // Only redirect when the auth state has been initialized and the user is not authenticated.
// isAuthenticated becomes false if the fetch fails (e.g., 401) if (initialized.value && !isAuthenticated.value) {
// or true if it succeeds. We redirect only when we are sure.
if (process.client && isAuthenticated.value === false) {
navigateTo('/login'); navigateTo('/login');
} }
}); });

View File

@ -24,18 +24,23 @@ definePageMeta({
layout: 'login', layout: 'login',
}); });
const { login, loading } = useAuth(); const { login } = useAuth();
const email = ref(''); const email = ref('');
const password = ref(''); const password = ref('');
const error = ref<string | null>(null); const error = ref<string | null>(null);
const loading = ref(false); // This is the local loading state
const handleLogin = async () => { const handleLogin = async () => {
loading.value = true;
error.value = null; error.value = null;
try { try {
await login(email.value, password.value); await login(email.value, password.value);
await navigateTo('/'); // Explicitly navigate on success
} catch (err: any) { } catch (err: any) {
console.error(err); console.error(err);
error.value = err.data?.message || 'Login failed. Please check your credentials.'; error.value = err.data?.message || 'Login failed. Please check your credentials.';
} finally {
loading.value = false;
} }
}; };
</script> </script>