Skip to content

Commit

Permalink
modify signup page (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ida631 authored Nov 9, 2024
1 parent fe06202 commit 41512c6
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 112 deletions.
12 changes: 6 additions & 6 deletions data/lib/repositories/auth_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ class AuthRepositoryImpl implements AuthRepository {
AuthRepositoryImpl({required this.client, required this.authClient});

@override
Future<User> signup(String username, String email, String password) async {
Future<User> signup(String email, String password, {String? name}) async {
final url = Uri.parse('http://localhost:3000/api/users/');
final response = await client.post(
url,
headers: {'Content-Type': 'application/json'},
body: jsonEncode(
{'username': username, 'email': email, 'password': password}),
{'email': email, 'password': password,
if (name != null) 'name': name,}),
);

if (response.statusCode == 200 || response.statusCode == 201) {
return User(username: username, email: email);
return User(email: email, name: name,);
} else {
throw Exception('Failed to sign up. Status code: ${response.statusCode}');
}
}

@override
Future<String> login(String email, String password, {String? nickname}) async {
Future<String> login(String email, String password) async {
final url = Uri.parse('http://localhost:3000/api/auth/login');
final response = await client.post(
url,
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'email': email, 'password': password,
if (nickname != null) 'nickname': nickname,}),
body: jsonEncode({'email': email, 'password': password,}),
);

if (response.statusCode == 200) {
Expand Down
4 changes: 2 additions & 2 deletions domain/lib/entities/user.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class User {
final String username;
final String? username;
final String email;
String? name;
final String? description;
final String? avatarUrl;

User(
{required this.username,
{this.username,
required this.email,
this.name,
this.description,
Expand Down
4 changes: 2 additions & 2 deletions domain/lib/repositories_abstract/auth_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import '../entities/user.dart';

abstract class AuthRepository {
Future<User> signup(String username, String email, String password);
Future<String> login(String email, String password, {String? nickname});
Future<User> signup(String email, String password, {String? name});
Future<String> login(String email, String password);
Future<void> logout();
Future<void> forgetPassword(String email);
Future<String> resetPassword(String email, String newPassword, String confirmationCode);
Expand Down
8 changes: 4 additions & 4 deletions domain/lib/usecases/auth_usecase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class AuthUseCase {

AuthUseCase({required this.repository, required this.tokenProvider});

Future<User> signup(String username, String email, String password) async {
return await repository.signup(username, email, password);
Future<User> signup(String email, String password, {String? name}) async {
return await repository.signup(email, password, name: name);
}

Future<String> login(String email, String password, {String? nickname}) async {
String accessToken = await repository.login(email, password, nickname: nickname);
Future<String> login(String email, String password) async {
String accessToken = await repository.login(email, password);
await tokenProvider.saveToken(accessToken);
return accessToken;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/presentation/settings/viewModels/login_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class LoginViewModel extends ChangeNotifier {
notifyListeners();
}

Future<String?> login(String email,String password, {String? nickname}) async {
Future<String?> login(String email,String password) async {
_isLoading = true;
_errorMessage = null;
notifyListeners();

try {
final accessToken = await authUseCase.login(email, password, nickname: nickname);
final accessToken = await authUseCase.login(email, password);
return accessToken; // Successful login returns the access token
} catch (e) {
_errorMessage = 'Login failed: ${e.toString()}';
Expand Down
18 changes: 16 additions & 2 deletions lib/presentation/settings/viewModels/signup_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import 'package:domain/usecases/auth_usecase.dart';
class SignupViewModel extends ChangeNotifier {
final AuthUseCase authUseCase;

bool obscureTextNewPassword = true;
bool obscureTextConfirmPassword = true;

void toggleNewPasswordVisibility() {
obscureTextNewPassword = !obscureTextNewPassword;
notifyListeners(); // Notify the UI about the change
}

void toggleConfirmPasswordVisibility() {
obscureTextConfirmPassword = !obscureTextConfirmPassword;
notifyListeners(); // Notify the UI about the change
}


SignupViewModel({required this.authUseCase});

bool _isLoading = false;
Expand All @@ -16,13 +30,13 @@ class SignupViewModel extends ChangeNotifier {
String? _errorMessage;
String? get errorMessage => _errorMessage;

Future<User?> signup(String username, String email, String password) async {
Future<User?> signup(String email, String password, {String? name}) async {
_isLoading = true;
_errorMessage = null;
notifyListeners();

try {
User user = await authUseCase.signup(username, email, password);
User user = await authUseCase.signup(email, password, name: name);
return user;
} catch (e) {
_errorMessage = 'Signup failed: ${e.toString()}';
Expand Down
22 changes: 3 additions & 19 deletions lib/presentation/settings/views/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ class _LoginPageState extends State<NewLoginPage> {
final _formKey = GlobalKey<FormState>();
String email = '';
String password = '';
String nickname = '';
bool isLoading = false;
bool isButtonEnabled = false;
bool isPasswordValid = false;
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _nicknameController = TextEditingController();



@override
Expand All @@ -51,9 +50,8 @@ class _LoginPageState extends State<NewLoginPage> {

try {
// Call login from viewModel and pass the credentials
nickname = _nicknameController.text.trim();
final accessToken = await viewModel.login(_emailController.text, _passwordController.text,
nickname: nickname.isNotEmpty ? nickname : null,);

final accessToken = await viewModel.login(_emailController.text, _passwordController.text,);

if (accessToken != null) {
// Login successful
Expand Down Expand Up @@ -148,20 +146,6 @@ class _LoginPageState extends State<NewLoginPage> {
),
SizedBox(height: 16.0),

TextFormField(
controller: _nicknameController,
decoration: InputDecoration(
labelText: 'Nickname (optional)',
hintText: 'Enter your nickname', // Optional hint text
border: UnderlineInputBorder(),
),
style: TextStyle(color: Colors.black), // Text color when typing
obscureText: false,
onChanged: (value) => nickname = value.trim(),
),
SizedBox(height: 16.0),


// Password Field
TextFormField(
controller: _passwordController,
Expand Down
Loading

0 comments on commit 41512c6

Please sign in to comment.