Skip to content

Commit

Permalink
add forget password function (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ida631 authored Nov 6, 2024
1 parent 4f1d09b commit 69fd068
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 408 deletions.
26 changes: 7 additions & 19 deletions data/lib/repositories/auth_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,17 @@ class AuthRepositoryImpl implements AuthRepository {
}
}

Future<String> resetPasswordVerify(String token) async {
final url = Uri.parse(
'http://localhost:3000/api/auth/reset-password-verify/$token');
final response = await client.get(
url,
headers: {'Content-Type': 'application/json'},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
return data['message'];
} else {
throw ServerException(
'Password reset verification failed with status code: ${response.statusCode}');
}
}

Future<String> resetPassword(String token, String newPassword) async {
Future<String> resetPassword(String email, String newPassword, String confirmationCode) async {
final url =
Uri.parse('http://localhost:3000/api/auth/reset-password/$token');
Uri.parse('http://localhost:3000/api/auth/reset-password');
final response = await client.post(
url,
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'password': newPassword}),
body: jsonEncode({
"email": email,
'password': newPassword,
"confirmationCode": confirmationCode
}),
);

if (response.statusCode == 200) {
Expand Down
3 changes: 1 addition & 2 deletions domain/lib/repositories_abstract/auth_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ abstract class AuthRepository {
Future<String> login(String username, String password);
Future<void> logout();
Future<void> forgetPassword(String email);
Future<String> resetPasswordVerify(String token);
Future<String> resetPassword(String token, String newPassword);
Future<String> resetPassword(String email, String newPassword, String confirmationCode);
}
16 changes: 6 additions & 10 deletions domain/lib/usecases/auth_usecase.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// lib/domain/usecases/signup_usecase.dart


import '../entities/user.dart';
import '../repositories_abstract/auth_repository.dart';
import '../repositories_abstract/token_provider.dart';
Expand All @@ -23,27 +24,22 @@ class AuthUseCase {
Future<void> logout() async {
await repository.logout();
tokenProvider.deleteToken();
return;
}

Future<bool> isLoggedIn() async {
final token = await tokenProvider.getToken();
return token != null;
}

// Change return type to Future<String>
Future<void> forgetPassword(String email) async {
// Assuming repository.forgetPassword returns a reset token
return await repository.forgetPassword(email);
}

Future<String> resetPasswordVerify(String token) async {
return await repository.resetPasswordVerify(token);
}

Future<String> resetPassword(String token, String newPassword) async {
return await repository.resetPassword(token, newPassword);
Future<String> resetPassword(email, newPassword, confirmationCode) async {
return await repository.resetPassword(email, newPassword, confirmationCode);
}

confirmPasswordReset(String email, String newPassword, String confirmationCode) {}
}


}
2 changes: 0 additions & 2 deletions lib/injection_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'package:swiftcomp/presentation/settings/providers/feature_flag_provider.
import 'package:swiftcomp/presentation/settings/viewModels/feature_flag_view_model.dart';
import 'package:swiftcomp/presentation/settings/viewModels/forget_password_view_model.dart';
import 'package:swiftcomp/presentation/settings/viewModels/login_view_model.dart';
import 'package:swiftcomp/presentation/settings/viewModels/reset_password_view_model.dart';
import 'package:swiftcomp/presentation/settings/viewModels/settings_view_model.dart';
import 'package:swiftcomp/presentation/settings/viewModels/signup_view_model.dart';
import 'package:swiftcomp/presentation/settings/viewModels/user_profile_view_model.dart';
Expand All @@ -44,7 +43,6 @@ void initInjection() {
() => FeatureFlagViewModel(featureFlagProvider: sl()));
sl.registerFactory<UserProfileViewModel>(() =>
UserProfileViewModel(authUseCase: sl(), userUseCase: sl()));
sl.registerFactory<ResetPasswordViewModel>(() => ResetPasswordViewModel(authUseCase: sl()));
sl.registerFactory<ForgetPasswordViewModel>(() => ForgetPasswordViewModel(authUseCase: sl()));

// Providers
Expand Down
57 changes: 0 additions & 57 deletions lib/presentation/bottom_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:swiftcomp/presentation/chat/viewModels/chat_view_model.dart';
import 'package:swiftcomp/presentation/settings/providers/feature_flag_provider.dart';
import 'package:swiftcomp/presentation/settings/views/reset_password_page.dart';
import 'package:swiftcomp/presentation/tools/page/tool_page.dart';

import 'chat/views/chat_screen.dart';
Expand All @@ -27,16 +26,6 @@ class _BottomNavigatorState extends State<BottomNavigator> {
final _activeColor = Colors.green;
int _currentIndex = 0;

@override
void initState() {
super.initState();
if (kIsWeb) {
_handleWebLink();
} else {
_setupAppLinks();
}
}

@override
Widget build(BuildContext context) {
final chatViewModel = Provider.of<ChatViewModel>(context);
Expand Down Expand Up @@ -91,50 +80,4 @@ class _BottomNavigatorState extends State<BottomNavigator> {
),
label: title);
}

void _handleWebLink() {
final uri = Uri.base;
final pathSegments = uri.pathSegments;
if (uri.host == 'compositesai.com' || uri.host == 'localhost') {
if (uri.pathSegments.isNotEmpty && pathSegments[0] == 'reset-password') {
final token = pathSegments[1];
WidgetsBinding.instance.addPostFrameCallback((_) {
_navigateToResetPasswordPage(token);
});
}
}
}

void _setupAppLinks() async {
_appLinks = AppLinks();

_appLinks.getInitialLink().then((uri) {
if (uri != null) {
print(uri);
}
});

_appLinks.uriLinkStream.listen((uri) {
print(uri);
final token =
uri.pathSegments.length > 1 && uri.pathSegments[0] == 'reset-password'
? uri.pathSegments[1]
: uri.queryParameters['token'];
print(token);
if (token != null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_navigateToResetPasswordPage(token);
});
}
});
}

