Created
September 1, 2019 06:58
-
-
Save ccfiel/c62724770ce1ee41633f531ec5e69913 to your computer and use it in GitHub Desktop.
colyseus auth to dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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