사이트 로그인: 앱에서 녹음할 서버로 고른 서버 멤버만 (아니면 '봇과 다른 서버' 안내)
- 앱이 서버를 알려 준 뒤에는 ALLOWED_GUILD_IDS 대신 그 서버 하나만 허용 - 앱에서 녹음할 서버를 바꾸면 사이트 서버에 바로 다시 알린다 (hello) - 로그인 화면: GET /auth/guild 로 서버 이름 안내 - 테스트 2개 (login-guild.test.ts) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -71,11 +71,11 @@ export async function buildServer(
|
||||
return payload;
|
||||
});
|
||||
|
||||
/** 사이트에 들어올 수 있는 서버: 게임 PC 앱에서 녹음할 서버로 고른 곳 하나.
|
||||
* 앱이 아직 한 번도 알려 주지 않았을 때만 ALLOWED_GUILD_IDS 를 쓴다 */
|
||||
const allowedGuilds = (): string[] => {
|
||||
const g = new Set(cfg.allowedGuilds);
|
||||
const dg = store.device()?.guildId;
|
||||
if (dg) g.add(dg);
|
||||
return [...g];
|
||||
return dg ? [dg] : cfg.allowedGuilds;
|
||||
};
|
||||
|
||||
/** 세션 확인. 10분마다 서버 멤버인지 다시 확인 (서버에서 나가면 바로 막힘) */
|
||||
@@ -191,6 +191,12 @@ export async function buildServer(
|
||||
}
|
||||
});
|
||||
|
||||
/** 로그인 화면: 들어올 수 있는 서버 이름 (봇과 같은 서버) */
|
||||
app.get('/auth/guild', async () => {
|
||||
const g = allowedGuilds()[0];
|
||||
return { name: g ? await discord.guildName(g) : null };
|
||||
});
|
||||
|
||||
app.post('/auth/logout', async (req, reply) => {
|
||||
const raw = req.cookies[SID];
|
||||
const un = raw ? req.unsignCookie(raw) : null;
|
||||
|
||||
@@ -52,6 +52,22 @@ export class DiscordApi {
|
||||
const m: any = await r.json();
|
||||
return { nick: m.nick ?? null, avatar: m.avatar ?? null };
|
||||
}
|
||||
|
||||
private guildNames = new Map<string, { name: string | null; at: number }>();
|
||||
/** 서버 이름 (로그인 화면 안내용, 10분 캐시). 모르면 null */
|
||||
async guildName(guildId: string): Promise<string | null> {
|
||||
const c = this.guildNames.get(guildId);
|
||||
if (c && Date.now() - c.at < 600_000) return c.name;
|
||||
let name: string | null = null;
|
||||
try {
|
||||
const r = await fetch(`${this.cfg.discordApi}/guilds/${guildId}`, { headers: { authorization: `Bot ${this.cfg.botToken}` } });
|
||||
if (r.ok) name = ((await r.json()) as { name?: string }).name ?? null;
|
||||
} catch {
|
||||
/* 디스코드 장애면 이름 없이 안내 */
|
||||
}
|
||||
this.guildNames.set(guildId, { name, at: Date.now() });
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
/** 봇 토큰으로 디스코드 계정 조회 (유저 등록 때 이름 확인). 없는 아이디면 null */
|
||||
|
||||
Reference in New Issue
Block a user