Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firebase_auth_dart): web support #72

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
library firebase_auth_dart;

import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:io';

import 'package:firebase_core_dart/firebase_core_dart.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:storagebox/storagebox.dart';

import 'src/api/api.dart';
import 'src/api/errors.dart';
Expand Down Expand Up @@ -46,4 +46,3 @@ part 'src/user.dart';
part 'src/user_credential.dart';
part 'src/user_info.dart';
part 'src/user_metadata.dart';
part 'src/utils/persistence.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ part 'authentication/sms.dart';
part 'authentication/token.dart';
part 'emulator.dart';

/// All API classes calling to IDP API must extend this template.
/// All API classes calling to Identity Toolkit API must extend this template.
abstract class APIDelegate {
/// Construct a new [APIDelegate].
const APIDelegate(this.api);

/// The [API] instance containing required configurations to make the requests.
final API api;

/// Convert [DetailedApiRequestError] thrown by idp to [FirebaseAuthException].
/// Convert [DetailedApiRequestError] thrown by Identity Toolkit to [FirebaseAuthException].
FirebaseAuthException makeAuthException(DetailedApiRequestError apiError) {
try {
final json = apiError.jsonResponse;
Expand Down Expand Up @@ -83,7 +83,7 @@ abstract class APIDelegate {
}
}

/// Configurations necessary for making all idp requests.
/// Configurations necessary for making all Identity Toolkit requests.
@protected
class APIConfig {
/// Construct [APIConfig].
Expand Down Expand Up @@ -133,7 +133,7 @@ abstract class SignInResponse {
}
}

/// A return type from Idp authentication requests, must be extended by any other response
/// A return type from Identity Toolkit authentication requests, must be extended by any other response
/// type for any operation that requires idToken.
@protected
abstract class IdTokenResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ part of api;
/// A return type from Idp phone authentication requests.
@internal
class SignInWithPhoneNumberResponse extends SignInResponse {
/// Construct a new [IdTokenResponse].
/// Construct a new [SignInWithPhoneNumberResponse].
SignInWithPhoneNumberResponse._({
required String idToken,
required String refreshToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ class IdToken extends APIDelegate {
// ignore: public_member_api_docs
IdToken(API api) : super(api);

/// Refresh a user ID token using the refreshToken,
/// Refresh a user's IdToken using a `refreshToken`,
/// will refresh even if the token hasn't expired.
///
/// Common error codes:
/// - `TOKEN_EXPIRED`: The user's credential is no longer valid. The user must sign in again.
/// - `USER_DISABLED`: The user account has been disabled by an administrator.
/// - `USER_NOT_FOUND`: The user corresponding to the refresh token was not found. It is likely the user was deleted.
/// - `INVALID_REFRESH_TOKEN`: An invalid refresh token is provided.
/// - `INVALID_GRANT_TYPE`: the grant type specified is invalid.
/// - `MISSING_REFRESH_TOKEN`: no refresh token provided.
/// - API key not valid. Please pass a valid API key. (invalid API key provided)
Future<String?> refreshIdToken(String? refreshToken) async {
try {
return await _exchangeRefreshWithIdToken(refreshToken);
} on HttpException catch (_) {
rethrow;
} catch (_) {
rethrow;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ class FirebaseAuth {
_api.client = client;
}

StorageBox<Object> get _userStorage =>
StorageBox.instanceOf(app.options.projectId);
StorageBox get _userStorage => StorageBox(
app.options.projectId,
configPathPrefix: '.firebase-auth',
);

Map<String, dynamic>? _localUser() {
try {
return (_userStorage.getValue('${app.options.apiKey}:${app.name}')
as Map<String, dynamic>)['currentUser'];
return (_userStorage['${app.options.apiKey}:${app.name}']
as Map<String, dynamic>?)?['currentUser'];
} catch (e) {
return null;
}
Expand Down Expand Up @@ -109,10 +111,11 @@ class FirebaseAuth {
@protected
void _updateCurrentUserAndEvents(User? user,
[bool authStateChanged = false]) {
_userStorage.putValue(
'${app.options.apiKey}:${app.name}',
{'currentUser': user?.toMap()},
);
_userStorage.addAll({
'${app.options.apiKey}:${app.name}': {
'currentUser': user?.toMap(),
},
});

_currentUser = user;

Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions packages/firebase_auth/firebase_auth_dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
googleapis_auth: ^1.1.0
http: ^0.13.4
meta: ^1.3.0
storagebox: ^0.1.0+2

dev_dependencies:
async: ^2.8.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,35 +178,35 @@ void main() {
});
});
});
group('StorageBox ', () {
test('put a new value.', () {
final box = StorageBox.instanceOf('box');
box.putValue('key', '123');

expect(box.getValue('key'), '123');
});
test('put a null value does not add the value.', () {
final box = StorageBox.instanceOf('box');
box.putValue('key_2', null);
expect(
() => box.getValue('key_2'),
throwsA(isA<StorageBoxException>()),
);
});
test('get a key that does not exist.', () {
final box = StorageBox.instanceOf('box');
expect(
() => box.getValue('random_key'),
throwsA(isA<StorageBoxException>()),
);
});
test('get a key from a box that does not exist.', () {
final box = StorageBox.instanceOf('box_');
expect(
() => box.getValue('key'),
throwsA(isA<StorageBoxException>()),
);
});
});
// group('StorageBox ', () {
// test('put a new value.', () {
// final box = StorageBox.instanceOf('box');
// box.putValue('key', '123');

// expect(box.getValue('key'), '123');
// });
// test('put a null value does not add the value.', () {
// final box = StorageBox.instanceOf('box');
// box.putValue('key_2', null);
// expect(
// () => box.getValue('key_2'),
// throwsA(isA<StorageBoxException>()),
// );
// });
// test('get a key that does not exist.', () {
// final box = StorageBox.instanceOf('box');
// expect(
// () => box.getValue('random_key'),
// throwsA(isA<StorageBoxException>()),
// );
// });
// test('get a key from a box that does not exist.', () {
// final box = StorageBox.instanceOf('box_');
// expect(
// () => box.getValue('key'),
// throwsA(isA<StorageBoxException>()),
// );
// });
// });
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
firebase_core_dart: ^0.1.1
http: ^0.13.4
meta: ^1.7.0
storagebox: ^0.1.0+1

dev_dependencies:
test: ^1.16.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:firebase_auth_dart/firebase_auth_dart.dart';
import 'package:firebase_core_dart/firebase_core_dart.dart';
import 'package:firebase_functions_dart/firebase_functions_dart.dart';
import 'package:http/http.dart' as http;
import 'package:storagebox/storagebox.dart';
import 'package:test/test.dart';

import 'data.dart' as data;
Expand Down Expand Up @@ -154,11 +155,11 @@ Future<void> main() async {
functions.useFunctionsEmulator('localhost', 5001);
httpsCallable = functions.httpsCallable('testFunctionAuthorized');
});

tearDown(() {
final config = StorageBox(app.options.projectId);

// Clear auth storage
StorageBox.instanceOf(app.options.projectId)
.putValue('${app.options.apiKey}:${app.name}', null);
config.remove('${app.options.apiKey}:${app.name}');
});
test('unauthorized access throws', () async {
expect(
Expand All @@ -167,7 +168,6 @@ Future<void> main() async {
.having((e) => e.code, 'code', contains('unauthenticated'))),
);
});

test('authorized access succeeds', () async {
final auth = FirebaseAuth.instanceFor(app: app);
await auth.useAuthEmulator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:firebase_core_dart/firebase_core_dart.dart';
import 'package:firebase_functions_dart/firebase_functions_dart.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:storagebox/storagebox.dart';
import 'package:test/test.dart';

import 'data.dart' as data;
Expand Down Expand Up @@ -379,18 +380,23 @@ Future<void> main() async {
options: firebaseOptions,
name: 'auth_functions',
);

// Clear auth storage
final config = StorageBox(app.options.projectId);

// Clear auth storage
StorageBox.instanceOf(app.options.projectId)
.putValue('${app.options.apiKey}:${app.name}', null);
config.remove('${app.options.apiKey}:${app.name}');
functions = FirebaseFunctions.instanceFor(app: app);
functions.setApiClient(MockClient(_authCheck));
httpsCallable = functions.httpsCallable('testFunctionAuthorized');
});

tearDown(() async {
// Clear auth storage
StorageBox.instanceOf(app.options.projectId)
.putValue('${app.options.apiKey}:${app.name}', null);
final config = StorageBox(app.options.projectId);

// Clear auth storage
config.remove('${app.options.apiKey}:${app.name}');
});

test('unauthorized access throws', () async {
Expand Down