- Added Kakao OAuth client implementation for handling authentication flows. - Updated user repository to support OAuth-based user creation and retrieval. - Introduced Redis-based session management for OAuth sessions. - Enhanced in-memory user repository to handle OAuth user data. - Updated environment configuration files to include new OAuth settings. - Modified API routes to support Kakao login and registration flows. - Added Prisma schema updates for user model to accommodate OAuth fields. - Implemented tests for the new authentication flow and user repository methods.
94 lines
3.0 KiB
TypeScript
94 lines
3.0 KiB
TypeScript
import fastify from 'fastify';
|
|
import cors from '@fastify/cors';
|
|
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
|
|
import {
|
|
createPostgresConnector,
|
|
createRedisConnector,
|
|
resolvePostgresConfigFromEnv,
|
|
resolveRedisConfigFromEnv,
|
|
} from '@sammo-ts/infra';
|
|
|
|
import { resolveGatewayApiConfigFromEnv } from './config.js';
|
|
import { createGatewayApiContext } from './context.js';
|
|
import { RedisGatewayFlushPublisher } from './auth/flushPublisher.js';
|
|
import { KakaoOAuthClient } from './auth/kakaoClient.js';
|
|
import { RedisOAuthSessionStore } from './auth/oauthSessionStore.js';
|
|
import { createPostgresUserRepository } from './auth/postgresUserRepository.js';
|
|
import { RedisGatewaySessionService } from './auth/redisSessionService.js';
|
|
import { appRouter } from './router.js';
|
|
|
|
export const createGatewayApiServer = async () => {
|
|
const config = resolveGatewayApiConfigFromEnv();
|
|
const postgres = createPostgresConnector(resolvePostgresConfigFromEnv());
|
|
const redis = createRedisConnector(resolveRedisConfigFromEnv());
|
|
await postgres.connect();
|
|
await redis.connect();
|
|
|
|
const users = createPostgresUserRepository(postgres.prisma);
|
|
const sessions = new RedisGatewaySessionService(redis.client, {
|
|
keyPrefix: config.redisKeyPrefix,
|
|
sessionTtlSeconds: config.sessionTtlSeconds,
|
|
gameSessionTtlSeconds: config.gameSessionTtlSeconds,
|
|
});
|
|
const flushPublisher = new RedisGatewayFlushPublisher(redis.client, config.flushChannel);
|
|
const kakaoClient = new KakaoOAuthClient({
|
|
restKey: config.kakaoRestKey,
|
|
adminKey: config.kakaoAdminKey,
|
|
redirectUri: config.kakaoRedirectUri,
|
|
});
|
|
const oauthSessions = new RedisOAuthSessionStore(
|
|
redis.client,
|
|
config.redisKeyPrefix,
|
|
config.oauthSessionTtlSeconds
|
|
);
|
|
|
|
const app = fastify({
|
|
logger: true,
|
|
});
|
|
|
|
await app.register(cors, {
|
|
origin: true,
|
|
credentials: true,
|
|
});
|
|
|
|
await app.register(fastifyTRPCPlugin, {
|
|
prefix: config.trpcPath,
|
|
trpcOptions: {
|
|
router: appRouter,
|
|
createContext: () =>
|
|
createGatewayApiContext({
|
|
users,
|
|
sessions,
|
|
flushPublisher,
|
|
gameTokenSecret: config.gameTokenSecret,
|
|
gameSessionTtlSeconds: config.gameSessionTtlSeconds,
|
|
kakaoClient,
|
|
oauthSessions,
|
|
publicBaseUrl: config.publicBaseUrl,
|
|
}),
|
|
},
|
|
});
|
|
|
|
app.get('/healthz', async () => ({
|
|
ok: true,
|
|
}));
|
|
|
|
app.addHook('onClose', async () => {
|
|
await redis.disconnect();
|
|
await postgres.disconnect();
|
|
});
|
|
|
|
return {
|
|
app,
|
|
config,
|
|
};
|
|
};
|
|
|
|
export const runGatewayApiServer = async (): Promise<void> => {
|
|
const { app, config } = await createGatewayApiServer();
|
|
await app.listen({
|
|
host: config.host,
|
|
port: config.port,
|
|
});
|
|
};
|