Skip to content

Commit ae02a04

Browse files
committed
feat: アカウント取得機能を追加し、許可されたメールアドレスの取得方法を更新
1 parent de9b0f9 commit ae02a04

5 files changed

Lines changed: 59 additions & 23 deletions

File tree

frontend/auth.config.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import type { NextAuthConfig } from "next-auth";
22
import Google from "next-auth/providers/google";
3+
import { getAllAccounts } from "./src/util/cosmos/account";
34

4-
const allowedEmails = process.env.ALLOWED_EMAILS?.split(",") || []; // 環境変数から許可されるメールアドレスを取得
5+
let allowedEmails: string[] = [];
6+
7+
async function fetchAllowedEmails() {
8+
try {
9+
allowedEmails = await getAllAccounts()
10+
.then((accounts) => accounts.map((account) => account.email))
11+
console.log("🚀Allowed emails updated:", allowedEmails);
12+
} catch (error) {
13+
console.error("Error fetching allowed emails:", error);
14+
}
15+
}
16+
17+
// 初期化時に許可されたメールアドレスを取得
18+
fetchAllowedEmails();
519

620
export const authConfig: NextAuthConfig = {
721
providers: [Google],
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { NextResponse } from "next/server";
2+
import { getAllAccounts } from "../../../../util/cosmos/account";
23

34
export async function GET() {
4-
const allowedEmails = process.env.ALLOWED_EMAILS?.split(",") || [];
5-
return NextResponse.json(allowedEmails);
6-
}
5+
try {
6+
const accounts = await getAllAccounts();
7+
const allowedEmails = accounts.map(account => account.email);
8+
console.log("🚀Allowed emails retrieved successfully:", allowedEmails);
9+
return NextResponse.json(allowedEmails);
10+
} catch (error) {
11+
console.error("Error retrieving allowed emails:", error);
12+
return NextResponse.json({ error: "Failed to retrieve allowed emails" }, { status: 500 });
13+
}
14+
}

frontend/src/app/layout.tsx

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Inter } from "next/font/google";
44
import "./globals.css";
55
import { SessionProvider, useSession, signIn } from "next-auth/react";
6-
import { useEffect, useState } from "react";
6+
import { useEffect } from "react";
77

88
const inter = Inter({ subsets: ["latin"] });
99

@@ -35,28 +35,14 @@ export default function RootLayout({
3535

3636
function AuthGuard({ children }: { children: React.ReactNode }) {
3737
const { data: session, status } = useSession();
38-
const [allowedEmails, setAllowedEmails] = useState<string[]>([]);
3938

40-
useEffect(() => {
41-
const fetchAllowedEmails = async () => {
42-
const response = await fetch("/api/auth/allowed-emails");
43-
const emails = await response.json();
44-
setAllowedEmails(emails);
45-
};
46-
fetchAllowedEmails();
47-
}, []);
48-
49-
if (status === "loading" || allowedEmails.length === 0) {
50-
return <div>読み込み中...</div>; // ローディング状態
39+
if (status === "loading") {
40+
return <div>読み込み中...</div>;
5141
}
5242

5343
if (!session?.user) {
54-
return <div>ログインが必要です。</div>; // 未ログインの場合
55-
}
56-
57-
if (!allowedEmails.includes(session.user.email || "")) {
58-
return <div>認可されていないアカウントです。</div>; // 許可されていない場合
44+
return <div>ログインが必要です。</div>;
5945
}
6046

61-
return <>{children}</>; // 許可された場合のみ子要素を表示
47+
return <>{children}</>;
6248
}

frontend/src/models/models.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ export interface CategoryItem {
3232
id: string;
3333
category: string; // PartitionKey
3434
order: number;
35+
}
36+
37+
// アカウントアイテム
38+
export interface AccountItem {
39+
id: string;
40+
email: string; // メールアドレス
3541
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { CosmosClient } from "@azure/cosmos";
2+
import { AccountItem } from "../../models/models";
3+
4+
/**
5+
* categoryを全取得する
6+
* @returns - カテゴリの配列
7+
* */
8+
export const getAllAccounts = async (): Promise<AccountItem[]> => {
9+
return new Promise(async (resolve, reject) => {
10+
const cosmosClient = new CosmosClient(process.env.COSMOS_CONNECTION_STRING!);
11+
const database = cosmosClient.database(process.env.COSMOS_DATABASE_NAME!);
12+
const container = database.container(process.env.COSMOS_ACCOUNT_CONTAINER_NAME!);
13+
14+
try {
15+
const { resources } = await container.items.readAll<AccountItem>().fetchAll();
16+
resolve(resources);
17+
} catch (error) {
18+
console.error("🚀Error retrieving accounts:", error);
19+
reject(error);
20+
}
21+
});
22+
};

0 commit comments

Comments
 (0)