Skip to content

Commit

Permalink
認証されたユーザーを取得するクラス名を変更した。ユーザーをデータベースに登録するためのクラスを準備した。
Browse files Browse the repository at this point in the history
  • Loading branch information
eigoninaritai-naokichi committed Jan 16, 2024
1 parent 0229d4e commit ed57403
Show file tree
Hide file tree
Showing 31 changed files with 299 additions and 134 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ FF14SNSはネタバレを恐れずにFF14の感想を投稿できるSNSです。

## ローカル開発環境
### サーバー証明書
`openssl req -nodes -new -x509 -keyout server.key -out server.cert`を実行し、サーバー証明書を作成してください。
`openssl req -nodes -new -x509 -keyout server.key -out server.cert -days 365`を実行し、サーバー証明書を作成してください。

# Welcome to Remix!

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import IUserRegistrar from "../../libraries/user/i-user-registrar";

/**
* FF14SNSのユーザー登録を行うアクション
* SNSのユーザー登録を行うアクション
*/
export default class FF14SnsUserRegistrationAction {
export default class SnsUserRegistrationAction {
/**
* FF14SNSのユーザー登録を行うアクションを生成する
* SNSのユーザー登録を行うアクションを生成する
* @param userRegistrar ユーザー登録を行うクラス。
*/
constructor(
Expand All @@ -15,10 +15,11 @@ export default class FF14SnsUserRegistrationAction {

/**
* ユーザーを登録する。
* @param userName ユーザー名。
* @returns 登録に成功したかどうか。
*/
public async register(): Promise<boolean> {
const response = await this.userRegistrar.register();
public async register(userName: string): Promise<boolean> {
const response = await this.userRegistrar.register(userName);
return response;
}

Expand Down
4 changes: 2 additions & 2 deletions app/contexts/user/sns-user-context.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext } from "react";
import FF14SnsUser from "../../models/user/ff14-sns-user";
import SnsUser from "../../models/user/sns-user";

/**
* SNSのユーザーコンテキスト。
*/
export const SnsUserContext = createContext<FF14SnsUser | null>(null);
export const SnsUserContext = createContext<SnsUser | null>(null);
6 changes: 3 additions & 3 deletions app/contexts/user/sns-user-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { ReactNode } from "react"
import FF14SnsUser from "../../models/user/ff14-sns-user";
import SnsUser from "../../models/user/sns-user";
import { SnsUserContext } from "./sns-user-context";

/**
* SNSのユーザープロバイダー。
* @param children 子要素。
* @param snsUser SNSのユーザー
* @param snsUser SNSユーザー情報
* @returns SNSのユーザープロバイダー。
*/
const SnsUserProvider = ({
children,
snsUser,
} : {
children: ReactNode,
snsUser: FF14SnsUser,
snsUser: SnsUser,
}) => {
return (
<SnsUserContext.Provider value={snsUser}>
Expand Down
13 changes: 10 additions & 3 deletions app/dependency-injector/get-load-context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type * as express from "express";
import { AppLoadContext } from "@netlify/remix-runtime";
import AuthenticatedUserProvider from "../libraries/user/authenticated-user-provider";
import FF14SnsUserLoader from "../loaders/user/ff14-sns-user-loader";
import AuthenticatedUserLoader from "../loaders/user/authenticated-user-loader";
import UserAccountManager from "../libraries/authentication/user-account-manager";
import UserAuthenticationAction from "../actions/authentication/user-authentication-action";
import IUserAuthenticator from "../libraries/authentication/i-user-authenticator";
import UserRegistrationAction from "../actions/authentication/user-registration-action";
import SnsUserRegistrationAction from "../actions/user/sns-user-registration-action";
import UserRegistrar from "../libraries/user/user-registrar";
import PostgresUserRepository from "../repositories/user/postgres-user-repository";
import IAuthenticationUserRegistrar from "../libraries/authentication/i-authentication-user-registrar";
import LatestPostsLoader from "../loaders/post/latest-posts-loader";
import FirebaseClient from "../libraries/authentication/firebase-client";
Expand All @@ -15,22 +18,26 @@ const authenticationClient = new FirebaseClient();
const userAccountManager = new UserAccountManager(authenticationClient);
const authenticationUserRegistrar: IAuthenticationUserRegistrar = userAccountManager;
const userRegistrationAction = new UserRegistrationAction(authenticationUserRegistrar);
const userRepository = new PostgresUserRepository();
const userRegistrar = new UserRegistrar(userRepository);
const snsUserRegistrationAction = new SnsUserRegistrationAction(userRegistrar);

// ユーザー認証を行うためのクラスを生成する。
const userAuthenticator: IUserAuthenticator = userAccountManager;
const userAuthenticationAction = new UserAuthenticationAction(userAuthenticator);

// ユーザー情報を取得するためのクラスを生成する。
const authenticatedUserProvider = new AuthenticatedUserProvider(authenticationClient);
const ff14SnsUserLoader = new FF14SnsUserLoader(authenticatedUserProvider);
const authenticatedUserLoader = new AuthenticatedUserLoader(authenticatedUserProvider);

// 最新の投稿を取得するためのクラスを生成する。
const latestPostsLoader = new LatestPostsLoader();

export const appLoadContext: AppLoadContext = {
userRegistrationAction,
snsUserRegistrationAction,
userAuthenticationAction,
ff14SnsUserLoader,
authenticatedUserLoader,
latestPostsLoader,
};

Expand Down
10 changes: 6 additions & 4 deletions app/libraries/user/authenticated-user-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FF14SnsUser from "../../models/user/ff14-sns-user";
import AuthenticatedUser from "../../models/user/authenticated-user";
import IAuthenticatedUserProvider from "./i-authenticated-user-provider";
import IAuthenticationClient from "../authentication/i-authentication-client";

Expand All @@ -15,13 +15,15 @@ export default class AuthenticatedUserProvider implements IAuthenticatedUserProv
) {
}

public async getUser(token: string): Promise<FF14SnsUser> {
public async getUser(token: string): Promise<AuthenticatedUser> {
const response = await this.authenticationClient.getUserInformation(token);
const ff14SnsUser = {
const authenticatedUser = {
id: response.users[0].localId,
profileId: response.users[0].localId,
authenticationProviderId: response.users[0].localId,
userName: response.users[0].email,
createdAt: new Date(Number(response.users[0].createdAt)),
};
return ff14SnsUser;
return authenticatedUser;
}
}
4 changes: 2 additions & 2 deletions app/libraries/user/i-authenticated-user-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FF14SnsUser from "../../models/user/ff14-sns-user";
import AuthenticatedUser from "../../models/user/authenticated-user";

/**
* 認証済みユーザーを提供するインターフェース。
Expand All @@ -9,5 +9,5 @@ export default interface IAuthenticatedUserProvider {
* @param token トークン。
* @returns 認証済みユーザー。
*/
getUser(token: string): Promise<FF14SnsUser>;
getUser(token: string): Promise<AuthenticatedUser>;
}
3 changes: 2 additions & 1 deletion app/libraries/user/i-user-registrar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
export default interface IUserRegistrar {
/**
* ユーザーを登録する。
* @param userName ユーザー名。
*/
register(): Promise<boolean>;
register(userName: string): Promise<boolean>;

/**
* ユーザーを削除する。
Expand Down
24 changes: 24 additions & 0 deletions app/libraries/user/user-registrar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import IUserRegistrar from './i-user-registrar';
import IUserRepository from '../../repositories/user/i-user-repository';

/**
* ユーザーの登録を行うクラス。
*/
export default class UserRegistrar implements IUserRegistrar {
/**
* ユーザーの登録を行うクラスを生成する。
* @param userRepository ユーザーのリポジトリ。
*/
constructor(
private readonly userRepository: IUserRepository,
) {
}

register(userName: string): Promise<boolean> {
throw new Error('Method not implemented.');
}

delete(id: string): Promise<boolean> {
throw new Error('Method not implemented.');
}
}
26 changes: 26 additions & 0 deletions app/loaders/user/authenticated-user-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import AuthenticatedUser from "../../models/user/authenticated-user";
import IAuthenticatedUserProvider from "../../libraries/user/i-authenticated-user-provider";

/**
* 認証済みユーザーを取得するローダー。
*/
export default class AuthenticatedUserLoader {
/**
* 認証済みユーザーを取得するローダーを生成する。
* @param authenticatedUserProvider 認証済みユーザーを提供するクラス。
*/
constructor(
private readonly authenticatedUserProvider: IAuthenticatedUserProvider
) {
}

/**
* 認証済みユーザーを取得する。
* @param token トークン。
* @returns 認証済みユーザー。
*/
public async getUser(token: string): Promise<AuthenticatedUser> {
const authenticatedUser = await this.authenticatedUserProvider.getUser(token);
return authenticatedUser;
}
}
26 changes: 0 additions & 26 deletions app/loaders/user/ff14-sns-user-loader.ts

This file was deleted.

21 changes: 21 additions & 0 deletions app/models/user/authenticated-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Entity from '../common/entity';

/**
* 認証済みユーザー。
*/
export default interface AuthenticatedUser extends Entity {
/**
* プロフィールID。
*/
readonly profileId: string;

/**
* 認証プロバイダID。
*/
readonly authenticationProviderId: string;

/**
* ユーザー名。
*/
readonly userName: string;
}
11 changes: 0 additions & 11 deletions app/models/user/ff14-sns-user.ts

This file was deleted.

9 changes: 9 additions & 0 deletions app/models/user/sns-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* SNSのユーザー。
*/
export default interface SnsUser {
/**
* ユーザー名。
*/
readonly userName: string;
}
31 changes: 31 additions & 0 deletions app/repositories/user/postgres-user-repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import User from '../../models/user/user';
import IUserRepository from './i-user-repository';

/**
* Postgresのユーザーのリポジトリ。
*/
export default class PostgresUserRepository implements IUserRepository {
create(): Promise<boolean> {
throw new Error('Method not implemented.');
}

update(user: User): Promise<boolean> {
throw new Error('Method not implemented.');
}

delete(id: string): Promise<boolean> {
throw new Error('Method not implemented.');
}

findById(id: string): Promise<User | null> {
throw new Error('Method not implemented.');
}

findByProfileId(profileId: string): Promise<User | null> {
throw new Error('Method not implemented.');
}

findByAuthenticationProviderId(authenticationProviderId: string): Promise<User | null> {
throw new Error('Method not implemented.');
}
}
32 changes: 32 additions & 0 deletions app/routes/app.register-user/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ActionFunctionArgs, json, redirect } from "@netlify/remix-runtime";
import { Form } from "@remix-run/react";

export const action = async ({
request,
context,
}: ActionFunctionArgs) => {
// フォームデータを取得する。
const formData = await request.formData();
const userName = formData.get("userName") as string;

// ユーザー名がない場合、エラーを返す。
if (!userName) return json({ error: "ユーザー名が入力されていません。" });

// ユーザーを登録する。
context.snsUserRegistrationAction.register(userName);
return redirect("/app");
}

/**
* ユーザー登録ページ。
* @returns ユーザー登録ページ。
*/
export default function RegisterUser() {
return (
<Form method="post">
<label htmlFor="userName">FF14のユーザー名(user_name@world)</label>
<input type="text" name="userName" />
<button type="submit">登録</button>
</Form>
);
}
4 changes: 2 additions & 2 deletions app/routes/app/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Form } from "@remix-run/react";
import useSnsUser from "../../../contexts/user/use-sns-user";

export default function Header() {
const ff14SnsUser = useSnsUser();
const snsUser = useSnsUser();

return (
<header>
<h1>FF14 Header</h1>
<p>{ff14SnsUser.userName}</p>
<p>{snsUser.userName}</p>
<Form method="post">
<button type="submit">ログアウト</button>
</Form>
Expand Down
Loading

0 comments on commit ed57403

Please sign in to comment.