Skip to content

Commit

Permalink
chore(auth): Simplify web callback session
Browse files Browse the repository at this point in the history
Only redirect when the URI has been requested so that there is time to persist state by the calling library.
  • Loading branch information
dnys1 committed Aug 27, 2024
1 parent ad4d969 commit 4479172
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
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

0 comments on commit 4479172

Please sign in to comment.