Skip to content

Commit

Permalink
Create logic for new login and signup
Browse files Browse the repository at this point in the history
  • Loading branch information
banghuazhao committed Oct 27, 2024
1 parent 8270e96 commit cde3ba0
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 86 deletions.
46 changes: 46 additions & 0 deletions data/lib/repositories/auth_repository.dart
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');
}
}
}
5 changes: 5 additions & 0 deletions domain/lib/entities/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class User {
final String email;

User({required this.email});
}
8 changes: 8 additions & 0 deletions domain/lib/repositories_abstract/auth_repository.dart
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
}
20 changes: 20 additions & 0 deletions domain/lib/usecases/auth_usecase.dart
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);
}
}
13 changes: 13 additions & 0 deletions lib/injection_container.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import 'package:data/data_sources/function_tools_data_source.dart';
import 'package:data/data_sources/open_ai_data_source.dart';
import 'package:data/repositories/auth_repository.dart';
import 'package:data/repositories/chat_repository_impl.dart';
import 'package:data/repositories/chat_session_repository_imp.dart';
import 'package:domain/domain.dart';
import 'package:domain/repositories_abstract/auth_repository.dart';
import 'package:domain/usecases/auth_usecase.dart';
import 'package:domain/usecases/function_tools_usecase.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart' as http;
import 'package:swiftcomp/presentation/more/NewLogin/viewModels/login_view_model.dart';
import 'package:swiftcomp/presentation/more/NewLogin/viewModels/signup_view_model.dart';
import 'presentation/chat/viewModels/chat_view_model.dart';

final sl = GetIt.instance;
Expand All @@ -14,6 +19,10 @@ void initInjection() {
// ViewModels
sl.registerFactory<ChatViewModel>(() => ChatViewModel(
chatUseCase: sl(), chatSessionUseCase: sl(), functionToolsUseCase: sl()));
sl.registerFactory<LoginViewModel>(() => LoginViewModel(authUseCase: sl()));
sl.registerFactory<SignupViewModel>(() => SignupViewModel(
authUseCase: sl()));


// Use Cases
sl.registerLazySingleton<ChatUseCase>(
Expand All @@ -24,11 +33,15 @@ void initInjection() {

sl.registerLazySingleton<FunctionToolsUseCase>(() => FunctionToolsUseCase());

sl.registerLazySingleton<AuthUseCase>(() => AuthUseCase(repository: sl()));

// Repositories
sl.registerLazySingleton<ChatRepository>(() =>
ChatRepositoryImp(openAIDataSource: sl(), functionToolsDataSource: sl()));
sl.registerLazySingleton<ChatSessionRepository>(
() => ChatSessionRepositoryImpl());
sl.registerLazySingleton<AuthRepository>(
() => AuthRepositoryImpl(client: sl()));

// Data Sources
sl.registerLazySingleton<OpenAIDataSource>(
Expand Down
85 changes: 0 additions & 85 deletions lib/presentation/more/NewLogin/new_login.dart

This file was deleted.

33 changes: 33 additions & 0 deletions lib/presentation/more/NewLogin/viewModels/login_view_model.dart
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 lib/presentation/more/NewLogin/viewModels/signup_view_model.dart
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();
}
}
}
Loading

0 comments on commit cde3ba0

Please sign in to comment.