void _navigateToResetPasswordPage(String token) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ResetPasswordPage(token: token),
),
);
}
}
18 changes: 17 additions & 1 deletion lib/presentation/settings/login/old_login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ class _LoginPageState extends State<LoginPage> {
checkInput();
},
),

if (password != null && password!.length < 6)
Padding(
padding: EdgeInsets.only(left: 20, right: 20, top: 10),
child: Text(
'Password should be at least 6 characters',
style: TextStyle(color: Colors.red, fontSize: 14),
),
),

Padding(
padding: EdgeInsets.only(left: 20, right: 20, top: 20),
child: LoginButton(
Expand Down Expand Up @@ -92,16 +102,22 @@ class _LoginPageState extends State<LoginPage> {

void checkInput() {
bool enable;
if (isNotEmpty(email) && isNotEmpty(password)) {

if (isNotEmpty(email) && password != null && password!.length >= 6) {
enable = true;
} else {
enable = false;
if (password == null || password!.length < 6) {
print("Password should be at least 6 characters");
}
}

setState(() {
loginEnable = enable;
});
}


void _loginButtonOnPressed(BuildContext context) async {
final progress = ProgressHUD.of(context);
progress?.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ class ForgetPasswordViewModel extends ChangeNotifier {
errorMessage = '';

try {
// Call the auth use case to send the confirmation code to the email
await authUseCase.forgetPassword(email);
isPasswordResetting = true; // Move to the confirm reset stage
isPasswordResetting = true;
} catch (error) {
errorMessage = error.toString();
errorMessage = 'Failed to send confirmation code.';
} finally {
_setLoadingState(false);
}
}

Future<void> confirmPasswordReset(String email, String newPassword, String confirmationCode) async {
Future<void> confirmResetPassword(email, newPassword, confirmCode) async {
_setLoadingState(true);
errorMessage = '';

try {
await authUseCase.confirmPasswordReset(email, newPassword, confirmationCode);
isPasswordResetting = false; // Reset process completed
// Call the auth use case to send the confirmation code to the email
await authUseCase.resetPassword(email, newPassword, confirmCode);

} catch (error) {
errorMessage = error.toString();
errorMessage = 'Failed to send confirmation code.';
} finally {
_setLoadingState(false);
}
Expand Down

This file was deleted.

Loading

0 comments on commit 69fd068

Please sign in to comment.