-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
135 lines (111 loc) · 3.87 KB
/
Copy pathauth.js
File metadata and controls
135 lines (111 loc) · 3.87 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
/**
* auth.js
* * This file contains two main parts:
* 1. AuthService Class: Handles all network requests (register, login) to the API.
* 2. Session Management & Guard Logic: Manages the token/user in localStorage.
*/
// ==========================================================
// PART 1: AUTHENTICATION API SERVICE (Network Requests)
// ==========================================================
class AuthService {
constructor() {
// --- THIS IS THE FIX ---
// Change the URL to point to your local server
this.baseUrl = 'http://localhost:9000/api';
}
/**
* Handles the user registration process.
*/
async register(fullName, email, password) {
const url = `${this.baseUrl}/auth/register`;
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
fullName,
email,
password
}),
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.message || 'Registration failed due to server error.');
}
return data;
} catch (error) {
console.error('AuthService Error during registration:', error);
throw error;
}
}
/**
* Handles the user login process.
*/
async login(email, password) {
const url = `${this.baseUrl}/auth/login`;
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
password
}),
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.message || 'Login failed. Please check your credentials.');
}
// This logic is now correct from our previous fix
if (data.token) {
localStorage.setItem('smartproject_token', data.token);
const userToSave = {
_id: data._id,
name: data.name,
email: data.email,
skills: data.skills,
profilePicture: data.profilePicture,
headline: data.headline
};
localStorage.setItem('smartproject_user', JSON.stringify(userToSave));
}
return data;
} catch (error) {
console.error('AuthService Error during login:', error);
throw error;
}
}
}
// Instantiate the service for global use
const authService = new AuthService();
// ==========================================================
// PART 2: SESSION MANAGEMENT & GUARD LOGIC (Client-Side)
// ==========================================================
// This part is all correct and needs no changes
const token = localStorage.getItem('smartproject_token');
const protectedPages = [
'dashboard.html',
'my-projects.html',
'profile-settings.html',
'tasks.html',
'chat.html',
'notifications.html',
'project-detail.html',
'view-profile.html'
];
const isProtectedPage = protectedPages.some(page => window.location.pathname.includes(page));
if (isProtectedPage) {
if (!token) {
window.location.href = 'login.html';
}
}
const user = JSON.parse(localStorage.getItem('smartproject_user'));
function logout() {
localStorage.removeItem('smartproject_token');
localStorage.removeItem('smartproject_user');
window.location.href = 'login.html';
}