@@ -254,76 +254,8 @@ class _ThunderState extends State<Thunder> {
254254 }
255255
256256 Future <void > _oauthCallback (String link) async {
257- // TODO: Need to know state, and instance.
258-
259257 try {
260- String redirectUri = "https://thunderapp.dev/oauth/callback" ;
261258 debugPrint ("_oauthCallback $link " );
262- // oauthProviderState must match oauthClientState to ensure the response came from the Provider.
263- String oauthProviderState = Uri .parse (link).queryParameters['state' ] ?? "failed" ;
264- if (oauthProviderState == "failed" || oauthClientState != oauthProviderState) {
265- throw Exception ("OAuth state-check failed: oauthProviderState $oauthClientState must match oauthClientState $oauthClientState to ensure the response came from the Provider." );
266- }
267-
268- // Extract the code from the response.
269- String code = Uri .parse (link).queryParameters['code' ] ?? "failed" ;
270-
271- debugPrint ("CODE $code " );
272-
273- if (code == "failed" ) {
274- throw Exception ("OAuth login failed: no code received from provider." );
275- }
276-
277- // TODO: This should use lemmy_api_client.
278- // Authenthicate to lemmy and get a jwt.
279- // Durring this step lemmy connects to the Provider to get the user info.
280- final response = await http.post (Uri .parse ('https://$instance /api/v3/oauth/authenticate' ),
281- headers: {
282- 'Content-Type' : 'application/json' ,
283- },
284- body: json.encode ({
285- 'code' : code,
286- 'oauth_provider_id' : 1 , // This id can be found in the site reponse.
287- 'redirect_uri' : redirectUri,
288- }),
289- encoding: Encoding .getByName ('utf-8' ));
290-
291- // TODO: Need to add a step to set the account username.
292-
293- final accessToken = jsonDecode (response.body)['jwt' ] as String ;
294-
295- debugPrint ("JWT $accessToken " );
296-
297- GetSiteResponse getSiteResponse = await lemmy.run (GetSite (auth: accessToken));
298-
299- // TODO: Login fails when this is uncommented. Have to get this working.
300- //if (event.showContentWarning && getSiteResponse.siteView.site.contentWarning?.isNotEmpty == true) {
301- // return emit(state.copyWith(status: AuthStatus.contentWarning, contentWarning: getSiteResponse.siteView.site.contentWarning));
302- //}
303-
304- // Create a new account in the database
305- Account ? account = Account (
306- id: '' ,
307- username: getSiteResponse.myUser? .localUserView.person.name,
308- jwt: accessToken,
309- instance: instance,
310- userId: getSiteResponse.myUser? .localUserView.person.id,
311- index: - 1 ,
312- );
313-
314- account = await Account .insertAccount (account);
315-
316- if (account == null ) {
317- return emit (state.copyWith (status: AuthStatus .failure, account: null , isLoggedIn: false ));
318- }
319-
320- // Set this account as the active account
321- SharedPreferences prefs = (await UserPreferences .instance).sharedPreferences;
322- prefs.setString ('active_profile_id' , account.id);
323-
324- bool downvotesEnabled = getSiteResponse.siteView.localSite.enableDownvotes ?? false ;
325-
326- return emit (state.copyWith (status: AuthStatus .success, account: account, isLoggedIn: true , downvotesEnabled: downvotesEnabled, getSiteResponse: getSiteResponse));
327259 } catch (e) {
328260 if (context.mounted) {
329261 _showLinkProcessingError (context, AppLocalizations .of (context)! .exceptionProcessingUri, link);
0 commit comments