-
Flutter Web Firebase Auth์ฐ๋Flutter ํ ํฌ 2024. 7. 8. 23:08
1. Firebase๊ธฐ๋ณธ์ฐ๋์ด ์๋ฃ๋ ์ํ์์ ํญ ๋ฉ๋ด์์ "๋น๋" -> "Authentication"์ ํ
2. ๊ธฐ๋ณธ์ ์ธ "์ด๋ฉ์ผ/๋น๋ฐ๋ฒํธ"์ ํ ํ ์ฌ์ฉ์ค์ ํ ์ ์ฅ ํด๋ฆญ
3. Firebase Authํจํค์ง ์ค์น(https://pub.dev/packages/firebase_auth/install)
firebase_auth: ^5.1.1
4. Flutter ํ๋ก์ ํธ์ web/index.html ์ด๊ธฐ
5. ํ๋จ์ <body>ํ๊ทธ ๋ฐ์ ์๋์ ์ค๋ํซ ์ถ๊ฐ
<!-- Firebase ๊ตฌ์ฑ ์ค๋ํซ --> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-analytics.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-auth.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-functions.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-messaging.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-storage.js"></script>
6. ๋ฐฐํฌ ํ ํ์ธ
7. ํ์ฉ ์์ ์ฝ๋
// ๋น๋ฐ๋ฒํธ ์ฌ์ค์ ๋ฉ์ผ ์ ์ก Future<List> sendResetPasswordEmail(String email) async { try { await FirebaseAuth.instance.setLanguageCode("ko"); //์ธ์ด ์ค์ await FirebaseAuth.instance.sendPasswordResetEmail(email: email); return [true]; } catch(e) { print('error sendResetPasswordEmail : ${e}'); return [false, '${e}']; } } // ๋ก๊ทธ์ธ ํจ์ Future<List> login({required String email, required String password,}) async { try { try { UserCredential _cred = await FirebaseAuth.instance.signInWithEmailAndPassword( email: email, password: password); //๋ก๊ทธ์ธ ์ฑ๊ณต return [true, _cred.user?.uid, _cred.user?.email]; } on FirebaseAuthException catch (e) { if (e.code == 'user-not-found') { //์กด์ฌํ์ง ์๋ ์ด๋ฉ์ผ return [false, '์กด์ฌํ์ง ์๋ ์ด๋ฉ์ผ์ ๋๋ค.']; } else if (e.code == 'wrong-password') { //๋น๋ฐ๋ฒํธ ์ค๋ฅ return [false, '๋น๋ฐ๋ฒํธ ์ค๋ฅ']; } } return [false, '๋ก๊ทธ์ธ ์คํจ']; } catch(e) { print('error login ${e}'); return [false, '${e}']; } } // ์ด๋ฉ์ผ,๋น๋ฐ๋ฒํธ๋ฅผ ์ฌ์ฉํ ํ์๊ฐ์ Future<List> signUpWithEmail({ required String email, required String password, required context, }) async { try { UserCredential _cred = await FirebaseAuth.instance.createUserWithEmailAndPassword( email: email, password: password); //์ด ์ ๋ณด๋ก ํ์๊ฐ์ //ํ์๊ฐ์ ์ฑ๊ณต return [true, _cred.user?.uid, _cred.user?.email]; } on FirebaseAuthException catch (e) { if (e.code == 'invalid-credential') { print('The supplied auth credential is incorrect, malformed or has expired.'); return [false, '์ธ์ฆ ์๊ฒฉ ์ฆ๋ช ์ด ์๋ชป๋์๊ฑฐ๋, ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์๊ฑฐ๋, ๋ง๋ฃ๋์์']; } else if (e.code == 'weak-password') { //๋น๋ฐ๋ฒํธ ๋ณด์ ๊ฐ๋ ๋ฌธ์ return [false, '๋น๋ฐ๋ฒํธ๊ฐ ๋ณด์์ ์ทจ์ฝํฉ๋๋ค']; } else if (e.code == 'email-already-in-use') { //์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ return [false, '์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ']; } return [false, e.toString()]; } catch (e) { print('on FirebaseAuthException catch ('); return [false, e.toString()]; } } // ๋ก๊ทธ์์์ฒ๋ฆฌ Future logoutUser() async { await FirebaseAuth.instance.signOut(); }
'Flutter ํ ํฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Flutter Chat GPT์ฐ๋ (0) 2024.07.10 Flutter Firebase Hosting ์ค์ (์น์ฌ์ดํธ๋ฐฐํฌ) (0) 2024.07.08 Flutter + Firebase ์ฐ๋ (0) 2023.11.24 Flutter + FCM ์ฐ๋ (Flutter 3.0์ด์, ์๋ก์ดFCM์ ์ฉ๋ฒ์ , 3.0์ด์์ด ์๋์ฌ๋ ๊ฐ๋ฅํฉ๋๋ค) (0) 2023.11.24 Flutter Fastlane CI/CD(2) - Android (1) 2023.03.07