diff --git a/src/server/router.ts b/src/server/router.ts index c55f9df..08e8a73 100644 --- a/src/server/router.ts +++ b/src/server/router.ts @@ -7,7 +7,7 @@ import { die } from './die'; import { v4 as uuid } from 'uuid'; import { config } from '../config'; import { upsertUser, getUser, getUserCount, updateUser, updateUsersMisshaiToken, getUserByMisshaiToken, deleteUser } from '../functions/users'; -import { api } from '../services/misskey'; +import { api, apiAvailable } from '../services/misskey'; import { getScores } from '../functions/get-scores'; import { AlertMode, alertModes } from '../types/AlertMode'; import { Users } from '../models'; @@ -53,18 +53,21 @@ const login = async (ctx: Context, user: Record, host: string, router.get('/', async ctx => { const token = ctx.cookies.get('token', { signed: true }); const user = token ? await getUserByMisshaiToken(token) : undefined; - if (!user) { - // 非ログイン - await ctx.render('welcome', { - usersCount: await getUserCount(), - welcomeMessage: welcomeMessage[Math.floor(Math.random() * welcomeMessage.length)], - }); - } else { + + const isAvailable = user && await apiAvailable(user.host, user.token); + console.log(isAvailable); + if (user && isAvailable) { await ctx.render('mypage', { user, usersCount: await getUserCount(), score: await getScores(user), }); + } else { + // 非ログイン + await ctx.render('welcome', { + usersCount: await getUserCount(), + welcomeMessage: welcomeMessage[Math.floor(Math.random() * welcomeMessage.length)], + }); } }); diff --git a/src/services/misskey.ts b/src/services/misskey.ts index 52dbf89..64cbd93 100644 --- a/src/services/misskey.ts +++ b/src/services/misskey.ts @@ -13,4 +13,13 @@ export const api = >(host: string, endpoint: string, a.i = i; } return axios.post(`https://${host}/api/${endpoint}`, a).then(res => res.data); +}; + +export const apiAvailable = async (host: string, i: string) : Promise => { + try { + const res = await api(host, 'i', {}, i); + return !res.error; + } catch { + return false; + } }; \ No newline at end of file