From bfec6ffd7d11961b7cde81fbeccebab4d260fac0 Mon Sep 17 00:00:00 2001 From: qzl Date: Wed, 25 Feb 2026 14:45:08 +0800 Subject: [PATCH] feat(apps): add auth data models --- .../auth/data/models/auth_response.dart | 56 +++++++++++++++++++ .../auth/data/models/login_request.dart | 24 ++++++++ .../auth/data/models/signup_request.dart | 34 +++++++++++ .../auth/data/models/auth_models_test.dart | 56 +++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 apps/lib/features/auth/data/models/auth_response.dart create mode 100644 apps/lib/features/auth/data/models/login_request.dart create mode 100644 apps/lib/features/auth/data/models/signup_request.dart create mode 100644 apps/test/features/auth/data/models/auth_models_test.dart diff --git a/apps/lib/features/auth/data/models/auth_response.dart b/apps/lib/features/auth/data/models/auth_response.dart new file mode 100644 index 0000000..b2dd3c5 --- /dev/null +++ b/apps/lib/features/auth/data/models/auth_response.dart @@ -0,0 +1,56 @@ +class AuthUser { + final String id; + final String email; + + const AuthUser({required this.id, required this.email}); + + factory AuthUser.fromJson(Map json) { + return AuthUser(id: json['id'] as String, email: json['email'] as String); + } +} + +class AuthResponse { + final String accessToken; + final String refreshToken; + final int expiresIn; + final String tokenType; + final AuthUser user; + + const AuthResponse({ + required this.accessToken, + required this.refreshToken, + required this.expiresIn, + required this.tokenType, + required this.user, + }); + + factory AuthResponse.fromJson(Map json) { + return AuthResponse( + accessToken: json['access_token'] as String, + refreshToken: json['refresh_token'] as String, + expiresIn: json['expires_in'] as int, + tokenType: json['token_type'] as String, + user: AuthUser.fromJson(json['user'] as Map), + ); + } +} + +class SignupStartResponse { + final String status; + final String email; + final String message; + + const SignupStartResponse({ + required this.status, + required this.email, + required this.message, + }); + + factory SignupStartResponse.fromJson(Map json) { + return SignupStartResponse( + status: json['status'] as String, + email: json['email'] as String, + message: json['message'] as String, + ); + } +} diff --git a/apps/lib/features/auth/data/models/login_request.dart b/apps/lib/features/auth/data/models/login_request.dart new file mode 100644 index 0000000..8d772a2 --- /dev/null +++ b/apps/lib/features/auth/data/models/login_request.dart @@ -0,0 +1,24 @@ +class LoginRequest { + final String email; + final String password; + + const LoginRequest({required this.email, required this.password}); + + Map toJson() => {'email': email, 'password': password}; +} + +class RefreshRequest { + final String refreshToken; + + const RefreshRequest({required this.refreshToken}); + + Map toJson() => {'refresh_token': refreshToken}; +} + +class LogoutRequest { + final String refreshToken; + + const LogoutRequest({required this.refreshToken}); + + Map toJson() => {'refresh_token': refreshToken}; +} diff --git a/apps/lib/features/auth/data/models/signup_request.dart b/apps/lib/features/auth/data/models/signup_request.dart new file mode 100644 index 0000000..55e59d8 --- /dev/null +++ b/apps/lib/features/auth/data/models/signup_request.dart @@ -0,0 +1,34 @@ +class SignupStartRequest { + final String username; + final String email; + final String password; + + const SignupStartRequest({ + required this.username, + required this.email, + required this.password, + }); + + Map toJson() => { + 'username': username, + 'email': email, + 'password': password, + }; +} + +class SignupVerifyRequest { + final String email; + final String token; + + const SignupVerifyRequest({required this.email, required this.token}); + + Map toJson() => {'email': email, 'token': token}; +} + +class SignupResendRequest { + final String email; + + const SignupResendRequest({required this.email}); + + Map toJson() => {'email': email}; +} diff --git a/apps/test/features/auth/data/models/auth_models_test.dart b/apps/test/features/auth/data/models/auth_models_test.dart new file mode 100644 index 0000000..7641de1 --- /dev/null +++ b/apps/test/features/auth/data/models/auth_models_test.dart @@ -0,0 +1,56 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:social_app/features/auth/data/models/signup_request.dart'; +import 'package:social_app/features/auth/data/models/login_request.dart'; +import 'package:social_app/features/auth/data/models/auth_response.dart'; + +void main() { + group('SignupStartRequest', () { + test('serializes to JSON', () { + final request = SignupStartRequest( + username: 'testuser', + email: 'test@example.com', + password: 'password123', + ); + + final json = request.toJson(); + + expect(json['username'], 'testuser'); + expect(json['email'], 'test@example.com'); + expect(json['password'], 'password123'); + }); + }); + + group('LoginRequest', () { + test('serializes to JSON', () { + final request = LoginRequest( + email: 'test@example.com', + password: 'password123', + ); + + final json = request.toJson(); + + expect(json['email'], 'test@example.com'); + expect(json['password'], 'password123'); + }); + }); + + group('AuthResponse', () { + test('parses from JSON', () { + final json = { + 'access_token': 'test_access', + 'refresh_token': 'test_refresh', + 'expires_in': 3600, + 'token_type': 'bearer', + 'user': {'id': '123', 'email': 'test@example.com'}, + }; + + final response = AuthResponse.fromJson(json); + + expect(response.accessToken, 'test_access'); + expect(response.refreshToken, 'test_refresh'); + expect(response.expiresIn, 3600); + expect(response.user.id, '123'); + expect(response.user.email, 'test@example.com'); + }); + }); +}