diff --git a/app/src/main/java/telegra/ph/TelegraphApi.kt b/app/src/main/java/telegra/ph/TelegraphApi.kt index 271f11f..59c1159 100644 --- a/app/src/main/java/telegra/ph/TelegraphApi.kt +++ b/app/src/main/java/telegra/ph/TelegraphApi.kt @@ -10,6 +10,7 @@ import com.github.kittinunf.fuel.httpPost import com.github.kittinunf.result.Result import org.json.JSONArray import org.json.JSONObject +import java.net.HttpCookie object TelegraphApi { @@ -151,4 +152,23 @@ object TelegraphApi { } } + // Telegra.ph + + fun login(authUrl: String, callback: (success: Boolean, accessToken: String?, account: Account?) -> Unit) { + authUrl.httpPost().response { _, response, _ -> + var token: String? = null + response.headers["Set-Cookie"] + ?.flatMap { HttpCookie.parse(it) } + ?.find { it.name == "tph_token" } + ?.let { + token = it.value + } + if (token != null) getAccountInfo(accessToken = token!!) { success, account, _ -> + if (success) callback(true, token, account) + else callback(false, null, null) + } + else callback(false, null, null) + } + } + } \ No newline at end of file