mirror of https://github.com/flutter/pinball.git
feat(authentication_repository): support anonymous authentication (#269)
parent
f2a20742f0
commit
874507d14b
@ -0,0 +1,22 @@
|
||||
name: authentication_repository
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- "packages/authentication_repository/**"
|
||||
- ".github/workflows/authentication_repository.yaml"
|
||||
|
||||
pull_request:
|
||||
paths:
|
||||
- "packages/authentication_repository/**"
|
||||
- ".github/workflows/authentication_repository.yaml"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/flutter_package.yml@v1
|
||||
with:
|
||||
working_directory: packages/authentication_repository
|
@ -0,0 +1,39 @@
|
||||
# Miscellaneous
|
||||
*.class
|
||||
*.log
|
||||
*.pyc
|
||||
*.swp
|
||||
.DS_Store
|
||||
.atom/
|
||||
.buildlog/
|
||||
.history
|
||||
.svn/
|
||||
|
||||
# IntelliJ related
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# VSCode related
|
||||
.vscode/
|
||||
|
||||
# Flutter/Dart/Pub related
|
||||
**/doc/api/
|
||||
**/ios/Flutter/.last_build_id
|
||||
.dart_tool/
|
||||
.flutter-plugins
|
||||
.flutter-plugins-dependencies
|
||||
.packages
|
||||
.pub-cache/
|
||||
.pub/
|
||||
/build/
|
||||
|
||||
# Web related
|
||||
lib/generated_plugin_registrant.dart
|
||||
|
||||
# Symbolication related
|
||||
app.*.symbols
|
||||
|
||||
# Obfuscation related
|
||||
app.*.map.json
|
@ -0,0 +1,11 @@
|
||||
# authentication_repository
|
||||
|
||||
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
|
||||
[![License: MIT][license_badge]][license_link]
|
||||
|
||||
Repository to manage user authentication.
|
||||
|
||||
[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
|
||||
[license_link]: https://opensource.org/licenses/MIT
|
||||
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
|
||||
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis
|
@ -0,0 +1 @@
|
||||
include: package:very_good_analysis/analysis_options.2.4.0.yaml
|
@ -0,0 +1,3 @@
|
||||
library authentication_repository;
|
||||
|
||||
export 'src/authentication_repository.dart';
|
@ -0,0 +1,36 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
|
||||
/// {@template authentication_exception}
|
||||
/// Exception for authentication repository failures.
|
||||
/// {@endtemplate}
|
||||
class AuthenticationException implements Exception {
|
||||
/// {@macro authentication_exception}
|
||||
const AuthenticationException(this.error, this.stackTrace);
|
||||
|
||||
/// The error that was caught.
|
||||
final Object error;
|
||||
|
||||
/// The Stacktrace associated with the [error].
|
||||
final StackTrace stackTrace;
|
||||
}
|
||||
|
||||
/// {@template authentication_repository}
|
||||
/// Repository to manage user authentication.
|
||||
/// {@endtemplate}
|
||||
class AuthenticationRepository {
|
||||
/// {@macro authentication_repository}
|
||||
AuthenticationRepository(this._firebaseAuth);
|
||||
|
||||
final FirebaseAuth _firebaseAuth;
|
||||
|
||||
/// Sign in the existing user anonymously using [FirebaseAuth]. If the
|
||||
/// authentication process can't be completed, it will throw an
|
||||
/// [AuthenticationException].
|
||||
Future<void> authenticateAnonymously() async {
|
||||
try {
|
||||
await _firebaseAuth.signInAnonymously();
|
||||
} on Exception catch (error, stackTrace) {
|
||||
throw AuthenticationException(error, stackTrace);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
name: authentication_repository
|
||||
description: Repository to manage user authentication.
|
||||
version: 1.0.0+1
|
||||
publish_to: none
|
||||
|
||||
environment:
|
||||
sdk: ">=2.16.0 <3.0.0"
|
||||
|
||||
dependencies:
|
||||
firebase_auth: ^3.3.16
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
mocktail: ^0.2.0
|
||||
very_good_analysis: ^2.4.0
|
@ -0,0 +1,40 @@
|
||||
import 'package:authentication_repository/authentication_repository.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockFirebaseAuth extends Mock implements FirebaseAuth {}
|
||||
|
||||
class MockUserCredential extends Mock implements UserCredential {}
|
||||
|
||||
void main() {
|
||||
late FirebaseAuth firebaseAuth;
|
||||
late UserCredential userCredential;
|
||||
late AuthenticationRepository authenticationRepository;
|
||||
|
||||
group('AuthenticationRepository', () {
|
||||
setUp(() {
|
||||
firebaseAuth = MockFirebaseAuth();
|
||||
userCredential = MockUserCredential();
|
||||
authenticationRepository = AuthenticationRepository(firebaseAuth);
|
||||
});
|
||||
|
||||
group('authenticateAnonymously', () {
|
||||
test('completes if no exception is thrown', () async {
|
||||
when(() => firebaseAuth.signInAnonymously())
|
||||
.thenAnswer((_) async => userCredential);
|
||||
await authenticationRepository.authenticateAnonymously();
|
||||
verify(() => firebaseAuth.signInAnonymously()).called(1);
|
||||
});
|
||||
|
||||
test('throws AuthenticationException when firebase auth fails', () async {
|
||||
when(() => firebaseAuth.signInAnonymously())
|
||||
.thenThrow(Exception('oops'));
|
||||
expect(
|
||||
() => authenticationRepository.authenticateAnonymously(),
|
||||
throwsA(isA<AuthenticationException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue