Skip to content

Instantly share code, notes, and snippets.

@ccfiel
Created September 1, 2019 06:58
Show Gist options
  • Save ccfiel/c62724770ce1ee41633f531ec5e69913 to your computer and use it in GitHub Desktop.
Save ccfiel/c62724770ce1ee41633f531ec5e69913 to your computer and use it in GitHub Desktop.
colyseus auth to dart
import 'dart:io';
import 'package:http/http.dart' as http;
class Auth {
String token;
String endpoint;
Auth(String _endpoint) {
endpoint = _endpoint.replaceAll("ws", "http");
}
hasToken() {
return token != null;
}
login() {
Map<String, String> query;
this.request("POST", "/auth", query, "");
}
request(String method, String segments, Map<String, String> query,
String body) async {
if (query == null) query = {};
if (this.hasToken()) {
query["token"] = token;
}
List<String> queryParams = [];
query.forEach((key, value) => queryParams.add('$key=$value'));
var client = new http.Client();
var request = new http.Request(method, Uri.parse('$endpoint/$segments'));
if (body != null) {
request.headers[HttpHeaders.contentTypeHeader] =
'application/json; charset=utf-8';
request.body = body;
}
var response = await client.send(request);
response.stream.bytesToString().then((value) => print(value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment