Skip to content

Commit

Permalink
feat: publish Authenticator.user (#10)
Browse files Browse the repository at this point in the history
* feat: publish Authenticator.user

* test: add AuthException tests

* style: fix text length
  • Loading branch information
riscait authored Oct 15, 2023
1 parent 2a097ba commit 2ef1c9a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
19 changes: 13 additions & 6 deletions packages/flutterfire_authenticator/lib/src/auth_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ sealed class AuthException implements Exception {
// Google認証でキャンセルした場合は「cancel」が返ってくる
// Apple認証でキャンセルした場合は「canceled」が返ってくる
'cancel' || 'canceled' => const AuthCancelled(),
'requires-recent-login' => const AuthRequiresReLogin(),
'requires-recent-login' => const AuthRequiresRecentSignIn(),
'invalid-phone-number' => const AuthInvalidPhoneNumber(),
'credential-already-in-use' => const AuthCredentialAlreadyInUse(),
'network-request-failed' => const AuthNetworkError(),
'network-request-failed' => const AuthFailedNetworkRequest(),
// 'email-already-in-use' => const ,
// 'provider-already-linked' => const ,
// 'too-many-requests' => const ,
Expand All @@ -35,8 +35,8 @@ class AuthCancelled implements AuthException {
String get message => '認証がキャンセルされました';
}

class AuthRequiresReLogin implements AuthException {
const AuthRequiresReLogin();
class AuthRequiresRecentSignIn implements AuthException {
const AuthRequiresRecentSignIn();

@override
String get message => '再ログインが必要です';
Expand All @@ -56,8 +56,8 @@ class AuthCredentialAlreadyInUse implements AuthException {
String get message => 'この認証情報はすでに使用されています';
}

class AuthNetworkError implements AuthException {
const AuthNetworkError();
class AuthFailedNetworkRequest implements AuthException {
const AuthFailedNetworkRequest();

@override
String get message => 'ネットワークエラーが発生しました';
Expand All @@ -69,3 +69,10 @@ class AuthUndefinedError implements AuthException {
@override
String get message => '予期せぬエラーが発生しました';
}

class AuthRequiresSignIn implements AuthException {
const AuthRequiresSignIn();

@override
String get message => '予期せぬエラーが発生しました';
}
17 changes: 10 additions & 7 deletions packages/flutterfire_authenticator/lib/src/authenticator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ class Authenticator {
final GoogleAuthenticator _googleAuth;
final PhoneAuthenticator _phoneAuth;

/// 現在サインイン中のユーザー。Null Assertionを使用しているので、
/// `requireUser` にリネームした方が良さそう。
User get _user => _auth.currentUser!;
/// 現在サインイン中のユーザー。サインインしていない場合はnullを返す。
User? get user => _auth.currentUser;

/// 現ユーザーのJWT(JSON Web Token)を非同期で取得する。
/// 未サインイン時など、現ユーザーが存在しない場合はnullを返す。
Future<String?>? get fetchJsonWebToken => _auth.currentUser?.getIdToken();
Future<String?>? get fetchJwt => _auth.currentUser?.getIdToken();

/// 匿名サインイン
Future<UserCredential> signInAnonymously() async {
Expand Down Expand Up @@ -111,15 +110,19 @@ class Authenticator {

/// アカウントを削除する
Future<void> deleteAccount() async {
final user = this.user;
if (user == null) {
throw const AuthRequiresSignIn();
}
try {
// アカウントを削除実行
await _user.delete();
await user.delete();
} on FirebaseAuthException catch (exception) {
final e = AuthException.fromError(exception);
if (e is AuthRequiresReLogin) {
if (e is AuthRequiresRecentSignIn) {
// 再認証の後、再度実行する
await _reauthenticate();
await _user.delete();
await user.delete();
} else {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutterfire_authenticator/authenticator.dart';

void main() {
group('Constructor', () {
test('AuthCancelled instance is created from the "cancel"', () {
final e = FirebaseAuthException(code: 'cancel');
final sut = AuthException.fromError(e);
expect(sut, isA<AuthCancelled>());
});

test('AuthCancelled instance is created from the "canceled"', () {
final e = FirebaseAuthException(code: 'cancel');
final sut = AuthException.fromError(e);
expect(sut, isA<AuthCancelled>());
});

test(
'AuthCancelled instance is created from the "requires-recent-login"',
() {
final e = FirebaseAuthException(code: 'requires-recent-login');
final sut = AuthException.fromError(e);
expect(sut, isA<AuthRequiresRecentSignIn>());
},
);

test(
'AuthCancelled instance is created from the "invalid-phone-number"',
() {
final e = FirebaseAuthException(code: 'invalid-phone-number');
final sut = AuthException.fromError(e);
expect(sut, isA<AuthInvalidPhoneNumber>());
},
);

test(
'AuthCancelled instance is created from the "credential-already-in-use"',
() {
final e = FirebaseAuthException(code: 'credential-already-in-use');
final sut = AuthException.fromError(e);
expect(sut, isA<AuthCredentialAlreadyInUse>());
},
);

test(
'AuthCancelled instance is created from the "network-request-failed"',
() {
final e = FirebaseAuthException(code: 'network-request-failed');
final sut = AuthException.fromError(e);
expect(sut, isA<AuthFailedNetworkRequest>());
},
);
});
}

0 comments on commit 2ef1c9a

Please sign in to comment.