frontend: request new activation code

Pedro Lucas Porcellis porcellis@eletrotupi.com 16 days ago 4e8e478f6d7cd313e333b3ca6793334f4c19c905
Parents: 10dda3b
4 file(s) changed
  • frontend/app/auth/activate.tsx +8 -2
  • frontend/app/profile.tsx +0 -2
  • frontend/context/AuthContext.tsx +11 -17
  • frontend/lib/api/client.ts +6 -0
frontend/app/auth/activate.tsx
@@ -22,7 +22,7 @@ const [errors, setErrors] = useState<{
22 22 code?: string;
23 23 }>({});
24 24
25 - const { activate } = useAuth();
25 + const { activate, requestActivateCode } = useAuth();
26 26 const [loading, setLoading] = useState(false);
27 27 const textColor = useThemeColor({}, 'text');
28 28 const backgroundColor = useThemeColor({}, 'background');
@@ -63,6 +63,12 @@ }
63 63 };
64 64
65 65 const resendCode = async () => {
66 + await requestActivateCode()
67 +
68 + Alert.alert(
69 + "Novo código",
70 + "Enviado novo código, verifique sua caixa de entrada"
71 + );
66 72 }
67 73
68 74 return (
@@ -106,7 +112,7 @@ <Text style={[styles.footerText, { color: textColor, opacity: 0.7 }]}>
106 112 Não recebeu?{' '}
107 113 </Text>
108 114
109 - <Button variant="outline" title="Reenviar um novo código" onPressed={resendCode} />
115 + <Button variant="outline" title="Reenviar um novo código" onPress={resendCode} />
110 116 </View>
111 117 </ScrollView>
112 118 </KeyboardAvoidingView>
frontend/app/profile.tsx
@@ -40,8 +40,6 @@ useEffect(() => {
40 40 setFirstName(user.firstName)
41 41 setLastName(user.lastName)
42 42 setEmail(user.email)
43 -
44 - console.log("User@profile", user);
45 43 }, [user])
46 44
47 45 const backgroundColor = useThemeColor({}, 'background');
frontend/context/AuthContext.tsx
@@ -10,15 +10,7 @@ interface AuthContextType {
10 10 user: User | null;
11 11 isLoading: boolean;
12 12 isAuthenticated: boolean;
13 - updateAuthUser: (user: {
14 - firstName: string;
15 - lastName?: string;
16 - email: string;
17 - updatedAt: date;
18 - avatarURL?: string;
19 - avatarKey?: string;
20 - active: boolean;
21 - }) => void;
13 + updateAuthUser: (user: User) => void;
22 14 login: (email: string, password: string) => Promise<void>;
23 15 signup: (userData: {
24 16 firstName: string;
@@ -30,6 +22,7 @@ logout: () => Promise<void>;
30 22 forgotPassword: (email: string) => Promise<{ message: string; token?: string }>;
31 23 resetPassword: (token: string, password: string) => Promise<void>;
32 24 activate: (code: string) => Promise<void>;
25 + requestActivateCode: () => Promise<void>;
33 26 }
34 27
35 28 export function sanitizeUser(data: any): User {
@@ -78,11 +71,7 @@ // For now, we'll create a basic user object
78 71 setUser({
79 72 id: verifyResponse.userId,
80 73 email: verifyResponse.email,
81 - firstName: verifyResponse.user.firstName,
82 - lastName: verifyResponse.user.lastName,
83 - avatarURL: verifyResponse.user.avatarURL,
84 - avatarKey: verifyResponse.user.avatarKey,
85 - active: verifyResponse.user.active
74 + ...verifyResponse.user
86 75 });
87 76 }
88 77 }
@@ -147,14 +136,18 @@ const activate = async (code: number) => {
147 136 const response = await apiClient.activate(code);
148 137
149 138 if (response.user) {
150 - updateAuthUser(response.user)
139 + setUser(response.user)
151 140 }
152 141
153 142 return response
143 + }
144 +
145 + const requestActivateCode = async() => {
146 + await apiClient.requestActivateCode();
154 147 }
155 148
156 149 const updateAuthUser = (user) => {
157 - setUser(sanitizeUser(user));
150 + setUser(user);
158 151 }
159 152
160 153 const value: AuthContextType = {
@@ -167,7 +160,8 @@ logout,
167 160 forgotPassword,
168 161 resetPassword,
169 162 updateAuthUser,
170 - activate
163 + activate,
164 + requestActivateCode
171 165 };
172 166
173 167 return (
frontend/lib/api/client.ts
@@ -100,6 +100,12 @@ body: JSON.stringify({ code }),
100 100 });
101 101 }
102 102
103 + async requestActivateCode(): Promise<void> {
104 + return this.request('/auth/resend_code', {
105 + method: 'POST'
106 + })
107 + }
108 +
103 109 async signup(user: SignUpPayload): Promise<AuthResponse> {
104 110 const response = await this.request<AuthResponse>('/users', {
105 111 method: 'POST',