Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casdoor.getUserInfo not working #45

Closed
zHElEARN opened this issue Dec 28, 2024 · 1 comment · Fixed by #46
Closed

Casdoor.getUserInfo not working #45

zHElEARN opened this issue Dec 28, 2024 · 1 comment · Fixed by #46
Assignees
Labels
bug Something isn't working

Comments

@zHElEARN
Copy link
Contributor

Code Example

Here’s the code I’m using:

import 'dart:convert';

import 'package:casdoor_flutter_sdk/casdoor_flutter_sdk.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';

final AuthConfig authConfig = AuthConfig(
  clientId: "",
  serverUrl: "",
  organizationName: "",
  appName: "",
  callbackUrlScheme: "",
  redirectUri: "",
);

final Casdoor casdoor = Casdoor(config: authConfig);

class AuthModel with ChangeNotifier {
  bool isAuthenticated = false;
  String? username;
  String? id;
  String accessToken = "";

  AuthModel() {
    init();
  }

  Future<void> init() async {
    // pass
    notifyListeners();
  }

  Future<void> signIn(BuildContext buildContext) async {
    String result = "";
    try {
      result = await casdoor.showFullscreen(buildContext);
      debugPrint("result: $result");

      final String code = Uri.parse(result).queryParameters["code"] ?? "";
      final Response response = await casdoor.requestOauthAccessToken(code);
      accessToken = jsonDecode(response.body)["access_token"] as String;

      SharedPreferences prefs = await SharedPreferences.getInstance();
      await prefs.setString("accessToken", accessToken);

      isAuthenticated = true;

      await fetchInfos();
    } catch (e) {
      debugPrint(e.toString());
    }
    notifyListeners();
  }

  Future<void> signOut() async {
    await casdoor.tokenLogout(accessToken, "", "logout", clearCache: true);

    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.remove("accessToken");

    isAuthenticated = false;

    notifyListeners();
  }

  Future<void> fetchInfos() async {
    final Response response = await casdoor.getUserInfo(accessToken);
    debugPrint(response.body);

    notifyListeners();
  }
}

Problem

When calling fetchInfos, the output is consistently:

I/flutter ( 6479): {
I/flutter ( 6479):   "status": "error",
I/flutter ( 6479):   "msg": "Unauthorized operation",
I/flutter ( 6479):   "data": null,
I/flutter ( 6479):   "data2": null
I/flutter ( 6479): }

Investigation

Looking at the getUserInfo method in the Casdoor Flutter SDK, it makes a POST request to /api/userinfo:

Future<http.Response> getUserInfo(String accessToken) async {
  return await http.post(
    Uri(
      scheme: parseScheme(),
      host: parseHost(),
      port: parsePort(),
      path: 'api/userinfo',
    ),
    headers: {'Authorization': 'Bearer $accessToken'},
  );
}

However, upon reviewing the Swagger API documentation at [this link](https://demo.casdoor.com/swagger/#/Account%20API/ApiController.UserInfo), it indicates that /api/userinfo should be a GET request, not a POST request.

Question

Should the getUserInfo method in the SDK be modified to use a GET request instead of POST?

@zHElEARN
Copy link
Contributor Author

I’ve submitted a PR to address this issue: #46

Could you confirm if the change to GET is correct?

@hsluoyz hsluoyz self-assigned this Dec 28, 2024
@hsluoyz hsluoyz added the bug Something isn't working label Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants