signInWithMicrosoft method

Future<User?> signInWithMicrosoft({
  1. required String clientId,
  2. required String redirectUri,
})

Implementation

Future<User?> signInWithMicrosoft({required String clientId, required String redirectUri}) async {
  try {
    final result = await FlutterWebAuth.authenticate(
      url: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
          '?client_id=$clientId'
          '&response_type=code'
          '&redirect_uri=$redirectUri'
          '&response_mode=query'
          '&scope=openid profile User.Read'
          '&state=12345',
      callbackUrlScheme: Uri.parse(redirectUri).scheme,
    );

    final code = Uri.parse(result).queryParameters['code'];

    if (code != null) {
      final oauthCredential = OAuthProvider("microsoft.com").credential(
        accessToken: 'your-access-token', // Replace with actual token after exchanging the code
        idToken: 'your-id-token', // Replace with actual token if applicable
      );

      UserCredential userCredential = await _auth.signInWithCredential(oauthCredential);
      return userCredential.user;
    }
    return null;
  } catch (e) {
    print(e.toString());
    return null;
  }
}