Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-7880] Change Password Screen and Backend Code #8381

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/queries/servers/change_password.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong and makes no real change to the user password, changing the password should be by calling the appropriate API and this is not needed

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {Q} from '@nozbe/watermelondb';

import {MM_TABLES} from '@constants/database';
import DatabaseManager from '@database/manager';

import type UserModel from '@typings/database/models/app/user';

const {APP: {USER}} = MM_TABLES;

/**
* Updates a user's password in the database.
*
* @param userid - The unique identifier of the user.
* @param newPassword - The new password to set for the user.
* @returns A boolean indicating success (true) or failure (false).
*/
export const updatePassword = async (userid: string, newPassword: string): Promise<boolean> => {
try {
const {database} = DatabaseManager.getAppDatabaseAndOperator();
const userCollection = database.get<UserModel>(USER);

// Find the user by their ID
const user = await userCollection.query(
Q.where('id', userid),
).fetch();

if (user.length === 0) {
console.error(`User with ID ${userid} not found.`);
return false;
}

// Update the password field for the user
await database.write(async () => {
await user[0].update((u) => {
u.password = newPassword;
});
});

return true;
} catch (error) {
console.error('Failed to update password:', error);
return false;
}
};
129 changes: 129 additions & 0 deletions app/screens/change_password/index.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we get to this screen?

Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import React, { useState } from 'react';
import {
View,
Text,
TextInput,
TouchableOpacity,
StyleSheet,
Alert,
ActivityIndicator,
} from 'react-native';

const ChangePasswordScreen = () => {
const [currentPassword, setCurrentPassword] = useState('');
const [newPassword, setNewPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [loading, setLoading] = useState(false);

const handleChangePassword = async () => {
if (!currentPassword || !newPassword || !confirmPassword) {
Alert.alert('Error', 'Please fill in all fields.');
return;
}

if (newPassword !== confirmPassword) {
Alert.alert('Error', 'New password and confirmation do not match.');
return;
}

setLoading(true);
try {
// Example API call to change password
const response = await fetch('https://mattermost.com/user/change-password', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
current_password: currentPassword,
new_password: newPassword,
}),
});

const data = await response.json();

if (response.ok) {
Alert.alert('Success', 'Password changed successfully!');
setCurrentPassword('');
setNewPassword('');
setConfirmPassword('');
} else {
Alert.alert('Error', data.message || 'An error occurred.');
}
} catch (error) {
Alert.alert('Error', 'Failed to change password. Please try again.');
} finally {
setLoading(false);
}
};

return (
<View style={styles.container}>
<Text style={styles.title}>Change Password</Text>
<TextInput
style={styles.input}
placeholder="Current Password"
secureTextEntry
value={currentPassword}
onChangeText={setCurrentPassword}
/>
<TextInput
style={styles.input}
placeholder="New Password"
secureTextEntry
value={newPassword}
onChangeText={setNewPassword}
/>
<TextInput
style={styles.input}
placeholder="Confirm New Password"
secureTextEntry
value={confirmPassword}
onChangeText={setConfirmPassword}
/>
{loading ? (
<ActivityIndicator size="large" color="#007AFF" />
) : (
<TouchableOpacity style={styles.button} onPress={handleChangePassword}>
<Text style={styles.buttonText}>Change Password</Text>
</TouchableOpacity>
)}
</View>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#F9F9F9',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
textAlign: 'center',
},
input: {
height: 50,
borderColor: '#CCC',
borderWidth: 1,
borderRadius: 8,
paddingHorizontal: 10,
marginBottom: 15,
backgroundColor: '#FFF',
},
button: {
backgroundColor: '#007AFF',
paddingVertical: 15,
borderRadius: 8,
alignItems: 'center',
},
buttonText: {
color: '#FFF',
fontSize: 16,
fontWeight: 'bold',
},
});

export default ChangePasswordScreen;