From e38f52265c4607e3670f48adc9aaec39e8c0db71 Mon Sep 17 00:00:00 2001 From: HuiChan Seo <78739194+seochan99@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:39:41 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Feat:=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=EC=9C=A0=EC=A7=80=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat: 로그인유지 기능 구현 --- lib/services/auth/auth_service.dart | 17 +++++++ .../auth/email_signup_viewmodel.dart | 10 ++++- lib/views/auth/logout_dialog.dart | 44 +++++-------------- lib/views/root/root_screen.dart | 2 - 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/lib/services/auth/auth_service.dart b/lib/services/auth/auth_service.dart index 8a9cc9d..451b533 100644 --- a/lib/services/auth/auth_service.dart +++ b/lib/services/auth/auth_service.dart @@ -3,9 +3,17 @@ import 'package:firebase_auth/firebase_auth.dart'; import 'package:get/get.dart'; import 'package:google_sign_in/google_sign_in.dart'; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; class AuthService { final FirebaseAuth _auth = FirebaseAuth.instance; + final FlutterSecureStorage _storage = const FlutterSecureStorage(); + + // 로그인 상태 확인 + Future isLoggedIn() async { + final String? uid = await _storage.read(key: 'uid'); + return uid != null && uid.isNotEmpty; + } Future signInWithGoogle() async { try { @@ -24,6 +32,9 @@ class AuthService { // 구글 로그인 성공시 UserCredential 반환 await _auth.signInWithCredential(credential); + // uid 저장 + await _storage.write(key: 'uid', value: _auth.currentUser?.uid); + // 홈화면으로 이동 Get.offAllNamed('/'); } catch (error) { @@ -35,4 +46,10 @@ class AuthService { ); } } + + // 로그아웃 + Future signOut() async { + await _auth.signOut(); + await _storage.delete(key: 'uid'); + } } diff --git a/lib/viewModels/auth/email_signup_viewmodel.dart b/lib/viewModels/auth/email_signup_viewmodel.dart index 2e2737d..d49d785 100644 --- a/lib/viewModels/auth/email_signup_viewmodel.dart +++ b/lib/viewModels/auth/email_signup_viewmodel.dart @@ -1,12 +1,14 @@ import 'package:earlips/utilities/validators/auth_validators.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:get/get.dart'; class EmailSignupViewModel extends GetxController { final formKey = GlobalKey(); final emailController = TextEditingController(); final passwordController = TextEditingController(); + final FlutterSecureStorage _storage = const FlutterSecureStorage(); // Email Validator String? emailValidator(String? value) { @@ -21,10 +23,16 @@ class EmailSignupViewModel extends GetxController { Future registerWithEmailAndPassword() async { if (formKey.currentState!.validate()) { try { - await FirebaseAuth.instance.createUserWithEmailAndPassword( + final UserCredential userCredential = + await FirebaseAuth.instance.createUserWithEmailAndPassword( email: emailController.text.trim(), password: passwordController.text.trim(), ); + + // uid 저장 + final String uid = userCredential.user!.uid; + await _storage.write(key: 'uid', value: uid); + // 뒤로 Get.back(); } on FirebaseAuthException catch (_) { diff --git a/lib/views/auth/logout_dialog.dart b/lib/views/auth/logout_dialog.dart index 1427394..ea9646e 100644 --- a/lib/views/auth/logout_dialog.dart +++ b/lib/views/auth/logout_dialog.dart @@ -1,39 +1,19 @@ -import 'package:firebase_auth/firebase_auth.dart'; +import 'package:earlips/services/auth/auth_service.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; Future showLogoutDialog(BuildContext context) async { - return showDialog( - context: context, - barrierDismissible: false, - builder: (BuildContext context) { - return AlertDialog( - title: const Text('로그아웃'), - content: const SingleChildScrollView( - child: ListBody( - children: [ - Text('정말 로그아웃하시겠습니까?'), - ], - ), - ), - actions: [ - TextButton( - child: const Text('취소'), - onPressed: () { - Get.back(); - }, - ), - TextButton( - child: const Text('로그아웃'), - onPressed: () async { - // 로그아웃 처리 - // FirebaseAuth.instance.signOut(); - await FirebaseAuth.instance.signOut(); - Get.back(); - }, - ), - ], - ); + final AuthService authService = AuthService(); + + return Get.defaultDialog( + title: '로그아웃', + content: const Text('정말 로그아웃하시겠습니까?'), + textConfirm: '로그아웃', + textCancel: '취소', + onCancel: () => Get.back(), // Equivalent to closing the dialog + onConfirm: () async { + await authService.signOut(); + Get.back(); // Close the dialog after signOut }, ); } diff --git a/lib/views/root/root_screen.dart b/lib/views/root/root_screen.dart index 13ce069..77e28ed 100644 --- a/lib/views/root/root_screen.dart +++ b/lib/views/root/root_screen.dart @@ -1,10 +1,8 @@ -import 'package:earlips/views/auth/login_screen.dart'; import 'package:earlips/views/profile/profile_screen.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:earlips/views/home/home_screen.dart'; import 'package:earlips/views/root/custom_bottom_navigation_bar.dart'; - import '../../viewModels/root/root_viewmodel.dart'; import '../base/base_screen.dart';