-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FirebaseUIを使ってログイン周りを作成 #1
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
749fe94
firebase uiをインストール
tsubasa1122 159f3f8
認証状態をチェックするhooks作成
tsubasa1122 5bd374d
install react-firebaseui
tsubasa1122 f76bdce
reactのバージョンを下げた
tsubasa1122 fd54605
サインインコンポーネント作成
tsubasa1122 c427a2a
サインインをコンポーネントに切り出した
tsubasa1122 e4d50e2
authStateにトークンを追加
tsubasa1122 f48f801
ログイン情報をPropsで受け取るようにした
tsubasa1122 c050dae
バックエンドにトークンが有効か確認する処理を追加
tsubasa1122 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import NextLink from "next/link"; | ||
import { Box, Spinner, VStack } from "@chakra-ui/react"; | ||
import { useAuthState } from "../hooks/useAuthState"; | ||
import { getAuth, signOut } from "firebase/auth"; | ||
|
||
const SignInOrSignOutButton: React.FC = () => { | ||
const { isSignedIn, isLoading } = useAuthState(); | ||
|
||
if (isLoading) return <Spinner />; | ||
|
||
return ( | ||
<VStack> | ||
{isSignedIn ? ( | ||
<button onClick={() => signOut(getAuth())}> | ||
ログアウト(※ ログインはないよ) | ||
</button> | ||
) : ( | ||
<NextLink href={"/signup"}> | ||
<Box | ||
as="span" | ||
textAlign="center" | ||
marginTop={4} | ||
cursor="pointer" | ||
backgroundColor="teal.500" | ||
color="white" | ||
fontSize={16} | ||
paddingX={6} | ||
paddingY={2} | ||
borderRadius={10} | ||
> | ||
サインイン | ||
</Box> | ||
</NextLink> | ||
)} | ||
</VStack> | ||
); | ||
}; | ||
|
||
export default SignInOrSignOutButton; |
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,27 @@ | ||
import { EmailAuthProvider, getAuth } from "firebase/auth"; | ||
import { useEffect, useState } from "react"; | ||
import StyledFirebaseAuth from "react-firebaseui/StyledFirebaseAuth"; | ||
|
||
const uiConfig = { | ||
signInFlow: "popup", | ||
signInOptions: [EmailAuthProvider.PROVIDER_ID], | ||
signInSuccessUrl: "/", | ||
}; | ||
|
||
export const SignInForm: React.FC = () => { | ||
const [renderAuth, setRenderAuth] = useState(false); | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined") { | ||
setRenderAuth(true); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
{renderAuth ? ( | ||
<StyledFirebaseAuth uiConfig={uiConfig} firebaseAuth={getAuth()} /> | ||
) : null} | ||
</div> | ||
); | ||
}; |
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,51 @@ | ||
import { useEffect, useState } from "react"; | ||
import { getAuth, onAuthStateChanged } from "firebase/auth"; | ||
|
||
/** | ||
* useAuthState フックの戻り値の型。 | ||
*/ | ||
export type AuthState = { | ||
isSignedIn: boolean; | ||
isLoading: boolean; | ||
userId: string | undefined; | ||
userName: string | undefined; | ||
}; | ||
|
||
/** | ||
* useAuthState が返す初期値。 | ||
* Next.js のサーバーサイドレンダリング時もこの値になる。 | ||
*/ | ||
const INITIAL_AUTH_STATE: AuthState = { | ||
isSignedIn: false, | ||
isLoading: true, | ||
userId: undefined, | ||
userName: undefined, | ||
}; | ||
|
||
/** | ||
* ユーザーのサインイン状態を取得するためのカスタムフック。 | ||
*/ | ||
export const useAuthState = (): AuthState => { | ||
const [authState, setAuthState] = useState(INITIAL_AUTH_STATE); | ||
|
||
// サインイン状態の変化を監視する | ||
useEffect(() => { | ||
const unsubscribe = onAuthStateChanged(getAuth(), (user) => { | ||
if (user) { | ||
setAuthState({ | ||
isSignedIn: true, | ||
isLoading: false, | ||
userId: user.uid, | ||
userName: user.displayName || undefined, | ||
}); | ||
} else { | ||
setAuthState({ ...INITIAL_AUTH_STATE, isLoading: false }); | ||
} | ||
}); | ||
|
||
// ページ遷移時にサインイン状態の監視を解除 | ||
return () => unsubscribe(); | ||
}, []); | ||
|
||
return authState; | ||
}; |
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
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
import type { NextPage } from "next"; | ||
import { SignInForm } from "../components/SignInform"; | ||
|
||
const Signup: NextPage = () => { | ||
return ( | ||
<div> | ||
<SignInForm /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Signup; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 react18にするとfirebaseuiでエラーになるため、バージョンを下げることにした
ref: firebase/firebaseui-web-react#172