eletrotupi / tcc/ commit / 316a81f

frontend: add sanitizer to internal user & clean up logs

This sanitizer is kind of dumb, but, it'll do for now
Pedro Lucas Porcellis porcellis@eletrotupi.com · 5 months ago 316a81f611bd540e01543b7804761dc5348f1352 Browse files
Parents ba96371
Changed files
1
Additions
+18
Deletions
-6
frontend/context/AuthContext.tsx
1 1
import React, { createContext, useContext, useEffect, useState } from 'react';
2 2
import { apiClient, User } from '@/lib/api';
3 3

4 4
interface AuthProviderProps {
5 5
  children: React.ReactNode;
12 12
  updateAuthUser: (user: {
13 13
    firstName: string;
14 14
    lastName?: string;
15 15
    email: string;
16 16
    updatedAt: date;
17 +
    avatarURL?: string;
18 +
    avatarKey?: string;
17 19
  }) => void;
18 20
  login: (email: string, password: string) => Promise<void>;
19 21
  signup: (userData: {
20 22
    firstName: string;
21 23
    lastName?: string;
25 27
  logout: () => Promise<void>;
26 28
  forgotPassword: (email: string) => Promise<{ message: string; token?: string }>;
27 29
  resetPassword: (token: string, password: string) => Promise<void>;
28 30
}
29 31

32 +

33 +
export function sanitizeUser(data: any): User {
34 +
  return {
35 +
    id: data.id,
36 +
    email: data.email,
37 +
    firstName: data.firstName,
38 +
    lastName: data.lastName,
39 +
    updatedAt: data.updatedAt,
40 +
    avatarKey: data.avatarKey,
41 +
    avatarURL: data.avatarURL,
42 +
  };
43 +
}
44 +

30 45
const AuthContext = createContext<AuthContextType | null>(null);
31 46

32 47
export function useAuth() {
33 48
  const context = useContext(AuthContext);
34 49
  if (!context) {
39 54
}
40 55

41 56
export const AuthProvider = ({ children }: AuthProviderProps) => {
42 57
  const [user, setUser] = useState<User | null>(null);
43 58
  const [isLoading, setIsLoading] = useState(true);
44 -

45 -
  console.log("user & auth", isAuthenticated, user)
46 59
  const isAuthenticated = !!user;
47 -
  console.log("after user & auth", isAuthenticated, user)
48 60

49 61
  // Check for existing authentication on app start
50 62
  useEffect(() => {
51 63
    checkAuthState();
52 64
  }, []);
53 65

54 66
  const checkAuthState = async () => {
55 67
    try {
56 68
      const { token } = await apiClient.getStoredAuthData();
57 -
      console.log("We have a token?", token);
58 69

59 70
      if (token) {
60 71
        const verifyResponse = await apiClient.verifyToken();
61 -
        console.log("Checking response: ", verifyResponse)
62 72
        if (verifyResponse.valid && verifyResponse.userId && verifyResponse.email) {
63 73
          // TODO: Fetch the full user profile
64 74
          // For now, we'll create a basic user object
65 75
          setUser({
66 76
            id: verifyResponse.userId,
67 77
            email: verifyResponse.email,
68 78
            firstName: verifyResponse.user.firstName,
69 79
            lastName: verifyResponse.user.lastName,
80 +
            avatarURL: verifyResponse.user.avatarURL,
81 +
            avatarKey: verifyResponse.user.avatarKey
70 82
          });
71 83
        }
72 84
      }
73 85
    } catch (error) {
74 86
      console.error('Auth check failed:', error);
122 134
  const resetPassword = async (token: string, password: string) => {
123 135
    await apiClient.resetPassword(token, password);
124 136
  };
125 137

126 138
  const updateAuthUser = (user) => {
127 -
    setUser(user);
139 +
    setUser(sanitizeUser(user));
128 140
  }
129 141

130 142
  const value: AuthContextType = {
131 143
    user,
132 144
    isLoading,
143 155
    <AuthContext.Provider value={value}>
144 156
      {children}
145 157
    </AuthContext.Provider>
146 158
  );
147 159
}