-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthController.js
More file actions
106 lines (91 loc) · 2.87 KB
/
Copy pathauthController.js
File metadata and controls
106 lines (91 loc) · 2.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
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
const { db } = require('../config/firebase');
const register = async (req, res) => {
try {
const { name, email, password, timezone } = req.body;
// Check if the user already exists
const usersRef = db.collection('users');
const snapshot = await usersRef.where('email', '==', email).get();
if (!snapshot.empty) {
return res.status(400).json({ error: 'User already exists with this email.' });
}
// Hash password
const salt = await bcrypt.genSalt(12);
const passwordHash = await bcrypt.hash(password, salt);
// Create user (new)
const userId = uuidv4();
const userData = {
user_id: userId,
name,
email,
password_hash: passwordHash,
timezone: timezone || 'UTC',
created_at: new Date().toISOString(),
};
await usersRef.doc(userId).set(userData);
// Create default preferences
const prefId = uuidv4();
await db.collection('preferences').doc(prefId).set({
preference_id: prefId,
user_id: userId,
work_start_time: '09:00',
work_end_time: '17:00',
break_duration: 15,
focus_level: 'medium',
});
// Generate JWT
const token = jwt.sign(
{ user_id: userId, email },
process.env.JWT_SECRET || 'dev_secret_key',
{ expiresIn: '7d' }
);
res.status(201).json({
message: 'User registered successfully',
token,
user: { user_id: userId, name, email, timezone: userData.timezone },
});
} catch (error) {
console.error('Registration error:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
const login = async (req, res) => {
try {
const { email, password } = req.body;
// Find user by email
const usersRef = db.collection('users');
const snapshot = await usersRef.where('email', '==', email).get();
if (snapshot.empty) {
return res.status(401).json({ error: 'Invalid email or password.' });
}
const userDoc = snapshot.docs[0];
const userData = userDoc.data();
// Verify password
const isMatch = await bcrypt.compare(password, userData.password_hash);
if (!isMatch) {
return res.status(401).json({ error: 'Invalid email or password.' });
}
// Generate JWT
const token = jwt.sign(
{ user_id: userData.user_id, email: userData.email },
process.env.JWT_SECRET || 'dev_secret_key',
{ expiresIn: '7d' }
);
res.json({
message: 'Login successful',
token,
user: {
user_id: userData.user_id,
name: userData.name,
email: userData.email,
timezone: userData.timezone,
},
});
} catch (error) {
console.error('Login error:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
module.exports = { register, login };