0

I'm trying to create a register page in flutter. Using HTTP package to post data to back end and in turn i'm receiving status message. I need to access the session_id(and csrf value) stored in cache of the API, so that i can set its value in shared preference for further session management. my code to post data is:

 Future registeruser(String firstname, String lastname,String email,String password,int 
 phone) async {
  String apiUrl="api here";
  final body ={"firstname":firstname,
    "lastname":lastname,
    "email":email,
    "password":password,
    "phone":phone,

  };
  final response= await http.post(apiUrl,headers:{'Content-type': 
 'Application/json','Accept':'Application/json'} ,body:body );
  var convertedDatatoJson= jsonDecode(response.body);
  return convertedDatatoJson;

 }


 var res =await registeruser(firstname, lastname, email, password, phone); 
 if(res.containsKey('status')){
                              setState(() {
                                message=res['status'];
                              });
                              if(res['status']==1){

                                 Navigator.pop(context);
                              }
                              else{
                                print('error');
                              }
                            }
                            }}, 

but how do i get the data from cache of the API and store in shared preferences, i'm quite new to flutter please help me.

1 Answers1

0

If you are using Laravel, make sure the endpoint called by your registeruser returns the session and csrf:

function register(Request $request) {
   return([ 
      "session" => $request->session()->get('key') ,
      "csrf" => csrf(),
   ]);
}

On Flutter, do the registration as usual

Map res = registeruser(...);

For user session check out FlutterSession. The package adds user session support in Flutter and is easy to use.

// Store value to session
await FlutterSession().set("session", res["session"]);
await FlutterSession().set("csrf", res["csrf"]);

// Retrieve item from session
dynamic token = await FlutterSession().get("session");
dynamic token = await FlutterSession().get("csrf");
Jhourlad Estrella
  • 3,545
  • 4
  • 37
  • 66
  • Hey, @Jhourlad can check on this please. https://stackoverflow.com/questions/73119121/laravel-api-integration-in-flutter-problem – Sandeep Sharma Jul 26 '22 at 11:00