Skip to content

Commit

Permalink
✨ add login/register logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocpd-1250 committed Aug 19, 2024
1 parent 2cef774 commit bcf093b
Show file tree
Hide file tree
Showing 38 changed files with 647 additions and 143 deletions.
3 changes: 3 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
plugins {
id "com.android.application"
// START: FlutterFire Configuration
id 'com.google.gms.google-services'
// END: FlutterFire Configuration
id "kotlin-android"
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id "dev.flutter.flutter-gradle-plugin"
Expand Down
29 changes: 29 additions & 0 deletions android/app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"project_info": {
"project_number": "155760957773",
"project_id": "fir-swiftui-4f49f",
"storage_bucket": "fir-swiftui-4f49f.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:155760957773:android:7ce973f12b023b6b61bf04",
"android_client_info": {
"package_name": "ngocpd.SwiftUIBase.android.dev"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyDp--jBlRrF6NiVT1PMpSy-5ExW_QxWPnc"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": []
}
}
}
],
"configuration_version": "1"
}
3 changes: 3 additions & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pluginManagement {
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
// START: FlutterFire Configuration
id "com.google.gms.google-services" version "4.3.15" apply false
// END: FlutterFire Configuration
id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

Expand Down
3 changes: 2 additions & 1 deletion assets/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"commonPassword": "Password",
"commonConfirmPassword": "Confirm Password",
"commonDontHaveAccount": "Don’t have an account?",
"commonAlreadyHaveAccount": "Already have an account?"
"commonAlreadyHaveAccount": "Already have an account?",
"registerSuccess": "Registered successfully. Please login!"
}
3 changes: 2 additions & 1 deletion assets/l10n/app_ja.arb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"commonPassword": "パスワード",
"commonConfirmPassword": "パスワードを確認",
"commonDontHaveAccount": "アカウントをお持ちでないですか?",
"commonAlreadyHaveAccount": "すでにアカウントをお持ちですか?"
"commonAlreadyHaveAccount": "すでにアカウントをお持ちですか?",
"registerSuccess": "登録が成功しました。ログインしてください!"
}
1 change: 1 addition & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"flutter":{"platforms":{"android":{"default":{"projectId":"fir-swiftui-4f49f","appId":"1:155760957773:android:7ce973f12b023b6b61bf04","fileOutput":"android/app/google-services.json"}},"ios":{"default":{"projectId":"fir-swiftui-4f49f","appId":"1:155760957773:ios:ed148fec33995c3161bf04","uploadDebugSymbols":false,"fileOutput":"ios/Runner/GoogleService-Info.plist"}},"dart":{"lib/firebase_options.dart":{"projectId":"fir-swiftui-4f49f","configurations":{"android":"1:155760957773:android:7ce973f12b023b6b61bf04","ios":"1:155760957773:ios:ed148fec33995c3161bf04"}}}}}}
80 changes: 80 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,17 +1,86 @@
PODS:
- Firebase/Auth (10.25.0):
- Firebase/CoreOnly
- FirebaseAuth (~> 10.25.0)
- Firebase/CoreOnly (10.25.0):
- FirebaseCore (= 10.25.0)
- firebase_auth (4.20.0):
- Firebase/Auth (= 10.25.0)
- firebase_core
- Flutter
- firebase_core (2.32.0):
- Firebase/CoreOnly (= 10.25.0)
- Flutter
- FirebaseAppCheckInterop (10.27.0)
- FirebaseAuth (10.25.0):
- FirebaseAppCheckInterop (~> 10.17)
- FirebaseCore (~> 10.0)
- GoogleUtilities/AppDelegateSwizzler (~> 7.8)
- GoogleUtilities/Environment (~> 7.8)
- GTMSessionFetcher/Core (< 4.0, >= 2.1)
- RecaptchaInterop (~> 100.0)
- FirebaseCore (10.25.0):
- FirebaseCoreInternal (~> 10.0)
- GoogleUtilities/Environment (~> 7.12)
- GoogleUtilities/Logger (~> 7.12)
- FirebaseCoreInternal (10.27.0):
- "GoogleUtilities/NSData+zlib (~> 7.8)"
- Flutter (1.0.0)
- flutter_secure_storage (3.3.1):
- Flutter
- GoogleUtilities/AppDelegateSwizzler (7.13.3):
- GoogleUtilities/Environment
- GoogleUtilities/Logger
- GoogleUtilities/Network
- GoogleUtilities/Privacy
- GoogleUtilities/Environment (7.13.3):
- GoogleUtilities/Privacy
- PromisesObjC (< 3.0, >= 1.2)
- GoogleUtilities/Logger (7.13.3):
- GoogleUtilities/Environment
- GoogleUtilities/Privacy
- GoogleUtilities/Network (7.13.3):
- GoogleUtilities/Logger
- "GoogleUtilities/NSData+zlib"
- GoogleUtilities/Privacy
- GoogleUtilities/Reachability
- "GoogleUtilities/NSData+zlib (7.13.3)":
- GoogleUtilities/Privacy
- GoogleUtilities/Privacy (7.13.3)
- GoogleUtilities/Reachability (7.13.3):
- GoogleUtilities/Logger
- GoogleUtilities/Privacy
- GTMSessionFetcher/Core (3.4.1)
- PromisesObjC (2.4.0)
- RecaptchaInterop (100.0.0)
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS

