guricode

[flutter-sns-project - 3]firebase_auth,google_sign_in 본문

앱/Flutter&Dart

[flutter-sns-project - 3]firebase_auth,google_sign_in

agentrakugaki 2025. 8. 31. 10:26

firebase_auth에서 제공하는 기능으로 google_sign_in 적용방법을 정리해보겠다

 

흐름은 이렇다

 

U[User📱]
GA[Google Auth Server]
GDB[(Google Auth DB)]
FA[Firebase Authentication]
FDB[(Firebase Authentication DB)]
U --1️⃣ 로그인 시도--- GA
subgraph Google Server
GA <--2️⃣ 아이디 비밀번호 데이터에비스의 정보와 비교해서 검증---> GDB
end
GA --3️⃣ 사용자를 특정지을수 있는 Token 발급--- U
U --4️⃣ Firebase Authentication 서버로 Google Token 전송--- FA
FA <--5️⃣ Google Server 로 Token 검증요청---> GA
subgraph Firebase
FA <--6️⃣ 검증 후 회원정보 없으면 회원정보 생성, 있으면 기존의 정보로 Firebase Token 생성---> FDB
end
FA --7️⃣ Firebase Token 전송---U

 

-구현

https://firebase.google.com/docs/auth/flutter/password-auth?hl=ko

 

Flutter에서 비밀번호 기반 계정으로 Firebase에 인증  |  Firebase Documentation

의견 보내기 Flutter에서 비밀번호 기반 계정으로 Firebase에 인증 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. Firebase 인증을 사용하여 사용자가 이메일 주소

firebase.google.com

 

firebase Console 세팅이 필요하다

에센티케이션화면으로 가서 로그인 제공업체 추가를 한다

 

그 후 이메일 비밀번호 사용설정

 

 

firebase_core,firebase_auth패키지를 추가한다(flutterfire configure)

google_sign_in 패키지를 추가한다

 

 

앱서명가이드를 참고한다

https://developers.google.com/android/guides/client-auth?hl=ko

 

//Ios 설정

https://pub.dev/packages/google_sign_in_ios#ios-integration

 

google_sign_in_ios | Flutter package

iOS implementation of the google_sign_in plugin.

pub.dev

//안드로이드

https://pub.dev/packages/google_sign_in

 

 

 

 

파이어베이스 초기화

//main.dart

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:google_sign/login_page.dart';
import 'firebase_options.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform, // ✅ 사용
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: LoginPage());
  }
}

 

 

 

로그인페이지를 구현한다

//login_page.dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: 500,
        height: 300,
        child: ElevatedButton(
          onPressed: () {
            googleLogin();
          },
          child: Text('로그인'),
        ),
      ),
    );
  }

  googleLogin() async {
    final FirebaseAuth _auth = FirebaseAuth.instance;
    final GoogleSignIn _googleSignIn = GoogleSignIn();

    final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
    if (googleUser == null) {
      // 사용자가 로그인 취소
      return;
    }

    // Google 인증 정보 가져오기
    final GoogleSignInAuthentication googleAuth =
        await googleUser.authentication;

    final AuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    // FirebaseAuth 로 생성된 Firebase 인증자격을 사용하여 계정을 생성하거나 로그인 진행
    final UserCredential userCredential = await _auth.signInWithCredential(
      credential,
    );
    print(userCredential);
  }
}

 

 

*버튼 브랜딩 가이드 규칙

https://developers.google.com/identity/branding-guidelines

 

Sign in with Google Branding Guidelines  |  Google Identity  |  Google for Developers

Send feedback Sign in with Google Branding Guidelines Stay organized with collections Save and categorize content based on your preferences. This document provides guidelines on how to display a Sign in with Google button on your website or app. Your websi

developers.google.com

 

*참고자료(예전버전)

https://www.notion.so/teamsparta/Firebase-Authentication-Google-24d2dc3ef51480d0b0b5e52d54edf4fc