Change username field to email in registration and login processes for consistency
Despliegue Automático / desplegar (push) Successful in 40s
Despliegue Automático / desplegar (push) Successful in 40s
This commit is contained in:
+4
-4
@@ -46,7 +46,7 @@ Body:
|
||||
|
||||
```json
|
||||
{
|
||||
"username": "maria",
|
||||
"email": "maria",
|
||||
"password": "123456",
|
||||
"encrypted_master_key": "texto_cifrado_opcional",
|
||||
"deviceName": "Flutter Pixel"
|
||||
@@ -55,7 +55,7 @@ Body:
|
||||
|
||||
Campos:
|
||||
|
||||
- `username`: `string`, obligatorio.
|
||||
- `email`: `string`, obligatorio.
|
||||
- `password`: `string`, obligatorio.
|
||||
- `encrypted_master_key`: `string | null`, opcional.
|
||||
|
||||
@@ -77,7 +77,7 @@ Body:
|
||||
|
||||
```json
|
||||
{
|
||||
"username": "maria",
|
||||
"email": "maria",
|
||||
"password": "123456",
|
||||
"deviceName": "Flutter Pixel"
|
||||
}
|
||||
@@ -85,7 +85,7 @@ Body:
|
||||
|
||||
Campos:
|
||||
|
||||
- `username`: `string`, obligatorio.
|
||||
- `email`: `string`, obligatorio.
|
||||
- `password`: `string`, obligatorio.
|
||||
- `deviceName`: `string`, opcional.
|
||||
|
||||
|
||||
@@ -11,19 +11,19 @@ if (!JWT_SECRET) {
|
||||
const register = async (req, res) => {
|
||||
try {
|
||||
console.log(req.body); // Log completo del body para depuración
|
||||
const { username, password, encrypted_master_key, deviceName } = req.body;
|
||||
const { email, password, encrypted_master_key, deviceName } = req.body;
|
||||
|
||||
if (!username || !password) {
|
||||
if (!email || !password) {
|
||||
return res.status(400).json({ error: 'Usuario y contraseña son obligatorios' });
|
||||
}
|
||||
console.log(`Intentando registrar usuario: ${username}`);
|
||||
console.log(`Intentando registrar usuario: ${email}`);
|
||||
console.log(`Encrypted Master Key recibida: ${encrypted_master_key}`);
|
||||
// Registramos el usuario y emitimos tokens para entrar directamente
|
||||
await authService.register(username, password, encrypted_master_key);
|
||||
await authService.register(email, password, encrypted_master_key);
|
||||
|
||||
// Generamos tokens reutilizando el flujo de login (no hay deviceName en register)
|
||||
const nombreDispositivo = deviceName || req.headers['user-agent'] || 'Dispositivo Desconocido';
|
||||
const { accessToken, refresh_token } = await authService.login(username, password, nombreDispositivo);
|
||||
const { accessToken, refresh_token } = await authService.login(email, password, nombreDispositivo);
|
||||
|
||||
res.status(201).json({
|
||||
message: 'Usuario registrado con éxito',
|
||||
@@ -38,12 +38,12 @@ const register = async (req, res) => {
|
||||
// 2. LOGIN
|
||||
const login = async (req, res) => {
|
||||
try {
|
||||
const { username, password, deviceName } = req.body;
|
||||
const { email, password, deviceName } = req.body;
|
||||
|
||||
// Si Flutter no envía un nombre, intentamos leer el User-Agent
|
||||
const nombreDispositivo = deviceName || req.headers['user-agent'] || 'Dispositivo Desconocido';
|
||||
|
||||
const { encrypted_master_key, accessToken, refresh_token } = await authService.login(username, password, nombreDispositivo);
|
||||
const { encrypted_master_key, accessToken, refresh_token } = await authService.login(email, password, nombreDispositivo);
|
||||
|
||||
res.json({
|
||||
message: 'Login exitoso',
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ const User = sequelize.define('User', {
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
username: {
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
|
||||
@@ -15,19 +15,19 @@ if (!JWT_SECRET) {
|
||||
|
||||
class AuthService {
|
||||
|
||||
async register(username, password, encrypted_master_key = null) {
|
||||
const userExists = await User.findOne({ where: { username } });
|
||||
async register(email, password, encrypted_master_key = null) {
|
||||
const userExists = await User.findOne({ where: { email: email } });
|
||||
if (userExists) throw new Error('El usuario ya existe');
|
||||
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
const hashedPassword = await bcrypt.hash(password, salt);
|
||||
|
||||
return await User.create({ username, password: hashedPassword, encrypted_master_key });
|
||||
return await User.create({ email: email, password: hashedPassword, encrypted_master_key });
|
||||
}
|
||||
|
||||
// LOGIN: Crea el Access Token corto y registra el dispositivo con un Refresh Token único
|
||||
async login(username, password, deviceName) {
|
||||
const user = await User.findOne({ where: { username } });
|
||||
async login(email, password, deviceName) {
|
||||
const user = await User.findOne({ where: { email: email } });
|
||||
if (!user) throw new Error('Credenciales inválidas');
|
||||
|
||||
const isValid = await bcrypt.compare(password, user.password);
|
||||
@@ -35,7 +35,7 @@ class AuthService {
|
||||
|
||||
// A. Generar Access Token (Vence en 15 minutos - Sin datos sensibles)
|
||||
const accessToken = jwt.sign(
|
||||
{ id: user.id, username: user.username },
|
||||
{ id: user.id, email: user.email },
|
||||
JWT_SECRET,
|
||||
{ expiresIn: '15m' }
|
||||
);
|
||||
@@ -69,7 +69,7 @@ class AuthService {
|
||||
|
||||
// 2. Si existe, emitimos un nuevo Access Token de 15 minutos
|
||||
const newAccessToken = jwt.sign(
|
||||
{ id: user.id, username: user.username },
|
||||
{ id: user.id, email: user.email },
|
||||
JWT_SECRET,
|
||||
{ expiresIn: '15m' }
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user