WeeWeee
WeeWeee

Reputation: 11

How to perform Headless Login in dart

import 'dart:convert';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:http/http.dart' as http;

class HeadlessLogin {
  final String username;
  final String password;

  HeadlessLogin(this.username, this.password);

  Future<bool> performLogin() async {
    final authUrl = 'https://portal.giu-uni.de';
    final authHeaders = {
      'Authorization': 'Basic ${base64Encode(utf8.encode('$username:$password'))}'
    };

    try {
      final response = await http.get(Uri.parse(authUrl), headers: authHeaders);

      if (response.statusCode == 200) {
        final cookies = response.headers['set-cookie'];
        if (cookies == null) {
          print('Failed to retrieve session cookies');
          return false;
        }

        HeadlessInAppWebView? headlessWebView;
        headlessWebView = HeadlessInAppWebView(
          initialOptions: InAppWebViewGroupOptions(
            crossPlatform: InAppWebViewOptions(
              useShouldOverrideUrlLoading: true,
              useOnLoadResource: true,
              userAgent:
              'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
            ),
          ),
          initialUrlRequest: URLRequest(
            url: Uri.parse(authUrl),
            headers: {
              'Cookie': cookies, // Inject session cookies
            },
          ),
          onLoadStop: (controller, url) async {
            // Check if on dashboard or other page that indicates a successful login
            final currentUrl = (await controller.getUrl())?.toString();
            if (currentUrl != null && currentUrl.contains('dashboard')) {
              print('Login successful');
              await headlessWebView?.dispose();
              return;
            } else {
              print('Redirected to unexpected page: $currentUrl');
              await headlessWebView?.dispose();
              return;
            }
          },
        );

        // Run the headless browser
        await headlessWebView.run();

        // Wait for the page to fully load
        while ((await headlessWebView.webViewController.getProgress())! < 100) {
          await Future.delayed(Duration(milliseconds: 100));
        }

        // Check the final URL
        final currentUrl = (await headlessWebView.webViewController.getUrl())?.toString();
        if (currentUrl != null && currentUrl.contains('dashboard')) {
          return true;
        } else {
          return false;
        }
      } else {
        print('Authentication failed with status code ${response.statusCode}');
        return false;
      }
    } catch (e) {
      print('Error during login: $e');
      return false;
    }
  }
}

I'm making a flutter app and a part of it requires perform a headless login on a website, I've tried it in dart SEVERAL times but to no avail, I made it in python and it worked first try but using an entire tech stack would be very inconvenient since I know it can be done in all dart. I've tried it in multiple different angles, I check redirects, response codes, and other methods but none work.

Upvotes: 0

Views: 53

Answers (0)

Related Questions