name27 / flutter

0 stars 0 forks source link

firebase 회원가입 로그인 이메일 인증 #112

Open name27 opened 1 year ago

name27 commented 1 year ago

image image image image image image

main.dart

import 'package:day_34_test_firebase/controller/autho_controller.dart';
import 'package:day_34_test_firebase/controller/login_controller.dart';
import 'package:day_34_test_firebase/controller/sign_up_controller.dart';
import 'package:day_34_test_firebase/firebase_options.dart';
import 'package:day_34_test_firebase/view/pages/login_page.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp(
   options: DefaultFirebaseOptions.currentPlatform,
 );
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialBinding: BindingsBuilder(() {
        Get.put(AuthController());
        Get.lazyPut(() => LoginController());
        Get.lazyPut(() => SignUpController(), fenix: true);
      }),
      home: const LoginPage(),
    );
  }
}

login_page.dart

import 'package:day_34_test_firebase/controller/login_controller.dart';
import 'package:day_34_test_firebase/view/pages/sign_up_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class LoginPage extends GetView<LoginController> {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('로그인 페이지'),
              TextField(
                controller: controller.emailController,
                decoration: const InputDecoration(hintText: 'email'),
              ),
              TextField(
                controller: controller.pwController,
                decoration: const InputDecoration(hintText: 'password'),
              ),
              ElevatedButton(
                  onPressed: controller.startLogin, child: const Text('로그인')),
              const SizedBox(
                width: 30,
              ),
              TextButton(
                  onPressed: () {
                    Get.to(const SignUpPage());
                  },
                  child: const Text('회원가입 하기')),
            ],
          ),
        ),
      ),
    );
  }
}

sign_up_page.dart

import 'package:day_34_test_firebase/controller/sign_up_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SignUpPage extends GetView<SignUpController> {
  const SignUpPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              const Text('회원가입 페이지'),
              TextField(
                controller: controller.emailController,
                decoration: const InputDecoration(hintText: 'email'),
              ),
              TextField(
                controller: controller.pwController,
                decoration: const InputDecoration(hintText: 'password'),
              ),
              ElevatedButton(
                  onPressed: controller.signUp, child: const Text('회원가입 하기')),
            ],
          ),
        ),
      ),
    );
  }
}

main_page.dart

import 'package:day_34_test_firebase/controller/auth_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MainPage extends GetView<AuthController> {
  const MainPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: controller.user.value != null
            ? Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const Text('메인페이지'),
                  Text(controller.user.value!.email.toString()),
                  controller.user.value!.emailVerified
                      ? const Text('이메일이 인증된 사용자입니다')
                      : TextButton(
                          onPressed: controller.sendEmail,
                          child: const Text('이곳을 눌러 이메일 인증을 해주세요')),
                  TextButton(
                      onPressed: controller.signOut, child: const Text('로그아웃')),
                ],
              )
            : const SizedBox(),
      ),
    );
  }
}

auth_controller.dart

import 'package:day_34_test_firebase/view/pages/login_page.dart';
import 'package:day_34_test_firebase/view/pages/main_page.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:get/get.dart';

class AuthController extends GetxController {
  final Rxn<User> user = Rxn<User>();
  sendEmail() => FirebaseAuth.instance.currentUser?.sendEmailVerification();

  signOut() => FirebaseAuth.instance.signOut();

  signUp(String email, String pw) => FirebaseAuth.instance
      .createUserWithEmailAndPassword(email: email, password: pw);

  login(String email, String pw) async {
    FirebaseAuth.instance
        .signInWithEmailAndPassword(email: email, password: pw);
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    FirebaseAuth.instance.authStateChanges().listen((value) {
      user(value);
      if (value != null) {
        print(value.email);
        Get.to(MainPage());
      } else {
        print('로그인 회원가입 필요');
        Get.to(LoginPage());
      }
    });
  }
}

login_controller.dart

import 'package:day_34_test_firebase/controller/auth_controller.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

class LoginController extends GetxController {
  var emailController = TextEditingController();
  var pwController = TextEditingController();
  startLogin() {
    Get.find<AuthController>().login(emailController.text, pwController.text);
  }
}

sign_up_controller.dart

import 'package:day_34_test_firebase/controller/auth_controller.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

class SignUpController extends GetxController {
  var emailController = TextEditingController();
  var pwController = TextEditingController();

  signUp() {
    Get.find<AuthController>().signUp(emailController.text, pwController.text);
  }
}
name27 commented 1 year ago

image