Skip to content

Commit

Permalink
Merge pull request #2259 from nextcloud/fix/neon_framework/qrcode-log…
Browse files Browse the repository at this point in the history
…in-routes
  • Loading branch information
provokateurin authored Jul 13, 2024
2 parents 0faa642 + 96d35d4 commit 8f54595
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 53 deletions.
2 changes: 1 addition & 1 deletion packages/neon_framework/lib/src/pages/login_qr_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class _LoginQRcodePageState extends State<LoginQRcodePage> {
throw const InvalidQRcodeException();
}

await LoginCheckServerStatusRoute.withCredentials(
await LoginCheckServerStatusWithCredentialsRoute(
serverUrl: match.serverURL,
loginName: match.username,
password: match.password,
Expand Down
109 changes: 61 additions & 48 deletions packages/neon_framework/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ GoRouter buildAppRouter({

final loginQRcode = LoginQRcode.tryParse(state.uri.toString());
if (loginQRcode != null) {
return LoginCheckServerStatusRoute.withCredentials(
return LoginCheckServerStatusWithCredentialsRoute(
serverUrl: loginQRcode.serverURL,
loginName: loginQRcode.username,
password: loginQRcode.password,
Expand Down Expand Up @@ -229,6 +229,9 @@ class HomeRoute extends GoRouteData {
TypedGoRoute<LoginCheckServerStatusRoute>(
path: 'check/server',
),
TypedGoRoute<LoginCheckServerStatusWithCredentialsRoute>(
path: 'check/server/:loginName/:password',
),
TypedGoRoute<LoginCheckAccountRoute>(
path: 'check/account',
),
Expand Down Expand Up @@ -327,51 +330,70 @@ class LoginQRcodeRoute extends GoRouteData {
class LoginCheckServerStatusRoute extends GoRouteData {
/// {@macro AppRoutes.LoginCheckServerStatusRoute}
///
/// [loginName] and [password] must both be null.
/// Use [LoginCheckServerStatusRoute.withCredentials] to specify credentials.
/// Use [LoginCheckServerStatusWithCredentialsRoute] to specify credentials.
const LoginCheckServerStatusRoute({
required this.serverUrl,
this.loginName,
this.password,
}) : assert(
loginName == null && password == null,
'loginName and password must be null. Use LoginCheckServerStatusRoute.withCredentials instead.',
);

/// {@macro AppRoutes.LoginCheckServerStatusRoute}
///
/// See [LoginCheckServerStatusRoute] for a route without initial credentials.
const LoginCheckServerStatusRoute.withCredentials({
required this.serverUrl,
required String this.loginName,
required String this.password,
});

/// {@macro AppRoutes.LoginFlow.serverUrl}
final Uri serverUrl;

@override
Widget build(BuildContext context, GoRouterState state) {
return LoginCheckServerStatusPage(
serverURL: serverUrl,
);
}

@override
FutureOr<String?> redirect(BuildContext context, GoRouterState state) {
final hasAccounts = NeonProvider.of<AccountsBloc>(context).hasAccounts;

if (state.fullPath == location && hasAccounts) {
return _AddAccountCheckServerStatusRoute(
serverUrl: serverUrl,
).location;
}

return null;
}
}

/// {@template AppRoutes.LoginCheckServerStatusWithCredentialsRoute}
/// Route for the [LoginCheckServerStatusPage].
///
/// Redirects to [_AddAccountCheckServerStatusWithCredentialsRoute] when at least one account
/// is already logged in.
/// {@endtemplate}
@immutable
class LoginCheckServerStatusWithCredentialsRoute extends LoginCheckServerStatusRoute {
/// {@macro AppRoutes.LoginCheckServerStatusWithCredentialsRoute}
const LoginCheckServerStatusWithCredentialsRoute({
required super.serverUrl,
required this.loginName,
required this.password,
});

/// {@macro AppRoutes.LoginFlow.serverUrl}
@override
Uri get serverUrl => super.serverUrl;

/// {@template AppRoutes.LoginFlow.loginName}
/// The login name of the credentials.
/// {@endtemplate}
final String? loginName;
final String loginName;

/// {@template AppRoutes.LoginFlow.password}
/// The password of the credentials.
/// {@endtemplate}
final String? password;
final String password;

@override
Widget build(BuildContext context, GoRouterState state) {
if (loginName != null && password != null) {
return LoginCheckServerStatusPage.withCredentials(
serverURL: serverUrl,
loginName: loginName!,
password: password!,
);
}

return LoginCheckServerStatusPage(
return LoginCheckServerStatusPage.withCredentials(
serverURL: serverUrl,
loginName: loginName,
password: password,
);
}

Expand All @@ -380,16 +402,10 @@ class LoginCheckServerStatusRoute extends GoRouteData {
final hasAccounts = NeonProvider.of<AccountsBloc>(context).hasAccounts;

if (state.fullPath == location && hasAccounts) {
if (loginName != null && password != null) {
return _AddAccountCheckServerStatusRoute.withCredentials(
serverUrl: serverUrl,
loginName: loginName!,
password: password!,
).location;
}

return _AddAccountCheckServerStatusRoute(
return _AddAccountCheckServerStatusWithCredentialsRoute(
serverUrl: serverUrl,
loginName: loginName,
password: password,
).location;
}

Expand Down Expand Up @@ -472,20 +488,17 @@ class _AddAccountCheckServerStatusRoute extends LoginCheckServerStatusRoute {
required super.serverUrl,
});

const _AddAccountCheckServerStatusRoute.withCredentials({
required super.serverUrl,
required super.loginName,
required super.password,
}) : super.withCredentials();

@override
Uri get serverUrl => super.serverUrl;
}

@override
String? get loginName => super.loginName;

@override
String? get password => super.password;
@immutable
class _AddAccountCheckServerStatusWithCredentialsRoute extends LoginCheckServerStatusWithCredentialsRoute {
const _AddAccountCheckServerStatusWithCredentialsRoute({
required super.serverUrl,
required super.loginName,
required super.password,
});
}

@immutable
Expand Down
32 changes: 28 additions & 4 deletions packages/neon_framework/lib/src/router.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f54595

Please sign in to comment.