forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthController.js
More file actions
164 lines (144 loc) 路 3.73 KB
/
Copy pathauthController.js
File metadata and controls
164 lines (144 loc) 路 3.73 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const User = require('../models/User');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
// Registrazione utente
exports.register = async (req, res) => {
try {
const { username, email, password, moneroWallet } = req.body;
// Validazione base
if (!username || !email || !password) {
return res.status(400).json({
success: false,
message: 'Username, email e password sono obbligatori'
});
}
// Verifica se l'utente esiste gi脿
const existingUser = await User.findOne({
$or: [{ email }, { username }]
});
if (existingUser) {
return res.status(400).json({
success: false,
message: 'Username o email gi脿 in uso'
});
}
// Crea nuovo utente
const user = new User({
username,
email,
password,
moneroWallet: moneroWallet || null
});
await user.save();
// Genera JWT token
const token = jwt.sign(
{ userId: user._id, username: user.username, role: user.role },
process.env.JWT_SECRET,
{ expiresIn: process.env.JWT_EXPIRES_IN || '7d' }
);
res.status(201).json({
success: true,
message: 'Utente registrato con successo',
data: {
user: {
id: user._id,
username: user.username,
email: user.email,
role: user.role,
moneroWallet: user.moneroWallet,
createdAt: user.createdAt
},
token
}
});
} catch (error) {
console.error('Registration error:', error);
res.status(500).json({
success: false,
message: 'Errore durante la registrazione',
error: error.message
});
}
};
// Login utente
exports.login = async (req, res) => {
try {
const { email, password } = req.body;
if (!email || !password) {
return res.status(400).json({
success: false,
message: 'Email e password sono obbligatori'
});
}
// Trova l'utente
const user = await User.findOne({ email });
if (!user) {
return res.status(401).json({
success: false,
message: 'Credenziali non valide'
});
}
// Verifica password
const isPasswordValid = await user.comparePassword(password);
if (!isPasswordValid) {
return res.status(401).json({
success: false,
message: 'Credenziali non valide'
});
}
// Aggiorna ultimo login
user.lastLogin = new Date();
await user.save();
// Genera JWT token
const token = jwt.sign(
{ userId: user._id, username: user.username, role: user.role },
process.env.JWT_SECRET,
{ expiresIn: process.env.JWT_EXPIRES_IN || '7d' }
);
res.json({
success: true,
message: 'Login effettuato con successo',
data: {
user: {
id: user._id,
username: user.username,
email: user.email,
role: user.role,
moneroWallet: user.moneroWallet,
lastLogin: user.lastLogin
},
token
}
});
} catch (error) {
console.error('Login error:', error);
res.status(500).json({
success: false,
message: 'Errore durante il login',
error: error.message
});
}
};
// Ottieni profilo utente
exports.getProfile = async (req, res) => {
try {
const user = await User.findById(req.userId).select('-password');
if (!user) {
return res.status(404).json({
success: false,
message: 'Utente non trovato'
});
}
res.json({
success: true,
data: { user }
});
} catch (error) {
console.error('Profile error:', error);
res.status(500).json({
success: false,
message: 'Errore durante il recupero del profilo',
error: error.message
});
}
};