Skip to content

Commit

Permalink
add flutterfire_authenticator (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
riscait authored Oct 15, 2023
1 parent e00f5c4 commit 2a097ba
Show file tree
Hide file tree
Showing 20 changed files with 586 additions and 4 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
".vscode",
".vscode-insiders",
],
"dart.projectSearchDepth": 3,
// ONLY GLOBAL: When reaching a Breakpoint, the debug view is automatically displayed.
"debug.openDebug": "openOnDebugBreak",
// Execute format on save if true.
Expand Down
Empty file removed packages/.gitkeep
Empty file.
30 changes: 30 additions & 0 deletions packages/flutterfire_authenticator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
10 changes: 10 additions & 0 deletions packages/flutterfire_authenticator/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: ffccd96b62ee8cec7740dab303538c5fc26ac543
channel: stable

project_type: package
3 changes: 3 additions & 0 deletions packages/flutterfire_authenticator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0

* initial release.
39 changes: 39 additions & 0 deletions packages/flutterfire_authenticator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
1 change: 1 addition & 0 deletions packages/flutterfire_authenticator/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:altive_lints/altive_lints.yaml
15 changes: 15 additions & 0 deletions packages/flutterfire_authenticator/lib/authenticator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// flutterfire_authenticator with FlutterFire Authentication.
library flutterfire_authenticator;

export 'package:firebase_auth/firebase_auth.dart'
show
AuthCredential,
OAuthCredential,
OAuthProvider,
PhoneAuthProvider,
UserCredential;

export 'src/auth_exception.dart';
export 'src/authenticator.dart';
export 'src/signing_method.dart';
export 'src/user_extension.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';

import '../authenticator.dart';
import 'authenticatable.dart';

class AppleAuthenticator implements Authenticatable {
AppleAuthenticator(this._auth);

final FirebaseAuth _auth;

final authProvider = AppleAuthProvider();

User get _user => _auth.currentUser!;

/// 既にAppleでサインイン済みなら`true`
@override
bool get alreadySigned => _auth.currentUser?.hasAppleSigning ?? false;

@override
Future<UserCredential> signIn([AuthCredential? credential]) async {
if (kIsWeb) {
final userCredential = await _auth.signInWithPopup(authProvider);
return userCredential;
} else {
final userCredential = await _auth.signInWithProvider(authProvider);
return userCredential;
}
}

@override
Future<UserCredential> reauthenticate([AuthCredential? credential]) async {
return _auth.currentUser!.reauthenticateWithProvider(authProvider);
}

@override
Future<UserCredential> link([AuthCredential? credential]) async {
return _user.linkWithProvider(authProvider);
}

@override
Future<User> unlink() async {
return _user.unlink(SigningMethod.apple.providerId);
}
}
71 changes: 71 additions & 0 deletions packages/flutterfire_authenticator/lib/src/auth_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import 'package:firebase_auth/firebase_auth.dart';

/// 認証で発生したエラーを判別するためのエラークラス。
/// FirebaseAuthExceptionから生成する。
sealed class AuthException implements Exception {
factory AuthException.fromError(FirebaseAuthException e) {
return switch (e.code) {
// Google認証でキャンセルした場合は「cancel」が返ってくる
// Apple認証でキャンセルした場合は「canceled」が返ってくる
'cancel' || 'canceled' => const AuthCancelled(),
'requires-recent-login' => const AuthRequiresReLogin(),
'invalid-phone-number' => const AuthInvalidPhoneNumber(),
'credential-already-in-use' => const AuthCredentialAlreadyInUse(),
'network-request-failed' => const AuthNetworkError(),
// 'email-already-in-use' => const ,
// 'provider-already-linked' => const ,
// 'too-many-requests' => const ,
// 'account-exists-with-different-credential' => const ,
// 'invalid-credential' => const ,
// 'user-disabled' => const ,
_ => const AuthUndefinedError(),
};
}

@override
String toString() => message;

String get message;
}

class AuthCancelled implements AuthException {
const AuthCancelled();

@override
String get message => '認証がキャンセルされました';
}

class AuthRequiresReLogin implements AuthException {
const AuthRequiresReLogin();

@override
String get message => '再ログインが必要です';
}

class AuthInvalidPhoneNumber implements AuthException {
const AuthInvalidPhoneNumber();

@override
String get message => '電話番号が不正です';
}

class AuthCredentialAlreadyInUse implements AuthException {
const AuthCredentialAlreadyInUse();

@override
String get message => 'この認証情報はすでに使用されています';
}

class AuthNetworkError implements AuthException {
const AuthNetworkError();

@override
String get message => 'ネットワークエラーが発生しました';
}

class AuthUndefinedError implements AuthException {
const AuthUndefinedError();

@override
String get message => '予期せぬエラーが発生しました';
}
30 changes: 30 additions & 0 deletions packages/flutterfire_authenticator/lib/src/authenticatable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:firebase_auth/firebase_auth.dart';

abstract class Authenticatable {
/// すでにサインイン済みかどうか。
bool get alreadySigned;

/// サインイン。
///
/// 電話番号認証ではサインイン処理が2STEPに分かれており、
/// [AuthCredential]を外から渡す必要があるため、
/// オプショナル引数として[AuthCredential]を受け取る形にしている。
Future<UserCredential> signIn([AuthCredential? credential]);

/// 再認証する。
///
/// 電話番号認証ではサインイン処理が2STEPに分かれており、
/// [AuthCredential]を外から渡す必要があるため、
/// オプショナル引数として[AuthCredential]を受け取る形にしている。
Future<UserCredential> reauthenticate([AuthCredential? credential]);

/// ユーザーにAppleを紐付ける。
///
/// 電話番号認証ではサインイン処理が2STEPに分かれており、
/// [AuthCredential]を外から渡す必要があるため、
/// オプショナル引数として[AuthCredential]を受け取る形にしている。
Future<UserCredential> link([AuthCredential? credential]);

/// Apple IDをリンク解除。
Future<User> unlink();
}
Loading

0 comments on commit 2a097ba

Please sign in to comment.