DEPENDENCIES:
- firebase_auth (from `.symlinks/plugins/firebase_auth/ios`)
- firebase_core (from `.symlinks/plugins/firebase_core/ios`)
- Flutter (from `Flutter`)
- flutter_secure_storage (from `.symlinks/plugins/flutter_secure_storage/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)

SPEC REPOS:
trunk:
- Firebase
- FirebaseAppCheckInterop
- FirebaseAuth
- FirebaseCore
- FirebaseCoreInternal
- GoogleUtilities
- GTMSessionFetcher
- PromisesObjC
- RecaptchaInterop

EXTERNAL SOURCES:
firebase_auth:
:path: ".symlinks/plugins/firebase_auth/ios"
firebase_core:
:path: ".symlinks/plugins/firebase_core/ios"
Flutter:
:path: Flutter
flutter_secure_storage:
Expand All @@ -20,8 +89,19 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

SPEC CHECKSUMS:
Firebase: 0312a2352584f782ea56f66d91606891d4607f06
firebase_auth: 5719ddc9f654b813405899480e84971bd8e61235
firebase_core: a626d00494efa398e7c54f25f1454a64c8abf197
FirebaseAppCheckInterop: 0dd062c9926a76332ca5711dbed6f1a9ac540b54
FirebaseAuth: c0f93dcc570c9da2bffb576969d793e95c344fbb
FirebaseCore: 7ec4d0484817f12c3373955bc87762d96842d483
FirebaseCoreInternal: 4b297a2d56063dbea2c1d0d04222d44a8d058862
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_secure_storage: 7953c38a04c3fdbb00571bcd87d8e3b5ceb9daec
GoogleUtilities: ea963c370a38a8069cc5f7ba4ca849a60b6d7d15
GTMSessionFetcher: 8000756fc1c19d2e5697b90311f7832d2e33f6cd
PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47
RecaptchaInterop: 7d1a4a01a6b2cb1610a47ef3f85f0c411434cb21
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78

PODFILE CHECKSUM: 6a14efad752d34a4bf2f66b717e84e2f72f17aca
Expand Down
4 changes: 4 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
E82183A63B7BD710E222C0D8 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = AE86177EE61F92763D07CBBD /* GoogleService-Info.plist */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -66,6 +67,7 @@
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A7C5372DD6EDD2C0771AF27A /* Pods-Runner.release-staging.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release-staging.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release-staging.xcconfig"; sourceTree = "<group>"; };
A9D9839ABF11D1EA912F92CD /* Pods_RunnerTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RunnerTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
AE86177EE61F92763D07CBBD /* GoogleService-Info.plist */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "Runner/GoogleService-Info.plist"; sourceTree = "<group>"; };
AF5D88FB7EB49912B969DD0A /* Pods-Runner.debug-dev.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug-dev.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug-dev.xcconfig"; sourceTree = "<group>"; };
B731D652B6D15BACE8385327 /* Pods-RunnerTests.debug-staging.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.debug-staging.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.debug-staging.xcconfig"; sourceTree = "<group>"; };
CFA5A146F8021840ABDC69C4 /* Pods-RunnerTests.release-dev.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release-dev.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release-dev.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -132,6 +134,7 @@
331C8082294A63A400263BE5 /* RunnerTests */,
D03161962E2453E5D7B59522 /* Pods */,
40381FCF2842740E611917D9 /* Frameworks */,
AE86177EE61F92763D07CBBD /* GoogleService-Info.plist */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -277,6 +280,7 @@
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,
E82183A63B7BD710E222C0D8 /* GoogleService-Info.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
30 changes: 30 additions & 0 deletions ios/Runner/GoogleService-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>API_KEY</key>
<string>AIzaSyCOJY9mkjOLH7srOe3nQHbEX-9qEJLmdKg</string>
<key>GCM_SENDER_ID</key>
<string>155760957773</string>
<key>PLIST_VERSION</key>
<string>1</string>
<key>BUNDLE_ID</key>
<string>com.example.baseFlutter</string>
<key>PROJECT_ID</key>
<string>fir-swiftui-4f49f</string>
<key>STORAGE_BUCKET</key>
<string>fir-swiftui-4f49f.appspot.com</string>
<key>IS_ADS_ENABLED</key>
<false></false>
<key>IS_ANALYTICS_ENABLED</key>
<false></false>
<key>IS_APPINVITE_ENABLED</key>
<true></true>
<key>IS_GCM_ENABLED</key>
<true></true>
<key>IS_SIGNIN_ENABLED</key>
<true></true>
<key>GOOGLE_APP_ID</key>
<string>1:155760957773:ios:ed148fec33995c3161bf04</string>
</dict>
</plist>
6 changes: 6 additions & 0 deletions lib/data/app_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart';

import 'package:dio/dio.dart';
import 'package:firebase_auth/firebase_auth.dart';

import 'package:base_flutter/data/remote/response/error_response.dart';

Expand All @@ -14,6 +15,7 @@ enum AppErrorType {
cancel,
timeout,
server,
firebaseAuth,
unknown,
}

Expand Down Expand Up @@ -88,6 +90,10 @@ class AppError {
type = AppErrorType.unauthorized;
code = HttpStatus.unauthorized;
message = exception.message;
} else if (exception is FirebaseAuthException) {
type = AppErrorType.firebaseAuth;
code = HttpStatus.unauthorized;
message = exception.message;
} else {
code = -1;
type = AppErrorType.unknown;
Expand Down
8 changes: 0 additions & 8 deletions lib/data/local/preferences/preferences.dart

This file was deleted.

41 changes: 41 additions & 0 deletions lib/data/remote/firebase_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:firebase_auth/firebase_auth.dart';

import 'package:base_flutter/data/base/data_source.dart';

class FirebaseDataSource with DataSource {
final FirebaseAuth _firebaseAuth;

FirebaseDataSource({FirebaseAuth? firebaseAuth})
: _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance;

Future<User?> signIn({
required String email,
required String password,
}) async {
final response = await excuse(() async {
final UserCredential userCredential =
await _firebaseAuth.signInWithEmailAndPassword(
email: email,
password: password,
);
return userCredential.user;
});

return response;
}

Future<User?> signUp({
required String email,
required String password,
}) async {
final response = await excuse(() async {
final UserCredential userCredential =
await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
);
return userCredential.user;
});
return response;
}
}
18 changes: 10 additions & 8 deletions lib/data/repositories/auth_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import 'package:base_flutter/data/local/local_data_source.dart';
import 'package:base_flutter/data/model/user_data.dart';
import 'package:base_flutter/data/remote/auth_remote_data_source.dart';
import 'package:base_flutter/data/remote/firebase_data_source.dart';
import 'package:base_flutter/domain/repositories/auth_repository.dart';

class AuthRepositoryImpl extends AuthRepository {
final AuthRemoteDataSource _authRemoteDataSource;
final FirebaseDataSource _firebaseDataSource;
final LocalDataSource _localDataSource;

AuthRepositoryImpl({
required AuthRemoteDataSource authRemoteDataSource,
required FirebaseDataSource firebaseDataSource,
required LocalDataSource localDataSource,
}) : _authRemoteDataSource = authRemoteDataSource,
}) : _firebaseDataSource = firebaseDataSource,
_localDataSource = localDataSource;

@override
Future login(String username, String password) async {
final authToken = await _authRemoteDataSource.login(username, password);
_localDataSource.saveAuthTokens(authToken);
_localDataSource.saveUser(UserData(id: 1, name: username));
await _firebaseDataSource.signIn(email: username, password: password);
}

@override
Future register(String username, String password) async {
await _firebaseDataSource.signUp(email: username, password: password);
}

@override
Expand Down
10 changes: 10 additions & 0 deletions lib/di/firebase_data_source_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';

import 'package:base_flutter/data/remote/firebase_data_source.dart';

part 'firebase_data_source_provider.g.dart';

@Riverpod(keepAlive: true)
FirebaseDataSource firebaseDataSource(FirebaseDataSourceRef ref) {
return FirebaseDataSource();
}
16 changes: 9 additions & 7 deletions lib/di/local_provider.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:base_flutter/data/local/local_data_source.dart';
import 'package:base_flutter/data/local/preferences/preferences.dart';
import 'package:base_flutter/data/local/storage/secure_storage.dart';
import 'package:base_flutter/data/local/storage/storage_keys.dart';

part 'local_provider.g.dart';

@Riverpod(keepAlive: true)
Future<SecureStorage> secureStorage(SecureStorageRef ref) {
return SecureStorage.getInstance(
keys: StorageKeys.values.map((e) => e.name).toSet());
SecureStorage secureStorage(SecureStorageRef ref) {
return throw UnimplementedError();
}

@Riverpod(keepAlive: true)
SharedPreferences sharedPreferences(SharedPreferencesRef ref) =>
throw UnimplementedError();

@Riverpod(keepAlive: true)
LocalDataSource localDataSource(LocalDataSourceRef ref) {
return LocalDataSource(
secureStorage: ref.watch(secureStorageProvider).requireValue,
sharedPreferences: ref.watch(prefsProvider).requireValue,
secureStorage: ref.watch(secureStorageProvider),
sharedPreferences: ref.watch(sharedPreferencesProvider),
);
}
Loading

0 comments on commit bcf093b

Please sign in to comment.