forked from MapComplete/MapComplete
Fix: revive missing 'login' button
This commit is contained in:
parent
34998b1082
commit
d9c8785d9b
3 changed files with 63 additions and 99 deletions
|
@ -17,10 +17,14 @@ interface OsmUserInfo {
|
||||||
changesets: { count: number }
|
changesets: { count: number }
|
||||||
traces: { count: number }
|
traces: { count: number }
|
||||||
blocks: { received: { count: number; active: number } }
|
blocks: { received: { count: number; active: number } }
|
||||||
|
img?: { href: string }
|
||||||
|
home: { lat: number, lon: number }
|
||||||
|
languages?: string[]
|
||||||
|
messages: { received: { count: number, unread: number }, sent: { count: number } }
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class UserDetails {
|
export default class UserDetails {
|
||||||
public name = "Not logged in"
|
public name
|
||||||
public uid: number
|
public uid: number
|
||||||
public csCount = 0
|
public csCount = 0
|
||||||
public img?: string
|
public img?: string
|
||||||
|
@ -91,7 +95,6 @@ export class OsmConnection {
|
||||||
public readonly _oauth_config: AuthConfig
|
public readonly _oauth_config: AuthConfig
|
||||||
private readonly _dryRun: Store<boolean>
|
private readonly _dryRun: Store<boolean>
|
||||||
private readonly fakeUser: boolean
|
private readonly fakeUser: boolean
|
||||||
private _onLoggedIn: ((userDetails: UserDetails) => void)[] = []
|
|
||||||
private readonly _iframeMode: boolean
|
private readonly _iframeMode: boolean
|
||||||
private readonly _singlePage: boolean
|
private readonly _singlePage: boolean
|
||||||
private isChecking = false
|
private isChecking = false
|
||||||
|
@ -128,7 +131,7 @@ export class OsmConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.userDetails = new UIEventSource<UserDetails>(
|
this.userDetails = new UIEventSource<UserDetails>(
|
||||||
new UserDetails(this._oauth_config.url),
|
undefined,
|
||||||
"userDetails"
|
"userDetails"
|
||||||
)
|
)
|
||||||
if (options.fakeUser) {
|
if (options.fakeUser) {
|
||||||
|
@ -197,15 +200,9 @@ export class OsmConnection {
|
||||||
return <UIEventSource<T>>this.preferencesHandler.getPreference(key, defaultValue, prefix)
|
return <UIEventSource<T>>this.preferencesHandler.getPreference(key, defaultValue, prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
public OnLoggedIn(action: (userDetails: UserDetails) => void) {
|
|
||||||
this._onLoggedIn.push(action)
|
|
||||||
}
|
|
||||||
|
|
||||||
public LogOut() {
|
public LogOut() {
|
||||||
this.auth.logout()
|
this.auth.logout()
|
||||||
this.userDetails.data.csCount = 0
|
this.userDetails.setData(undefined)
|
||||||
this.userDetails.data.name = ""
|
|
||||||
this.userDetails.ping()
|
|
||||||
console.log("Logged out")
|
console.log("Logged out")
|
||||||
this.loadingStatus.setData("not-attempted")
|
this.loadingStatus.setData("not-attempted")
|
||||||
}
|
}
|
||||||
|
@ -219,7 +216,7 @@ export class OsmConnection {
|
||||||
return this._oauth_config.url
|
return this._oauth_config.url
|
||||||
}
|
}
|
||||||
|
|
||||||
public AttemptLogin() {
|
public async AttemptLogin() {
|
||||||
this.UpdateCapabilities()
|
this.UpdateCapabilities()
|
||||||
if (this.loadingStatus.data !== "logged-in") {
|
if (this.loadingStatus.data !== "logged-in") {
|
||||||
// Stay 'logged-in' if we are already logged in; this simply means we are checking for messages
|
// Stay 'logged-in' if we are already logged in; this simply means we are checking for messages
|
||||||
|
@ -235,81 +232,46 @@ export class OsmConnection {
|
||||||
LocalStorageSource.get("location_before_login").setData(
|
LocalStorageSource.get("location_before_login").setData(
|
||||||
Utils.runningFromConsole ? undefined : window.location.href
|
Utils.runningFromConsole ? undefined : window.location.href
|
||||||
)
|
)
|
||||||
this.auth.xhr(
|
try {
|
||||||
{
|
|
||||||
method: "GET",
|
|
||||||
path: "/api/0.6/user/details",
|
|
||||||
},
|
|
||||||
(err, details: XMLDocument) => {
|
|
||||||
if (err != null) {
|
|
||||||
console.log("Could not login due to:", err)
|
|
||||||
this.loadingStatus.setData("error")
|
|
||||||
if (err.status == 401) {
|
|
||||||
console.log("Clearing tokens...")
|
|
||||||
// Not authorized - our token probably got revoked
|
|
||||||
this.auth.logout()
|
|
||||||
this.LogOut()
|
|
||||||
} else {
|
|
||||||
console.log("Other error. Status:", err.status)
|
|
||||||
this.apiIsOnline.setData("unreachable")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (details == null) {
|
const u = <OsmUserInfo>JSON.parse(await this.interact("user/details.json", "GET", {
|
||||||
this.loadingStatus.setData("error")
|
"accept-encoding": "application/json"
|
||||||
return
|
})).user
|
||||||
}
|
|
||||||
|
|
||||||
// details is an XML DOM of user details
|
if (!u) {
|
||||||
const userInfo = details.getElementsByTagName("user")[0]
|
this.loadingStatus.setData("error")
|
||||||
|
return
|
||||||
const data = this.userDetails.data
|
|
||||||
console.log("Login completed, userinfo is ", userInfo)
|
|
||||||
data.name = userInfo.getAttribute("display_name")
|
|
||||||
data.account_created = userInfo.getAttribute("account_created")
|
|
||||||
data.uid = Number(userInfo.getAttribute("id"))
|
|
||||||
data.languages = Array.from(
|
|
||||||
userInfo.getElementsByTagName("languages")[0].getElementsByTagName("lang")
|
|
||||||
).map((l) => l.textContent)
|
|
||||||
data.csCount = Number.parseInt(
|
|
||||||
userInfo.getElementsByTagName("changesets")[0].getAttribute("count") ?? "0"
|
|
||||||
)
|
|
||||||
data.tracesCount = Number.parseInt(
|
|
||||||
userInfo.getElementsByTagName("traces")[0].getAttribute("count") ?? "0"
|
|
||||||
)
|
|
||||||
|
|
||||||
data.img = undefined
|
|
||||||
const imgEl = userInfo.getElementsByTagName("img")
|
|
||||||
if (imgEl !== undefined && imgEl[0] !== undefined) {
|
|
||||||
data.img = imgEl[0].getAttribute("href")
|
|
||||||
}
|
|
||||||
|
|
||||||
const description = userInfo.getElementsByTagName("description")
|
|
||||||
if (description !== undefined && description[0] !== undefined) {
|
|
||||||
data.description = description[0]?.innerHTML
|
|
||||||
}
|
|
||||||
const homeEl = userInfo.getElementsByTagName("home")
|
|
||||||
if (homeEl !== undefined && homeEl[0] !== undefined) {
|
|
||||||
const lat = parseFloat(homeEl[0].getAttribute("lat"))
|
|
||||||
const lon = parseFloat(homeEl[0].getAttribute("lon"))
|
|
||||||
data.home = { lat: lat, lon: lon }
|
|
||||||
}
|
|
||||||
|
|
||||||
this.loadingStatus.setData("logged-in")
|
|
||||||
const messages = userInfo
|
|
||||||
.getElementsByTagName("messages")[0]
|
|
||||||
.getElementsByTagName("received")[0]
|
|
||||||
data.unreadMessages = parseInt(messages.getAttribute("unread"))
|
|
||||||
data.totalMessages = parseInt(messages.getAttribute("count"))
|
|
||||||
|
|
||||||
this.userDetails.ping()
|
|
||||||
for (const action of this._onLoggedIn) {
|
|
||||||
action(this.userDetails.data)
|
|
||||||
}
|
|
||||||
this._onLoggedIn = []
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
this.userDetails.set({
|
||||||
|
name: u.display_name,
|
||||||
|
img: u.img?.href,
|
||||||
|
unreadMessages: u.messages.received.unread,
|
||||||
|
tracesCount: u.traces.count,
|
||||||
|
uid: u.id,
|
||||||
|
account_created: u.account_created,
|
||||||
|
totalMessages: u.messages.received.count,
|
||||||
|
languages: u.languages,
|
||||||
|
home: u.home,
|
||||||
|
backend: this.Backend(),
|
||||||
|
description: u.description,
|
||||||
|
csCount: u.changesets.count
|
||||||
|
})
|
||||||
|
this.loadingStatus.setData("logged-in")
|
||||||
|
} catch (err) {
|
||||||
|
console.log("Could not login due to:", err)
|
||||||
|
this.loadingStatus.setData("error")
|
||||||
|
if (err.status == 401) {
|
||||||
|
console.log("Clearing tokens...")
|
||||||
|
// Not authorized - our token probably got revoked
|
||||||
|
this.auth.logout()
|
||||||
|
this.LogOut()
|
||||||
|
} else {
|
||||||
|
console.log("Other error. Status:", err.status)
|
||||||
|
this.apiIsOnline.setData("unreachable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -349,9 +311,9 @@ export class OsmConnection {
|
||||||
method,
|
method,
|
||||||
headers: header,
|
headers: header,
|
||||||
content,
|
content,
|
||||||
path: `/api/0.6/${path}`,
|
path: `/api/0.6/${path}`
|
||||||
},
|
},
|
||||||
function (err, response) {
|
function(err, response) {
|
||||||
if (err !== null) {
|
if (err !== null) {
|
||||||
error(err)
|
error(err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -431,7 +393,7 @@ export class OsmConnection {
|
||||||
"notes.json",
|
"notes.json",
|
||||||
content,
|
content,
|
||||||
{
|
{
|
||||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
|
||||||
},
|
},
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
@ -476,7 +438,7 @@ export class OsmConnection {
|
||||||
file: gpx,
|
file: gpx,
|
||||||
description: options.description,
|
description: options.description,
|
||||||
tags: options.labels?.join(",") ?? "",
|
tags: options.labels?.join(",") ?? "",
|
||||||
visibility: options.visibility,
|
visibility: options.visibility
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!contents.description) {
|
if (!contents.description) {
|
||||||
|
@ -484,9 +446,9 @@ export class OsmConnection {
|
||||||
}
|
}
|
||||||
const extras = {
|
const extras = {
|
||||||
file:
|
file:
|
||||||
'; filename="' +
|
"; filename=\"" +
|
||||||
(options.filename ?? "gpx_track_mapcomplete_" + new Date().toISOString()) +
|
(options.filename ?? "gpx_track_mapcomplete_" + new Date().toISOString()) +
|
||||||
'"\r\nContent-Type: application/gpx+xml',
|
"\"\r\nContent-Type: application/gpx+xml"
|
||||||
}
|
}
|
||||||
|
|
||||||
const boundary = "987654"
|
const boundary = "987654"
|
||||||
|
@ -494,7 +456,7 @@ export class OsmConnection {
|
||||||
let body = ""
|
let body = ""
|
||||||
for (const key in contents) {
|
for (const key in contents) {
|
||||||
body += "--" + boundary + "\r\n"
|
body += "--" + boundary + "\r\n"
|
||||||
body += 'Content-Disposition: form-data; name="' + key + '"'
|
body += "Content-Disposition: form-data; name=\"" + key + "\""
|
||||||
if (extras[key] !== undefined) {
|
if (extras[key] !== undefined) {
|
||||||
body += extras[key]
|
body += extras[key]
|
||||||
}
|
}
|
||||||
|
@ -505,7 +467,7 @@ export class OsmConnection {
|
||||||
|
|
||||||
const response = await this.post("gpx/create", body, {
|
const response = await this.post("gpx/create", body, {
|
||||||
"Content-Type": "multipart/form-data; boundary=" + boundary,
|
"Content-Type": "multipart/form-data; boundary=" + boundary,
|
||||||
"Content-Length": "" + body.length,
|
"Content-Length": "" + body.length
|
||||||
})
|
})
|
||||||
const parsed = JSON.parse(response)
|
const parsed = JSON.parse(response)
|
||||||
console.log("Uploaded GPX track", parsed)
|
console.log("Uploaded GPX track", parsed)
|
||||||
|
@ -526,9 +488,9 @@ export class OsmConnection {
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|
||||||
path: `/api/0.6/notes/${id}/comment?text=${encodeURIComponent(text)}`,
|
path: `/api/0.6/notes/${id}/comment?text=${encodeURIComponent(text)}`
|
||||||
},
|
},
|
||||||
function (err) {
|
function(err) {
|
||||||
if (err !== null) {
|
if (err !== null) {
|
||||||
error(err)
|
error(err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -543,7 +505,7 @@ export class OsmConnection {
|
||||||
* To be called by land.html
|
* To be called by land.html
|
||||||
*/
|
*/
|
||||||
public finishLogin(callback: (previousURL: string) => void) {
|
public finishLogin(callback: (previousURL: string) => void) {
|
||||||
this.auth.authenticate(function () {
|
this.auth.authenticate(function() {
|
||||||
// Fully authed at this point
|
// Fully authed at this point
|
||||||
console.log("Authentication successful!")
|
console.log("Authentication successful!")
|
||||||
const previousLocation = LocalStorageSource.get("location_before_login")
|
const previousLocation = LocalStorageSource.get("location_before_login")
|
||||||
|
@ -564,7 +526,7 @@ export class OsmConnection {
|
||||||
*/
|
*/
|
||||||
singlepage: !this._iframeMode,
|
singlepage: !this._iframeMode,
|
||||||
auto: autoLogin,
|
auto: autoLogin,
|
||||||
apiUrl: this._oauth_config.api_url ?? this._oauth_config.url,
|
apiUrl: this._oauth_config.api_url ?? this._oauth_config.url
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,9 +32,11 @@ export class OsmPreferences {
|
||||||
this.auth = auth
|
this.auth = auth
|
||||||
this._fakeUser = fakeUser
|
this._fakeUser = fakeUser
|
||||||
this.osmConnection = osmConnection
|
this.osmConnection = osmConnection
|
||||||
osmConnection.OnLoggedIn(() => {
|
this.osmConnection.isLoggedIn.addCallbackAndRunD(loggedIn => {
|
||||||
this.loadBulkPreferences()
|
if (loggedIn) {
|
||||||
return true
|
this.loadBulkPreferences()
|
||||||
|
return true
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -497,7 +497,7 @@ export default class UserRelatedState {
|
||||||
amendedPrefs.ping()
|
amendedPrefs.ping()
|
||||||
})
|
})
|
||||||
|
|
||||||
osmConnection.userDetails.addCallback((userDetails) => {
|
osmConnection.userDetails.addCallbackD((userDetails) => {
|
||||||
for (const k in userDetails) {
|
for (const k in userDetails) {
|
||||||
amendedPrefs.data["_" + k] = "" + userDetails[k]
|
amendedPrefs.data["_" + k] = "" + userDetails[k]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue