drupal_login.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Automatic FlutterFlow imports
  2. import '/backend/schema/structs/index.dart';
  3. import '/flutter_flow/flutter_flow_theme.dart';
  4. import '/flutter_flow/flutter_flow_util.dart';
  5. import 'index.dart'; // Imports other custom actions
  6. import '/flutter_flow/custom_functions.dart'; // Imports custom functions
  7. import 'package:flutter/material.dart';
  8. // Begin custom action code
  9. // DO NOT REMOVE OR MODIFY THE CODE ABOVE!
  10. import 'dart:convert';
  11. import 'package:http/http.dart' as http;
  12. Future<dynamic> drupalLogin(
  13. String username,
  14. String password,
  15. String loginUrl,
  16. ) async {
  17. try {
  18. final response = await http.post(
  19. Uri.parse(loginUrl),
  20. headers: {
  21. 'Content-Type': 'application/json',
  22. 'Accept': 'application/json',
  23. },
  24. body: jsonEncode({
  25. 'username': username,
  26. 'password': password,
  27. }),
  28. );
  29. if (response.statusCode == 200) {
  30. final data = jsonDecode(response.body);
  31. return {
  32. 'success': true,
  33. 'sessid': data['sessid'],
  34. 'session_name': data['session_name'],
  35. 'token': data['token'],
  36. 'uid': data['user']['uid']?.toString(),
  37. 'name': data['user']['name'],
  38. 'mail': data['user']['mail'],
  39. };
  40. } else {
  41. return {
  42. 'success': false,
  43. 'error': 'Login failed: ${response.statusCode}',
  44. 'body': response.body,
  45. };
  46. }
  47. } catch (e) {
  48. return {
  49. 'success': false,
  50. 'error': e.toString(),
  51. };
  52. }
  53. }
  54. // Set your action name, define your arguments and return parameter,
  55. // and then add the boilerplate code using the `</>` button on the right!