-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
認証情報をContextに保存して、useIsLoggedIn()などのHookを作成 (#144)
* [frontend] Implement AuthProvider and useAuthContext hooks * [frontend] Add client-auth-provider.tsx - AuthProvider as client component - useAuthContext() and useIsLoggedInContext() as client hooks - Add getCurrentUser() and isLoggedIn() to session.ts (Server-side)
- Loading branch information
Showing
11 changed files
with
115 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"use client"; | ||
|
||
import { AuthContext } from "@/app/lib/client-auth"; | ||
|
||
type User = { | ||
id: number; | ||
name: string; | ||
email: string; | ||
avatarURL?: string; | ||
createdAt: string; | ||
}; | ||
|
||
export type AuthProviderProps = { | ||
user?: User; | ||
children: React.ReactNode; | ||
isLoggedIn: boolean; | ||
}; | ||
|
||
export default function AuthProvider({ | ||
children, | ||
user, | ||
isLoggedIn, | ||
}: AuthProviderProps) { | ||
const auth = { currentUser: user, isLoggedIn }; | ||
return <AuthContext.Provider value={auth}>{children}</AuthContext.Provider>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,3 +249,10 @@ export async function deleteRoomUser(roomId: number, userId: number) { | |
return "Success"; | ||
} | ||
} | ||
|
||
export async function signInAsTestUser() { | ||
const email = "[email protected]"; | ||
const password = "password-test"; | ||
await signIn({ email, password }); | ||
redirect("/"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use client"; | ||
import { createContext, useContext } from "react"; | ||
|
||
type User = { | ||
id: number; | ||
name: string; | ||
email: string; | ||
avatarURL?: string; | ||
createdAt: string; | ||
}; | ||
|
||
type AuthContextType = { | ||
currentUser?: User; | ||
isLoggedIn: boolean; | ||
}; | ||
|
||
export const AuthContext = createContext<AuthContextType>({ | ||
isLoggedIn: false, | ||
}); | ||
|
||
export const useAuthContext = () => useContext(AuthContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { ModeToggle } from "@/components/toggle-mode"; | ||
import { Button } from "@/components/ui/button"; | ||
import Link from "next/link"; | ||
import { signIn, signOut } from "@/app/lib/actions"; | ||
import { signInAsTestUser, signOut } from "@/app/lib/actions"; | ||
import { isLoggedIn } from "@/app/lib/session"; | ||
import { redirect } from "next/navigation"; | ||
|
||
function AuthorizedMenu() { | ||
return ( | ||
|
@@ -20,14 +19,6 @@ function AuthorizedMenu() { | |
); | ||
} | ||
|
||
async function signInAsTestUser() { | ||
"use server"; | ||
const email = "[email protected]"; | ||
const password = "password-test"; | ||
await signIn({ email, password }); | ||
redirect("/"); | ||
} | ||
|
||
function UnauthorizedMenu() { | ||
return ( | ||
<li className="flex gap-8 items-center"> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
"use client"; | ||
import { Skeleton } from "@/components/ui/skeleton"; | ||
import { Separator } from "@/components/ui/separator"; | ||
import { Button } from "@/components/ui/button"; | ||
import { Input } from "@/components/ui/input"; | ||
import { Stack } from "@/app/ui/layout/stack"; | ||
import { Label } from "@/components/ui/label"; | ||
import { useAuthContext } from "@/app/lib/client-auth"; | ||
|
||
function AvatarSkeleton() { | ||
return <Skeleton className="rounded-full h-20 w-20" />; | ||
|
@@ -23,10 +25,11 @@ function ProfileItem({ type, title, value }: ProfileItemProps) { | |
export type ProfileItemProps = { | ||
type: string; | ||
title: string; | ||
value: string; | ||
value?: string; | ||
}; | ||
|
||
export default function ProfileForm() { | ||
const { currentUser } = useAuthContext(); | ||
// Menu: min 100px | ||
// Profile : the rest | ||
return ( | ||
|
@@ -37,8 +40,8 @@ export default function ProfileForm() { | |
<Separator /> | ||
</Stack> | ||
<AvatarSkeleton /> | ||
<ProfileItem type="text" title="name" value="susami" /> | ||
<ProfileItem type="email" title="email" value="[email protected]" /> | ||
<ProfileItem type="text" title="name" value={currentUser?.name} /> | ||
<ProfileItem type="email" title="email" value={currentUser?.email} /> | ||
<div></div> | ||
<Button variant="outline" className="w-40"> | ||
Save | ||
|