0
0
Fork 0

マイページにログインしたままMisskey側で連携を解除するとアクセスできなくなる不具合を修正

This commit is contained in:
Xeltica 2020-09-10 00:56:39 +09:00
parent 260b4501e1
commit 36d8fd1d2c
2 changed files with 20 additions and 8 deletions

View file

@ -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<string, unknown>, 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)],
});
}
});

View file

@ -13,4 +13,13 @@ export const api = <T = Record<string, unknown>>(host: string, endpoint: string,
a.i = i;
}
return axios.post<T>(`https://${host}/api/${endpoint}`, a).then(res => res.data);
};
export const apiAvailable = async (host: string, i: string) : Promise<boolean> => {
try {
const res = await api(host, 'i', {}, i);
return !res.error;
} catch {
return false;
}
};