forked from MapComplete/MapComplete
Anrdoid: add 'getToken' to OSM Connection
This commit is contained in:
parent
f25f5f156d
commit
3ec83483d8
2 changed files with 36 additions and 8 deletions
26
app/land.ts
26
app/land.ts
|
@ -3,22 +3,42 @@ import Constants from "../src/Models/Constants"
|
||||||
import { Utils } from "../src/Utils"
|
import { Utils } from "../src/Utils"
|
||||||
import { UIEventSource } from "../src/Logic/UIEventSource"
|
import { UIEventSource } from "../src/Logic/UIEventSource"
|
||||||
import { VariableUiElement } from "../src/UI/Base/VariableUIElement"
|
import { VariableUiElement } from "../src/UI/Base/VariableUIElement"
|
||||||
|
import Combine from "../src/UI/Base/Combine"
|
||||||
|
|
||||||
console.log("Authorizing...")
|
console.log("Authorizing...")
|
||||||
const key = Constants.osmAuthConfig.url + "oauth2_state"
|
const key = Constants.osmAuthConfig.url + "oauth2_state"
|
||||||
const st = window.localStorage.getItem(key)
|
const st = window.localStorage.getItem(key)
|
||||||
console.log("Prev state is", key, st)
|
console.log("Prev state is", key, st)
|
||||||
const tokenSrc = new UIEventSource("")
|
const tokenSrc = new UIEventSource("")
|
||||||
new VariableUiElement(tokenSrc).AttachTo("token")
|
const debug = new UIEventSource<string[]>([])
|
||||||
|
|
||||||
|
new Combine([
|
||||||
|
new VariableUiElement(debug.map(debug => "<ul><li>"+debug.join("</li><li>")+"</li></ul>")),
|
||||||
|
new VariableUiElement(tokenSrc)]).AttachTo("token")
|
||||||
|
|
||||||
|
|
||||||
new OsmConnection().finishLogin(async (_, token: string) => {
|
const connection = new OsmConnection()
|
||||||
|
connection.finishLogin(async () => {
|
||||||
|
let token: string = undefined
|
||||||
|
let attempt = 0
|
||||||
|
do {
|
||||||
|
await Utils.waitFor(500)
|
||||||
|
token = connection.getToken()
|
||||||
|
tokenSrc.set("Trying to get token ("+attempt+")")
|
||||||
|
attempt++
|
||||||
|
|
||||||
|
const dbg = []
|
||||||
|
Object.keys(localStorage).forEach(key => {
|
||||||
|
dbg.push(`${key} - ${localStorage.getItem(key)}`);
|
||||||
|
})
|
||||||
|
debug.set(dbg)
|
||||||
|
|
||||||
|
} while (!token)
|
||||||
console.log("Login finished, redirecting to passthrough; token is " + token)
|
console.log("Login finished, redirecting to passthrough; token is " + token)
|
||||||
tokenSrc.set(token)
|
tokenSrc.set(token)
|
||||||
if (!token) {
|
if (!token) {
|
||||||
tokenSrc.set("ERROR: no token retrieved!")
|
tokenSrc.set("ERROR: no token retrieved!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
await Utils.waitFor(500)
|
|
||||||
window.location.href = "orgmapcomplete://passthrough.html?oauth_token=" + token
|
window.location.href = "orgmapcomplete://passthrough.html?oauth_token=" + token
|
||||||
})
|
})
|
||||||
|
|
|
@ -278,7 +278,7 @@ export class OsmConnection {
|
||||||
Utils.runningFromConsole ? undefined : window.location.href,
|
Utils.runningFromConsole ? undefined : window.location.href,
|
||||||
)
|
)
|
||||||
|
|
||||||
this.auth.authenticate((err, result) => {
|
this.auth.authenticate((err) => {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
this.loadUserInfo()
|
this.loadUserInfo()
|
||||||
}
|
}
|
||||||
|
@ -564,16 +564,24 @@ export class OsmConnection {
|
||||||
/**
|
/**
|
||||||
* To be called by land.html
|
* To be called by land.html
|
||||||
*/
|
*/
|
||||||
public finishLogin(callback: (previousURL: string, oauth_token: string) => void) {
|
public finishLogin(callback: (previousURL: string) => void) {
|
||||||
this.auth.authenticate(() => {
|
this.auth.authenticate(() => {
|
||||||
// Fully authed at this point
|
// Fully authed at this point
|
||||||
console.log("Authentication successful!")
|
console.log("Authentication successful!")
|
||||||
const oauth_token = QueryParameters.GetQueryParameter("oauth_token", undefined).data ?? window.localStorage.getItem(this._oauth_config.url + "oauth2_access_token")
|
|
||||||
const previousLocation = LocalStorageSource.get("location_before_login")
|
const previousLocation = LocalStorageSource.get("location_before_login")
|
||||||
callback(previousLocation.data, oauth_token)
|
callback(previousLocation.data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getToken(): string {
|
||||||
|
// https://www.openstreetmap.orgoauth2_access_token
|
||||||
|
let prefix = this.Backend()
|
||||||
|
while(prefix.endsWith("/")){
|
||||||
|
prefix = prefix.substring(0, prefix.length-2)
|
||||||
|
}
|
||||||
|
return QueryParameters.GetQueryParameter(prefix+ "oauth_token", undefined).data ?? window.localStorage.getItem(this._oauth_config.url + "oauth2_access_token")
|
||||||
|
}
|
||||||
|
|
||||||
private async loginAndroidPolyfill() {
|
private async loginAndroidPolyfill() {
|
||||||
const token = await AndroidPolyfill.requestLoginCodes()
|
const token = await AndroidPolyfill.requestLoginCodes()
|
||||||
console.log("Got login token!", token)
|
console.log("Got login token!", token)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue