From f20d07905ad1ccb904436d24a4a5d4671eaf430c Mon Sep 17 00:00:00 2001 From: Cutls Date: Thu, 25 Apr 2019 23:39:45 +0900 Subject: [PATCH] Add: insert account data to db --- src/main/Auth.ts | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/Auth.ts b/src/main/Auth.ts index 93d35ddd..901ea596 100644 --- a/src/main/Auth.ts +++ b/src/main/Auth.ts @@ -1,5 +1,5 @@ import { ipcMain, Event, shell, app } from "electron" -import Mastodon, { Status, Response } from "megalodon" +import Mastodon, { Response, Account } from "megalodon" import { join } from "path" import Datastore from "nedb" import Window from "./Window" @@ -33,14 +33,14 @@ export default class Auth { } ) } else { - Window.windowMap.get("main")!.webContents.send(`error`, { + event.sender.send(`error`, { id: "ERROR_GET_AUTHURL", message: "Failed to get auth URL to login." }) } }) .catch((err: Error) => - Window.windowMap.get("main")!.webContents.send(`error`, { + event.sender.send(`error`, { id: "ERROR_CONNECTION", message: "Connection error", meta: err @@ -60,23 +60,39 @@ export default class Auth { ) client - .get<[Status]>("/accounts/verify_credentials") - .then((resp: Response<[Status]>) => { - console.log(resp.data) - var db = new Datastore({ - filename: join(app.getPath("userData"), "account.db") + .get("/accounts/verify_credentials") + .then((resp: Response) => { + let you = resp.data + let db = new Datastore({ + filename: join(app.getPath("userData"), "account.db"), + autoload: true + }) + let docs = { + acct: you.acct, + avatar: you.avatar, + avatarStatic: you.avatar_static, + accessToken: tokenData.accessToken + } + db.insert(docs, function(err, newDocs) { + if(err){ + event.sender.send(`error`, { + id: "ERROR_YOU_TRY_ANOTHER_ACCOUNT", + message: "You cannot login already logined account." + }) + }else{ + event.sender.send(`login-complete`, newDocs) + } }) - db.loadDatabase() }) } else { - Window.windowMap.get("main")!.webContents.send(`error`, { + event.sender.send(`error`, { id: "ERROR_GET_TOKEN", message: "Failed to get access token." }) } }) .catch((err: Error) => - Window.windowMap.get("main")!.webContents.send(`error`, { + event.sender.send(`error`, { id: "ERROR_CONNECTION", message: "Connection error", meta: err