Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Feat: 로그인유지 기능 구현 #11

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/services/auth/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> isLoggedIn() async {
final String? uid = await _storage.read(key: 'uid');
return uid != null && uid.isNotEmpty;
}

Future<void> signInWithGoogle() async {
try {
Expand All @@ -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) {
Expand All @@ -35,4 +46,10 @@ class AuthService {
);
}
}

// 로그아웃
Future<void> signOut() async {
await _auth.signOut();
await _storage.delete(key: 'uid');
}
}
10 changes: 9 additions & 1 deletion lib/viewModels/auth/email_signup_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -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<FormState>();
final emailController = TextEditingController();
final passwordController = TextEditingController();
final FlutterSecureStorage _storage = const FlutterSecureStorage();

// Email Validator
String? emailValidator(String? value) {
Expand All @@ -21,10 +23,16 @@ class EmailSignupViewModel extends GetxController {
Future<void> 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 (_) {
Expand Down
44 changes: 12 additions & 32 deletions lib/views/auth/logout_dialog.dart
Original file line number Diff line number Diff line change
@@ -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<void> showLogoutDialog(BuildContext context) async {
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('로그아웃'),
content: const SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('정말 로그아웃하시겠습니까?'),
],
),
),
actions: <Widget>[
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
},
);
}
2 changes: 0 additions & 2 deletions lib/views/root/root_screen.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Loading