drupal_login.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. FFAppState().userSessionid = data['sessid']?.toString() ?? '';
  32. FFAppState().userSessionname = data['session_name']?.toString() ?? '';
  33. FFAppState().userToken = data['token']?.toString() ?? '';
  34. FFAppState().userName = data['user']?['name']?.toString() ?? '';
  35. FFAppState().userUid =
  36. int.tryParse(data['user']?['uid']?.toString() ?? '') ?? 0;
  37. FFAppState().userMail = data['user']?['mail']?.toString() ?? '';
  38. return {
  39. 'success': true,
  40. 'sessid': data['sessid'],
  41. 'session_name': data['session_name'],
  42. 'token': data['token'],
  43. 'uid': data['user']['uid']?.toString(),
  44. 'name': data['user']['name'],
  45. 'mail': data['user']['mail'],
  46. };
  47. } else {
  48. return {
  49. 'success': false,
  50. 'error': 'Login failed: ${response.statusCode}',
  51. 'body': response.body,
  52. };
  53. }
  54. } catch (e) {
  55. return {
  56. 'success': false,
  57. 'error': e.toString(),
  58. };
  59. }
  60. }
  61. // Set your action name, define your arguments and return parameter,
  62. // and then add the boilerplate code using the `</>` button on the right!