ORY on flutter Android/iOS

yacmzcpb  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(230)

我正在尝试在我的android/ios应用程序中使用ORY登录。没有在网上找到任何例子,从web开始的一个,但不工作.这就是我所尝试的:

import 'dart:developer';

import 'package:dio/dio.dart';
import 'package:dio/native_imp.dart';
import 'package:ory_client/ory_client.dart';

class AuthService {
  final FrontendApi _ory;
  Session? _identity;

  AuthService(Dio dio) : _ory = OryClient(dio: dio).getFrontendApi();

  static AuthService init() {
    const baseUrl = "https://{my.slug}.projects.oryapis.com";

    // create the dio client for http requests
    final options = BaseOptions(
      baseUrl: baseUrl,
      connectTimeout: 10000,
      receiveTimeout: 5000,
      headers: {
        "Accept": "application/json",
      },
      validateStatus: (status) {
        // here we prevent the request from throwing an error when the status code is less than 500 (internal server error)
        return status! < 500;
      },
    );
    final dio = DioForNative(options);
    // final adapter = BrowserHttpClientAdapter();
    // enable cookies support
    // we need this so we can send HTTP requests to the server with the cookies stored in the browser
    // adapter.withCredentials = true;
    // dio.httpClientAdapter = adapter;

    return AuthService(dio);
  }

  Future<bool> isAuthenticated() async {
    try {
      final resp = await _ory.toSession();
      if (resp.statusCode == 200) {
        _identity = resp.data;
        return true;
      }
      return false;
    } catch (error) {
      log(error.toString());
      return false;
    }
  }

  Future login() async {
    try {
      final resp = await _ory.createNativeLoginFlow();
      log(resp.toString());
      final sdf = await _ory.updateLoginFlow(
        flow: "login", updateLoginFlowBody: UpdateLoginFlowBody());
      log(sdf.toString());
    } catch (error) {
      log(error.toString());
    }
  }

  Future logout() async {
    try {
      final resp = await _ory.performNativeLogout(
          performNativeLogoutBody: PerformNativeLogoutBody());
      final sdf = await _ory.updateLogoutFlow(token: "resp.data!.logoutToken TODO");
      log(resp.toString());
      log(sdf.toString());
    } catch (error) {
      log(error.toString());
    }
  }

  Session? get identity => _identity;
}

我在await _ory.createNativeLoginFlow()上得到DeserializationError。有人有一个工作的例子吗?

c3frrgcw

c3frrgcw1#

我发现用final dio = Dio(options)替换final dio = DioForNative(options)不会抛出这个错误。

相关问题