Skip to content

Commit

Permalink
token issues
Browse files Browse the repository at this point in the history
what's happening:

- in the text file you input what emial you want to udpate
- after reauthing, a confirmation email is sent to the new email, you click and the link and oyu get an email in ur old email confirming the changes (no need to do anything for this email)
- when you look at the firebase console, you see the changes have been made

- the issue is in backend logs (not necessarily the stuf mentioned in frontend logs)
- when user navigates out of updateprofile and come back to it, then the backend logs show errors bc of revoked token and then frontend discornnexts backend.

- as one could see i tried fooling around with backend code if that'd do anything
  • Loading branch information
jasmineguru committed Dec 10, 2023
1 parent cbbc9e4 commit 5b61e69
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 41 deletions.
15 changes: 15 additions & 0 deletions backend/routes/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,19 @@ def update_user(user_id: str) -> Response:
except CarbonTrackError as e:
abort(code=400, description=f"{e}")

@users.route("/user/update_email/<user_id>", methods=["PATCH"])
@carbon_auth.auth.login_required
def update_user_email(user_id: str) -> Response:
try:
query = {"_id": ObjectId(user_id)}
new_email = request.get_json().get('email', '')
current_user = carbon_auth.auth.current_user()
new_token = FirebaseAPI.refresh_token(current_user.uid)
CarbonTrackDB.users_coll.update_one(query, {'$set': {'email': new_email}})
item = CarbonTrackDB.users_coll.find_one(query)
item = User.from_json(item).to_json()
return jsonify({'user': item})
except CarbonTrackError as e:
abort(code=400, description=f"{e}")


8 changes: 8 additions & 0 deletions backend/utils/FirebaseAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ def get_user(id_token: str) -> Optional[User]:
item = CarbonTrackDB.users_coll.find_one(query)
item = User.from_json(item)
return item
@staticmethod
def refresh_token(user_id):
try:
user = auth.get_user(user_id)
new_id_token = user.refresh_id_tokens()
return new_id_token
except auth.AuthError as e:
raise e
6 changes: 4 additions & 2 deletions frontend/assets/colorConstants.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Colors = {
LIGHTFGREEN: '#E0EEC6',
DARKGREEN: '#243E36',
DARKGREEN: '#10302b',
DARKGREEN2: '#224A3E',
DARKGREEN3: '#2E5C4E',
WHITE: '#fff',
Expand All @@ -14,7 +14,9 @@ const Colors = {
BLUE: 'blue',
TEAL: '#6bcfca',
TRANSLIGHTGREEN: '#c6dbb2',
TRANSGREENBACK: '#07332b'
TRANSLIGHTGREEN2: '#b3c99d',
TRANSGREENBACK: '#07332b',
DARKDARKGREEN: '#031c19',
};

export default Colors;
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"expo-font": "~11.4.0",
"expo-image-picker": "~14.3.2",
"expo-status-bar": "~1.6.0",
"firebase": "^10.5.2",
"firebase": "^10.7.1",
"formik": "^2.4.5",
"mongodb": "^6.3.0",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/APIs/FLASK_API.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import firebaseService from "../utilities/firebase";
const FLASK_LOCAL_ADDRESS: string = "http://100.112.45.130:6050";
const FLASK_LOCAL_ADDRESS: string = "http://192.168.2.12:6050";

// Function to get the Firebase authentication token
const getFirebaseAuthToken = async (): Promise<string | null> => {
Expand Down
32 changes: 31 additions & 1 deletion frontend/src/APIs/UsersAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,35 @@ export const UsersAPI = {
console.error('Temp tip: have you started the backend?: ');
return undefined;
}
}
},
updateUserEmail: async (userId: ObjectId, newEmail: string) => {
try {
// Ensure that firebaseUser is defined before using it
const firebaseUser = await firebaseService.getFirebaseUser();
if (firebaseUser == null) {
// Handle the case when the user is not signed in
console.error('Firebase user is undefined');
return undefined;
}

// Get the refreshed token
const newToken = await firebaseUser.getIdToken(true);

// Make the request to update the email with the new token
const res = await FLASK_HTTPS.patch(
routeName + `/user/update_email/${userId.toHexString()}`,
{ email: newEmail },
{
headers: {
Authorization: `Bearer ${newToken}`,
},
}
);

return res.data.user as User;
} catch (error) {
console.error('UsersAPI(frontend): updateUserEmailError:', error);
return undefined;
}
},
}
35 changes: 18 additions & 17 deletions frontend/src/components/appNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,6 @@ const MainAppTabs = (): JSX.Element => {
),
}}
/>
<Tab.Screen
name="SettingsPage"
component={SettingsScreen}
options={{
title: 'Settings',
headerStyle: { backgroundColor: Colors.DARKGREEN },
headerTintColor: '#fff',
headerTitleStyle: { fontWeight: 'bold' },
tabBarIcon: ({ focused }) => (
<Ionicons
name={'settings'}
size={24}
style={{ color: (focused as boolean) ? Colors.LIGHTFGREEN : Colors.WHITE }}
/>
),
}}
/>
<Tab.Screen
name="YourForms"
component={YourForms}
Expand Down Expand Up @@ -208,8 +191,26 @@ const MainAppTabs = (): JSX.Element => {
/>
),
}}
/>
<Tab.Screen
name="SettingsPage"
component={SettingsScreen}
options={{
title: 'Settings',
headerStyle: { backgroundColor: Colors.DARKGREEN },
headerTintColor: '#fff',
headerTitleStyle: { fontWeight: 'bold' },
tabBarIcon: ({ focused }) => (
<Ionicons
name={'settings'}
size={24}
style={{ color: (focused as boolean) ? Colors.LIGHTFGREEN : Colors.WHITE }}
/>
),
}}
/>
</Tab.Navigator>

);
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const styles = StyleSheet.create({
width: '85%',
height: 56,
borderRadius: 15,
backgroundColor: Colors.TRANSLIGHTGREEN,
backgroundColor: Colors.TRANSLIGHTGREEN2,
marginVertical: 10,
justifyContent: 'center',
},
Expand Down
Loading

0 comments on commit 5b61e69

Please sign in to comment.