-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create logic for new login and signup
- Loading branch information
1 parent
8270e96
commit cde3ba0
Showing
11 changed files
with
399 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// lib/data/repositories/signup_repository_impl.dart | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:domain/entities/user.dart'; | ||
import 'package:domain/repositories_abstract/auth_repository.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
class AuthRepositoryImpl implements AuthRepository { | ||
final http.Client client; | ||
|
||
AuthRepositoryImpl({required this.client}); | ||
|
||
@override | ||
Future<User> signup(String email, String password) async { | ||
final url = Uri.parse('http://localhost:3000/api/users/'); | ||
final response = await client.post( | ||
url, | ||
headers: {'Content-Type': 'application/json'}, | ||
body: jsonEncode({'email': email, 'password': password}), | ||
); | ||
|
||
if (response.statusCode == 200 || response.statusCode == 201) { | ||
return User(email: email); | ||
} else { | ||
throw Exception('Failed to sign up. Status code: ${response.statusCode}'); | ||
} | ||
} | ||
|
||
@override | ||
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 (response.statusCode == 200) { | ||
final data = jsonDecode(response.body); | ||
return data['accessToken']; // Return access token on success | ||
} else { | ||
throw Exception('Login failed'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class User { | ||
final String email; | ||
|
||
User({required this.email}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// lib/domain/repositories/signup_repository.dart | ||
|
||
import '../entities/user.dart'; | ||
|
||
abstract class AuthRepository { | ||
Future<User> signup(String email, String password); | ||
Future<String> login(String email, String password); // New login method | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// lib/domain/usecases/signup_usecase.dart | ||
|
||
import '../entities/user.dart'; | ||
import '../repositories_abstract/auth_repository.dart'; | ||
|
||
class AuthUseCase { | ||
final AuthRepository repository; | ||
|
||
AuthUseCase({required this.repository}); | ||
|
||
Future<User> signup(String email, String password) async { | ||
// Add any business logic or validation here if needed | ||
return await repository.signup(email, password); | ||
} | ||
|
||
Future<String> login(String email, String password) async { | ||
// Add any business logic or validation here if needed | ||
return await repository.login(email, password); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
lib/presentation/more/NewLogin/viewModels/login_view_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// lib/presentation/viewmodels/login_view_model.dart | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:domain/usecases/auth_usecase.dart'; | ||
|
||
class LoginViewModel extends ChangeNotifier { | ||
final AuthUseCase authUseCase; | ||
|
||
LoginViewModel({required this.authUseCase}); | ||
|
||
bool _isLoading = false; | ||
bool get isLoading => _isLoading; | ||
|
||
String? _errorMessage; | ||
String? get errorMessage => _errorMessage; | ||
|
||
Future<String?> login(String email, String password) async { | ||
_isLoading = true; | ||
_errorMessage = null; | ||
notifyListeners(); | ||
|
||
try { | ||
final accessToken = await authUseCase.login(email, password); | ||
return accessToken; // Successful login returns the access token | ||
} catch (e) { | ||
_errorMessage = 'Login failed: ${e.toString()}'; | ||
return null; | ||
} finally { | ||
_isLoading = false; | ||
notifyListeners(); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
lib/presentation/more/NewLogin/viewModels/signup_view_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// lib/presentation/viewmodels/signup_view_model.dart | ||
|
||
import 'package:domain/entities/user.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:domain/usecases/auth_usecase.dart'; | ||
|
||
|
||
class SignupViewModel extends ChangeNotifier { | ||
final AuthUseCase authUseCase; | ||
|
||
SignupViewModel({required this.authUseCase}); | ||
|
||
bool _isLoading = false; | ||
bool get isLoading => _isLoading; | ||
|
||
String? _errorMessage; | ||
String? get errorMessage => _errorMessage; | ||
|
||
Future<User?> signup(String email, String password) async { | ||
_isLoading = true; | ||
_errorMessage = null; | ||
notifyListeners(); | ||
|
||
try { | ||
User user = await authUseCase.signup(email, password); | ||
return user; | ||
} catch (e) { | ||
_errorMessage = 'Signup failed: ${e.toString()}'; | ||
return null; | ||
} finally { | ||
_isLoading = false; | ||
notifyListeners(); | ||
} | ||
} | ||
} |
Oops, something went wrong.