1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2024-12-29 15:11:58 +00:00
immich/mobile/lib/services/oauth.service.dart
Kenneth Bingham 2297d86569
fix(mobile): use a valid OAuth callback URL (#10832)
* add root resource path '/' to mobile oauth scheme

* chore: add oauth-callback path

* add root resource path '/' to mobile oauth scheme

* chore: add oauth-callback path

* fix: make sure there are three forward slash in callback URL

---------

Co-authored-by: Jason Rasmussen <jason@rasm.me>
Co-authored-by: Alex <alex.tran1502@gmail.com>
2024-08-28 11:30:06 -05:00

55 lines
1.5 KiB
Dart

import 'package:immich_mobile/services/api.service.dart';
import 'package:logging/logging.dart';
import 'package:openapi/api.dart';
import 'package:flutter_web_auth/flutter_web_auth.dart';
// Redirect URL = app.immich:///oauth-callback
class OAuthService {
final ApiService _apiService;
final callbackUrlScheme = 'app.immich';
final log = Logger('OAuthService');
OAuthService(this._apiService);
Future<String?> getOAuthServerUrl(
String serverUrl,
) async {
// Resolve API server endpoint from user provided serverUrl
await _apiService.resolveAndSetEndpoint(serverUrl);
final redirectUri = '$callbackUrlScheme:///oauth-callback';
log.info(
"Starting OAuth flow with redirect URI: $redirectUri",
);
final dto = await _apiService.oAuthApi.startOAuth(
OAuthConfigDto(redirectUri: redirectUri),
);
final authUrl = dto?.url;
log.info('Received Authorization URL: $authUrl');
return authUrl;
}
Future<LoginResponseDto?> oAuthLogin(String oauthUrl) async {
String result = await FlutterWebAuth.authenticate(
url: oauthUrl,
callbackUrlScheme: callbackUrlScheme,
);
log.info('Received OAuth callback: $result');
if (result.startsWith('app.immich:/oauth-callback')) {
result = result.replaceAll(
'app.immich:/oauth-callback',
'app.immich:///oauth-callback',
);
}
return await _apiService.oAuthApi.finishOAuth(
OAuthCallbackDto(
url: result,
),
);
}
}