From d7085749ec798a9b377528be2526029f711c3371 Mon Sep 17 00:00:00 2001 From: hellotksan Date: Mon, 25 Nov 2024 15:38:25 +0900 Subject: [PATCH] =?UTF-8?q?useAuth=E9=96=A2=E4=BF=82=E3=80=81=E3=81=9D?= =?UTF-8?q?=E3=81=AE=E3=81=9F=E3=82=B9=E3=82=BF=E3=82=A4=E3=83=AA=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/app/followers/page.tsx | 2 +- src/app/followings/page.tsx | 2 +- src/app/post/edit/page.tsx | 2 +- src/app/profile/page.tsx | 2 +- src/app/setting/page.tsx | 2 +- src/components/layouts/login/LoginForm.tsx | 28 ++++++++------- .../layouts/register/RegisterForm.tsx | 13 ++++--- .../layouts/timeline/PostButton.tsx | 2 +- src/hooks/useAuth.ts | 5 +++ src/tsconfig.json | 36 ------------------- 11 files changed, 36 insertions(+), 60 deletions(-) delete mode 100644 src/tsconfig.json diff --git a/package.json b/package.json index 82dc006..437611f 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ }, "lint-staged": { "src/**/*.{js,jsx,ts,tsx,md}": [ - "" + "eslint --fix" ] }, "ignorePatterns": [ diff --git a/src/app/followers/page.tsx b/src/app/followers/page.tsx index 766c428..ac5758c 100644 --- a/src/app/followers/page.tsx +++ b/src/app/followers/page.tsx @@ -6,7 +6,7 @@ const FollowersPage: React.FC = () => { return (
-
+
diff --git a/src/app/followings/page.tsx b/src/app/followings/page.tsx index e525110..ce56420 100644 --- a/src/app/followings/page.tsx +++ b/src/app/followings/page.tsx @@ -6,7 +6,7 @@ const FollowingUsersPage: React.FC = () => { return (
-
+
diff --git a/src/app/post/edit/page.tsx b/src/app/post/edit/page.tsx index 530e361..fb2af49 100644 --- a/src/app/post/edit/page.tsx +++ b/src/app/post/edit/page.tsx @@ -34,7 +34,7 @@ const PostEditPage: React.FC = () => { return (
-
+
}> diff --git a/src/app/profile/page.tsx b/src/app/profile/page.tsx index d3101a2..8bd9236 100644 --- a/src/app/profile/page.tsx +++ b/src/app/profile/page.tsx @@ -30,7 +30,7 @@ const ProfilePage: React.FC = () => { return (
-
+
}> diff --git a/src/app/setting/page.tsx b/src/app/setting/page.tsx index bb85c02..50125cd 100644 --- a/src/app/setting/page.tsx +++ b/src/app/setting/page.tsx @@ -6,7 +6,7 @@ const SettingPage: React.FC = () => { return (
-
+
diff --git a/src/components/layouts/login/LoginForm.tsx b/src/components/layouts/login/LoginForm.tsx index 435b957..7a183cd 100644 --- a/src/components/layouts/login/LoginForm.tsx +++ b/src/components/layouts/login/LoginForm.tsx @@ -1,22 +1,24 @@ "use client"; import React, { useEffect, useRef } from "react"; -import { useRouter } from "next/navigation"; +// import { useRouter } from "next/navigation"; import { loginCall } from "@/app/actionCalls"; -import { - loginStart, - loginSuccess, - loginError, -} from "@/features/auth/authSlice"; +// import { +// loginStart, +// loginSuccess, +// loginError, +// } from "@/features/auth/authSlice"; import { useAppDispatch } from "@/hooks/useDispatch"; import { User } from "@/types/user"; +import { useAuth } from "@/hooks/useAuth"; const LoginForm: React.FC = () => { const emailRef = useRef(null); const passwordRef = useRef(null); const dispatch = useAppDispatch(); - const router = useRouter(); + const { login } = useAuth(); + // const router = useRouter(); useEffect(() => { // 自動でメールフィールドにフォーカスを当てる @@ -25,22 +27,24 @@ const LoginForm: React.FC = () => { const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); + const emailValue = emailRef.current?.value || ""; const passwordValue = passwordRef.current?.value || ""; onSubmit(emailValue, passwordValue); }; const onSubmit = async (email: string, password: string): Promise => { - dispatch(loginStart()); + // dispatch(loginStart()); try { const userData: User = await loginCall({ email, password, dispatch }); - dispatch(loginSuccess(userData)); - router.push("/home"); + login(userData); + // dispatch(loginSuccess(userData)); + // router.push("/home"); } catch (err) { - dispatch(loginError("ログインに失敗しました")); + // dispatch(loginError("ログインに失敗しました")); alert("エラーが発生しました。"); - router.refresh(); + // router.refresh(); } }; diff --git a/src/components/layouts/register/RegisterForm.tsx b/src/components/layouts/register/RegisterForm.tsx index 54d4f10..86bd462 100644 --- a/src/components/layouts/register/RegisterForm.tsx +++ b/src/components/layouts/register/RegisterForm.tsx @@ -1,18 +1,21 @@ "use client"; import React, { useState } from "react"; -import { useRouter } from "next/navigation"; import axios from "axios"; import { AUTH_REGISTER_ENDPOINT } from "@/constants/api"; import RegisterButton from "./RegisterButton"; +import { User } from "@/types/user"; +import { loginCall } from "@/app/actionCalls"; +import { useAppDispatch } from "@/hooks/useDispatch"; +import { useAuth } from "@/hooks/useAuth"; const RegisterForm: React.FC = () => { const [username, setUsername] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [passwordConfirmation, setPasswordConfirmation] = useState(""); - - const router = useRouter(); + const dispatch = useAppDispatch(); + const { login } = useAuth(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); @@ -25,10 +28,10 @@ const RegisterForm: React.FC = () => { try { await axios.post(AUTH_REGISTER_ENDPOINT, { username, email, password }); alert("ユーザーを登録しました。"); - router.push("/login"); + const userData: User = await loginCall({ email, password, dispatch }); + login(userData); } catch (err) { alert("ユーザー登録に失敗しました。もう一度お試しください。"); - router.refresh(); } }; diff --git a/src/components/layouts/timeline/PostButton.tsx b/src/components/layouts/timeline/PostButton.tsx index a120df7..2101ca6 100644 --- a/src/components/layouts/timeline/PostButton.tsx +++ b/src/components/layouts/timeline/PostButton.tsx @@ -12,7 +12,7 @@ function PostButton() { type="submit" disabled={pending} > - {pending ? "投稿" : "投稿中..."} + {pending ? "投稿中..." : "投稿"}
); diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index 351445f..19a774a 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -7,6 +7,7 @@ import { logout, } from "@/features/auth/authSlice"; import { User } from "@/types/user"; +import { useRouter } from "next/navigation"; interface UseAuthReturn { user: User | null; @@ -19,18 +20,22 @@ interface UseAuthReturn { export const useAuth = (): UseAuthReturn => { const dispatch = useAppDispatch(); const { user, isLoading, error } = useAppSelector((state) => state.auth); + const router = useRouter(); const login = async (userData: User) => { dispatch(loginStart()); try { dispatch(loginSuccess(userData)); + router.push("/home"); } catch (err) { dispatch(loginError("ログインに失敗しました")); + router.refresh(); } }; const logoutUser = () => { dispatch(logout()); + router.push("/") }; return { user, isLoading, error, login, logoutUser }; diff --git a/src/tsconfig.json b/src/tsconfig.json deleted file mode 100644 index 8b6c62c..0000000 --- a/src/tsconfig.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2017", - "lib": [ - "dom", - "dom.iterable", - "esnext" - ], - "allowJs": true, - "skipLibCheck": true, - "strict": false, - "forceConsistentCasingInFileNames": true, - "noEmit": true, - "incremental": true, - "module": "esnext", - "esModuleInterop": true, - "moduleResolution": "node", - "resolveJsonModule": true, - "isolatedModules": true, - "jsx": "preserve", - "plugins": [ - { - "name": "next" - } - ] - }, - "include": [ - "next-env.d.ts", - ".next/types/**/*.ts", - "**/*.ts", - "**/*.tsx" - ], - "exclude": [ - "node_modules" - ] -}