diff --git a/.env b/.env new file mode 100644 index 0000000..2873e2b --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +# API Configuration +VITE_API_BASE_URL=http://localhost:5269 diff --git a/src/pages/DashboardPage.js b/src/pages/DashboardPage.js index f7ec21a..eca570f 100644 --- a/src/pages/DashboardPage.js +++ b/src/pages/DashboardPage.js @@ -5,7 +5,7 @@ export function renderDashboard() { const container = document.createElement('div'); container.className = 'dashboard'; - container.innerHTML = ` + container.innerHTML = /*html*/`

Bienvenido, ${user?.email || 'Usuario'}

diff --git a/src/services/auth.js b/src/services/auth.js index 2395919..8260d07 100644 --- a/src/services/auth.js +++ b/src/services/auth.js @@ -4,27 +4,49 @@ export class Auth { this.isAuthenticated = false; } - login(email, password) { - if (email && password) { - this.currentUser = { email }; + async login(email, password) { + if (!email || !password) return false; + + try { + const response = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/Auth/login`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email, password }) + }); + + if (!response.ok) return false; + + const { token, user } = await response.json(); + if (!token) return false; + + this.currentUser = user || { email }; this.isAuthenticated = true; localStorage.setItem('user', JSON.stringify(this.currentUser)); + localStorage.setItem('token', token); return true; + } catch (error) { + console.error('Login failed', error); + return false; } - return false; } logout() { this.currentUser = null; this.isAuthenticated = false; localStorage.removeItem('user'); + localStorage.removeItem('token'); } checkAuth() { const user = localStorage.getItem('user'); - if (user) { + const token = localStorage.getItem('token'); + + if (user && token) { this.currentUser = JSON.parse(user); this.isAuthenticated = true; + } else { + this.isAuthenticated = false; + this.currentUser = null; } return this.isAuthenticated; } @@ -32,6 +54,10 @@ export class Auth { getUser() { return this.currentUser; } + + getToken() { + return localStorage.getItem('token'); + } } export const auth = new Auth();