Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter http Maintain PHP session

Tags:

flutter

I'm new to flutter. Basically I'm using code Igniter framework for my web application. I created REST API for my web app, after user login using API all the methods check for the session_id if it exists then it proceeds, and if it doesn't then it gives

{ ['status'] = false, ['message'] = 'unauthorized access' }

I'm creating app with flutter, when i use the http method of flutter it changes session on each request. I mean, it doesn't maintain the session. I think it destroys and creates new connection each time. Here is thr class method which i use for api calls get and post request.

class ApiCall {  
  static Map data;
  static List keys;

static Future<Map> getData(url) async {
 http.Response response = await http.get(url);
 Map  body =  JSON.decode(response.body);
 data = body;
 return body;
}

static Future postData(url, data) async {
Map result;    
http.Response response = await http.post(url, body: data).then((response) {
  result = JSON.decode(response.body);
}).catchError((error) => print(error.toString()));

data = result;
keys = result.keys.toList();

return result;  
}

I want to make API request and then store session_id, And is it possible to maintain session on the server so i can manage authentication on the web app it self.?

like image 210
user8854564 Avatar asked May 11 '18 19:05

user8854564


1 Answers

HTTP is a stateless protocol, so servers need some way to identify clients on the second, third and subsequent requests they make to the server. In your case you might authenticate using the first request, so you want the server to remember you on subsequent requests, so that it knows you are already authenticated. A common way to do this is with cookies.

Igniter sends a cookie with the session id. You need to gather this from each response and send it back in the next request. (Servers sometimes change the session id (to reduce things like clickjacking that we don't need to consider yet), so you need to keep extracting the cookie from every response.)

The cookie arrives as an HTTP response header called set-cookie (there may be more than one, though hopefully not for simplicity). To send the cookie back you add a HTTP request header to your subsequent requests called cookie, copying across some of the information you extracted from the set-cookie header.

Hopefully, Igniter only sends one set-cookie header, but for debugging purposes you may find it useful to print them all by using response.headers.forEach((a, b) => print('$a: $b'));. You should find Set-Cookie: somename=abcdef; optional stuff. We need to extract the string up to, but excluding the ;, i.e. somename=abcdef

On the next, and subsequent requests, add a request header to your next request of {'Cookie': 'somename=abcdef'}, by changing your post command to:

http.post(url, body: data, headers:{'Cookie': cookie})

Incidentally, I think you have a mismatch of awaits and thens in your code above. Generally, you don't want statics in classes, if they should be top level functions instead. Instead you could create a cookie aware class like:

class Session {
  Map<String, String> headers = {};

  Future<Map> get(String url) async {
    http.Response response = await http.get(url, headers: headers);
    updateCookie(response);
    return json.decode(response.body);
  }

  Future<Map> post(String url, dynamic data) async {
    http.Response response = await http.post(url, body: data, headers: headers);
    updateCookie(response);
    return json.decode(response.body);
  }

  void updateCookie(http.Response response) {
    String rawCookie = response.headers['set-cookie'];
    if (rawCookie != null) {
      int index = rawCookie.indexOf(';');
      headers['cookie'] =
          (index == -1) ? rawCookie : rawCookie.substring(0, index);
    }
  }
}
like image 188
Richard Heap Avatar answered Sep 19 '22 18:09

Richard Heap