File size: 2,902 Bytes
f853e83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  changeMyPassword,
  createAccount,
  getAuthStats,
  getMyProfile,
  loginAccount,
  refreshSession,
  updateMyProfile,
} from "../services/authService.js";

export const signup = async (req, res) => {
  try {
    const result = await createAccount(req.body || {});
    res.status(201).json({
      message: "Account created successfully",
      ...result,
    });
  } catch (error) {
    res.status(400).json({
      error: "Failed to create account",
      message: error.message,
    });
  }
};

export const login = async (req, res) => {
  try {
    const result = await loginAccount(req.body || {});
    res.json({
      message: "Login successful",
      ...result,
    });
  } catch (error) {
    res.status(400).json({
      error: "Failed to login",
      message: error.message,
    });
  }
};

export const refresh = async (req, res) => {
  try {
    const token = await refreshSession(req.body || {});
    res.json({ token });
  } catch (error) {
    res.status(401).json({ error: "Session expired", message: error.message });
  }
};

export const stats = async (req, res) => {
  try {
    const result = await getAuthStats();
    res.json(result);
  } catch (error) {
    res.status(500).json({
      error: "Failed to load stats",
      message: error.message,
    });
  }
};

export const me = async (req, res) => {
  try {
    const userUid = req.user?.uid;
    if (!userUid) {
      return res.status(401).json({ error: "Unauthorized", message: "Missing user context" });
    }

    const user = await getMyProfile({ userUid });
    res.json({ user });
  } catch (error) {
    res.status(400).json({
      error: "Failed to load profile",
      message: error.message,
    });
  }
};

export const updateProfile = async (req, res) => {
  try {
    const userUid = req.user?.uid;
    if (!userUid) {
      return res.status(401).json({ error: "Unauthorized", message: "Missing user context" });
    }

    const user = await updateMyProfile({
      userUid,
      fullName: req.body?.fullName,
    });

    res.json({
      message: "Profile updated successfully",
      user,
    });
  } catch (error) {
    res.status(400).json({
      error: "Failed to update profile",
      message: error.message,
    });
  }
};

export const changePassword = async (req, res) => {
  try {
    const userUid = req.user?.uid;
    if (!userUid) {
      return res.status(401).json({ error: "Unauthorized", message: "Missing user context" });
    }

    const result = await changeMyPassword({
      userUid,
      currentPassword: req.body?.currentPassword,
      newPassword: req.body?.newPassword,
    });

    res.json(result);
  } catch (error) {
    res.status(400).json({
      error: "Failed to change password",
      message: error.message,
    });
  }
};