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

chore(auth): Simplify web callback session #30

Merged
merged 1 commit into from
Aug 27, 2024
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
4 changes: 4 additions & 0 deletions packages/native/authentication/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.0+1

- chore: Prevent automatically redirecting on Web to allow time for persisting session state.

# 0.1.0

- Initial release
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:math';
import 'package:meta/meta.dart';

/// A session object that represents an ongoing authorization flow.
sealed class CallbackSession {
abstract class CallbackSession {
/// The unique identifier of this session.
int get id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import 'package:path/path.dart';
import 'package:web/web.dart';

final class NativeAuthenticationPlatform implements NativeAuthentication {
NativeAuthenticationPlatform({Logger? logger})
: _logger = logger ?? Logger('NativeAuthentication');

final Logger _logger;
NativeAuthenticationPlatform({Logger? logger});

/// The base URL, to which all local paths are relative.
// ignore: unused_element
Expand All @@ -20,22 +17,29 @@ final class NativeAuthenticationPlatform implements NativeAuthentication {
return url.join(window.location.origin, basePath);
}

static const _sessionStorageKey = 'dev.celest.native_auth:currentSession';

@override
CallbackSession startCallback({
required Uri uri,
required CallbackType type,
}) {
final sessionId = NativeAuthCallbackSessionImpl.nextId();
window.sessionStorage.setItem(_sessionStorageKey, '$sessionId');
final session = NativeAuthCallbackSessionImpl(
sessionId,
Completer<Uri>(),
() => window.sessionStorage.removeItem(_sessionStorageKey),
);
_logger.finer('Redirect flow started');
return _NativeAuthCallbackSessionWeb(uri);
}
}

final class _NativeAuthCallbackSessionWeb implements CallbackSession {
_NativeAuthCallbackSessionWeb(this.uri);

final Uri uri;

@override
void cancel() {}

@override
final int id = NativeAuthCallbackSessionImpl.nextId();

@override
Future<Never> get redirectUri async {
window.open(uri.toString(), '_self');
return session;
throw const NativeAuthException('Failed to redirect the user');
}
}
2 changes: 1 addition & 1 deletion packages/native/authentication/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: native_authentication
description: Native bindings for platform-specific authentication APIs like ASWebAuthenticationSession and Chrome Custom Tabs.
version: 0.1.0
version: 0.1.0+1
repository: https://github.com/celest-dev/dart-packages/tree/main/packages/native/authentication

environment:
Expand Down