Use client class instead
Add getter/setter to authorized clients Save new account client to Client.authorizedHTTP
This commit is contained in:
parent
c22484db7c
commit
f06327c1e4
|
@ -3,6 +3,8 @@ import Mastodon, { Response, Account, OAuth } from "megalodon"
|
||||||
import { join } from "path"
|
import { join } from "path"
|
||||||
import Datastore from "nedb"
|
import Datastore from "nedb"
|
||||||
|
|
||||||
|
import Client from "./Client"
|
||||||
|
|
||||||
interface AccountDoc {
|
interface AccountDoc {
|
||||||
_id?: string
|
_id?: string
|
||||||
domain: string
|
domain: string
|
||||||
|
@ -59,9 +61,8 @@ export default class Auth {
|
||||||
ipcMain.on(
|
ipcMain.on(
|
||||||
"new-account-auth",
|
"new-account-auth",
|
||||||
async (event: Event, code: string, instance: string) => {
|
async (event: Event, code: string, instance: string) => {
|
||||||
let url: string = "https://" + instance
|
|
||||||
try {
|
try {
|
||||||
let tokenData: Partial<{ accessToken: string }> = await Mastodon.fetchAccessToken(clientId, clientSecret, code, url)
|
let tokenData: Partial<{ accessToken: string }> = await Mastodon.fetchAccessToken(clientId, clientSecret, code, "https://" + instance)
|
||||||
if (tokenData.accessToken === undefined) {
|
if (tokenData.accessToken === undefined) {
|
||||||
event.sender.send(`error`, {
|
event.sender.send(`error`, {
|
||||||
id: "ERROR_GET_TOKEN",
|
id: "ERROR_GET_TOKEN",
|
||||||
|
@ -70,10 +71,7 @@ export default class Auth {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new Mastodon(
|
const client = Client.createAuthClient('http', instance, tokenData.accessToken)
|
||||||
tokenData.accessToken,
|
|
||||||
url + "/api/v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
let resp: Response<Account> = await client.get<Account>("/accounts/verify_credentials")
|
let resp: Response<Account> = await client.get<Account>("/accounts/verify_credentials")
|
||||||
let you = resp.data
|
let you = resp.data
|
||||||
|
@ -98,6 +96,7 @@ export default class Auth {
|
||||||
message: "You cannot login already logined account."
|
message: "You cannot login already logined account."
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
Client.setAuthClient('http', `@${newDocs.acct}@${newDocs.domain}`, client)
|
||||||
event.sender.send(`login-complete`, newDocs)
|
event.sender.send(`login-complete`, newDocs)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -12,6 +12,22 @@ export default class Clients {
|
||||||
private static nonAuthorizedHTTP: Map<string, Mastodon> = new Map()
|
private static nonAuthorizedHTTP: Map<string, Mastodon> = new Map()
|
||||||
private static nonAuthorizedWebsocket: Map<string, Mastodon> = new Map()
|
private static nonAuthorizedWebsocket: Map<string, Mastodon> = new Map()
|
||||||
|
|
||||||
|
public static getAuthClient(username: string, protocol: Protocol = 'http'): Mastodon {
|
||||||
|
let clients = protocol === 'http' ? this.authorizedHTTP : this.authorizedWebSocket
|
||||||
|
|
||||||
|
if (!clients.has(username)) {
|
||||||
|
// usernameからドメインをとトークンをデータベースから取得してクライアントを作る
|
||||||
|
//this.setAuthClient(protocol, username, this.createAuthClient(protocol, domain, accessToken))
|
||||||
|
}
|
||||||
|
|
||||||
|
return clients.get(username)!
|
||||||
|
}
|
||||||
|
|
||||||
|
public static setAuthClient(protocol: Protocol, username: string, client: Mastodon) {
|
||||||
|
let clients = protocol === 'http' ? this.nonAuthorizedHTTP : this.nonAuthorizedWebsocket
|
||||||
|
clients.set(username, client)
|
||||||
|
}
|
||||||
|
|
||||||
public static createAuthClient(protocol: Protocol, domain: string, accessToken: string): Mastodon {
|
public static createAuthClient(protocol: Protocol, domain: string, accessToken: string): Mastodon {
|
||||||
let scheme = protocol === 'http' ? 'https://' : 'wss://'
|
let scheme = protocol === 'http' ? 'https://' : 'wss://'
|
||||||
return new Mastodon(
|
return new Mastodon(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user