Skip to content

Commit

Permalink
working better now
Browse files Browse the repository at this point in the history
  • Loading branch information
gwbischof committed Dec 22, 2024
1 parent 1c40cb3 commit aa8e6e9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
22 changes: 8 additions & 14 deletions lib/core/auth/bloc/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
on<OAuthLoginAttempt>((event, emit) async {
LemmyClient lemmyClient = LemmyClient.instance;
String originalBaseUrl = lemmyClient.lemmyApiV3.host;
HttpServer? server;

try {
emit(state.copyWith(status: AuthStatus.loading, account: null, isLoggedIn: false));
Expand All @@ -223,23 +224,15 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
'state': oauthClientState,
});

// Start http server to receive callback.
// TODO: Figure out how to do this in a better cross-platform way. Maybe, https://pub.dev/packages/app_links
//Directory.current = path.dirname(Platform.script.toFilePath());

// Start https server to receive callback. This is just for development.
// TODO: Figure out how to do this in a better way for mobile.
var chain = utf8.encode(await rootBundle.loadString('assets/localhost.crt'));
var key = utf8.encode(await rootBundle.loadString('assets/localhost.key'));
var serverContext = SecurityContext();
serverContext.useCertificateChainBytes(chain);
serverContext.usePrivateKeyBytes(key);

var server = await HttpServer.bindSecure("localhost", 40000, serverContext);
// await server.forEach((HttpRequest request) {
// request.response.write('Hello, world!');
// request.response.close();
// });

//HttpServer server = await HttpServer.bind("localhost", 40000);
server = await HttpServer.bindSecure("localhost", 40000, serverContext);
//server = await HttpServer.bind("localhost", 40000);

// Present the dialog to the user.
final result = FlutterWebAuth2.authenticate(url: url.toString(), callbackUrlScheme: "thunder");
Expand Down Expand Up @@ -267,7 +260,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
// TODO: This should use lemmy_api_client.
// Authenthicate to lemmy and get a jwt.
// Durring this step lemmy with connect to the Provider to get the user info.
final response = await http.post(Uri.parse('https://hopandzip.com/api/v3/oauth/authenticate'),
final response = await http.post(Uri.parse('https://$instance/api/v3/oauth/authenticate'),
headers: {
'Content-Type': 'application/json',
},
Expand All @@ -285,7 +278,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {

GetSiteResponse getSiteResponse = await lemmy.run(GetSite(auth: accessToken));

// TODO: Login fails when this is uncommented.
// TODO: Login fails when this is uncommented. Have to get this working.
//if (event.showContentWarning && getSiteResponse.siteView.site.contentWarning?.isNotEmpty == true) {
// return emit(state.copyWith(status: AuthStatus.contentWarning, contentWarning: getSiteResponse.siteView.site.contentWarning));
//}
Expand Down Expand Up @@ -317,6 +310,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
return emit(state.copyWith(status: AuthStatus.failure, account: null, isLoggedIn: false, errorMessage: e.toString()));
} catch (e) {
try {
await server!.close();
// Restore the original baseUrl
lemmyClient.changeBaseUrl(originalBaseUrl);
} catch (e, s) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/singletons/lemmy_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LemmyClient {
LemmyClient._initialize();

void changeBaseUrl(String baseUrl) {
lemmyApiV3 = LemmyApiV3(baseUrl, tls: false, debug: true);
lemmyApiV3 = LemmyApiV3(baseUrl, tls: true, debug: true);
_populateSiteInfo(); // Do NOT await this. Let it populate in the background.
}

Expand Down
1 change: 1 addition & 0 deletions lib/thunder/pages/thunder_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class _ThunderState extends State<Thunder> {
@override
void initState() {
super.initState();
context.read<AuthBloc>().add(const LogOutOfAllAccounts());

selectedPageIndex = widget.pageController.initialPage;

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Future<GetInstanceInfoResponse> getInstanceInfo(String? url, {int? id, Duration?

try {
// TODO: need to remove tls and debug args, this is just for testing.
final site = await LemmyApiV3(url!, tls: false, debug: true).run(const GetSite()).timeout(timeout ?? const Duration(seconds: 5));
final site = await LemmyApiV3(url!, tls: true, debug: true).run(const GetSite()).timeout(timeout ?? const Duration(seconds: 5));
return GetInstanceInfoResponse(
success: true,
icon: site.siteView.site.icon,
Expand Down

0 comments on commit aa8e6e9

Please sign in to comment.