login method

  1. @override
Future<LoginResponse> login()
override

Performs a login in shadertoy website

Upon success error is null

In case of error the error field has the corresponding ResponseError structure

Implementation

@override
Future<LoginResponse> login() {
  var data =
      FormData.fromMap({'user': options.user, 'password': options.password});
  var clientOptions = Options(
      contentType:
          ContentType.parse('application/x-www-form-urlencoded').toString(),
      headers: {HttpHeaders.refererHeader: context.signInUrl},
      followRedirects: false,
      validateStatus: (int? status) => status == 302);

  if (options.supportsCookies) {
    clearCookies();
  }

  return catchDioError<LoginResponse>(
      client
          .post('/${ShadertoyContext.signInPath}',
              data: data, options: clientOptions)
          .then((Response<dynamic> response) {
        final headers = response.headers;
        final locationHeaders = headers.map[HttpHeaders.locationHeader];
        if (locationHeaders?.length == 1) {
          final location = locationHeaders?.single;
          if (location == '/') {
            return LoginResponse();
          } else if (location == '/${ShadertoyContext.signInPath}?error=1') {
            return LoginResponse(
                error: ResponseError.authentication(
                    message: 'Login error',
                    context: contextUser,
                    target: options.user));
          }
        }

        return LoginResponse(
            error: ResponseError.unknown(
                message: 'Invalid location header',
                context: contextUser,
                target: options.user));
      }), (de) {
    return LoginResponse(
        error: toResponseError(de)
            .copyWith(context: contextUser, target: options.user));
  });
}