Skip to content

Commit

Permalink
docker-composeでe2e環境構築
Browse files Browse the repository at this point in the history
  • Loading branch information
eatski committed Aug 15, 2024
1 parent 804fd16 commit 0c1aaa8
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 43 deletions.
19 changes: 19 additions & 0 deletions Dockerfile.e2e
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ベースイメージを指定(例:Node.jsの公式イメージの最新LTS版)
FROM node:20

# 作業ディレクトリを作成
WORKDIR /app

# アプリケーションのソースコードをコピー
COPY . .

RUN git clean -xdf

# 依存関係をインストール
RUN npm install

# アプリケーションを起動
CMD ["npm", "run", "dev"]

# ポート番号を指定(例:3000)
EXPOSE 3000
29 changes: 29 additions & 0 deletions docker-compose.e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
services:
db:
image: postgres:16-alpine
restart: always
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
nextjs:
build:
context: .
dockerfile: Dockerfile.e2e
ports:
- 3000:3000
depends_on:
- db
environment:
SUPABASE_POSTGRES_URL_NON_POOLING: postgres://postgres:postgres@db:5432/postgres
SUPABASE_POSTGRES_PRISMA_URL: postgres://postgres:postgres@db:5432/postgres?pgbouncer=true
MOCK_LOGIN: 1
RANKING_WEIGHT: '{"questionLogsLength": -0.1,"correctSolutionsLength": 0.3,"evaluationTotal": 0.9,"questionExamplesLength": 0.4,"random": 2,"timeFromPublished": -0.2}'
NEXTAUTH_SECRET: 'secret'
OPENAI_API_KEY: ${OPENAI_API_KEY}
ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
EDGE_CONFIG: ${EDGE_CONFIG}


128 changes: 85 additions & 43 deletions src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,94 @@ import { generateId } from "@/common/util/id";
import { neverReach } from "@/common/util/never";
import { prisma } from "@/libs/prisma";
import NextAuth, { type NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import GoogleProvider from "next-auth/providers/google";

if (
!process.env.GOOGLE_ID ||
!process.env.GOOGLE_SECRET ||
!process.env.NEXTAUTH_SECRET
) {
throw new Error("Google OAuth is not configured");
}

export const authConfig: NextAuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async jwt({ token, account }) {
if (account?.providerAccountId && !token?.userId) {
// 初回ログイン時のみuserが存在します
const user = await prisma.user.upsert({
where: { oauthId: account.providerAccountId },
update: {},
create: {
id: generateId(),
oauthId: account.providerAccountId,
export const authConfig: NextAuthOptions =
process.env.GOOGLE_ID && process.env.GOOGLE_SECRET
? {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async jwt({ token, account }) {
if (account?.providerAccountId && !token?.userId) {
// 初回ログイン時のみuserが存在します
const user = await prisma.user.upsert({
where: { oauthId: account.providerAccountId },
update: {},
create: {
id: generateId(),
oauthId: account.providerAccountId,
},
});
const userId: string = user.id;
token.userId = userId; // userオブジェクトに保存されているカスタム値をトークンに追加します
}
return token;
},
async session({ session, token }) {
// トークンからセッションにカスタム値を追加します
session.custom = {
userId:
typeof token.userId === "string"
? token.userId
: neverReach("token.userId is not string"),
};
return session;
},
});
const userId: string = user.id;
token.userId = userId; // userオブジェクトに保存されているカスタム値をトークンに追加します
},
}
return token;
},
async session({ session, token }) {
// トークンからセッションにカスタム値を追加します
session.custom = {
userId:
typeof token.userId === "string"
? token.userId
: neverReach("token.userId is not string"),
};
return session;
},
},
};
: process.env.MOCK_LOGIN
? {
providers: [
CredentialsProvider({
credentials: {
id: { label: "id", type: "text", placeholder: "User Id" },
},
authorize: function (credentials) {
return credentials?.id
? {
id: credentials.id,
email: "[email protected]",
}
: null;
},
}),
],
secret: "secret",
callbacks: {
async jwt({ user, token }) {
if (user) {
// 初回ログイン時のみuserが存在します
const createdUser = await prisma.user.upsert({
where: { oauthId: user.id },
update: {},
create: {
id: user.id,
},
});
const userId: string = createdUser.id;
token.userId = userId; // userオブジ
}
return token;
},
async session({ session, token }) {
// トークンからセッションにカスタム値を追加します
session.custom = {
userId:
typeof token.userId === "string"
? token.userId
: neverReach("token.userId is not string"),
};
return session;
},
},
}
: neverReach("Google OAuth is not configured");

export default NextAuth(authConfig);

0 comments on commit 0c1aaa8

Please sign in to comment.