-
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.
- Loading branch information
Showing
8 changed files
with
90 additions
and
10 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
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 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,79 @@ | ||
import { orderConverter } from "common/firebase-utils/converter"; | ||
import { auth } from "common/firebase-utils/firebase"; | ||
import { documentSub } from "common/firebase-utils/subscription"; | ||
import { | ||
GoogleAuthProvider, | ||
type User, | ||
onAuthStateChanged, | ||
signInWithPopup, | ||
signOut, | ||
} from "firebase/auth"; | ||
import { useEffect, useState } from "react"; | ||
import useSWRSubscription from "swr/subscription"; | ||
import { Button } from "~/components/ui/button"; | ||
|
||
const provider = new GoogleAuthProvider(); | ||
|
||
export default function Auth() { | ||
const [user, setUser] = useState<User | null>(null); | ||
console.log(user); | ||
|
||
useEffect(() => { | ||
onAuthStateChanged(auth, (user) => { | ||
if (user?.emailVerified) { | ||
setUser(user); | ||
} else { | ||
setUser(null); | ||
} | ||
}); | ||
}, []); | ||
|
||
const login = () => { | ||
signInWithPopup(auth, provider) | ||
.then((result) => { | ||
const credential = GoogleAuthProvider.credentialFromResult(result); | ||
if (credential == null) { | ||
console.log("credential is null"); | ||
return; | ||
} | ||
const token = credential.accessToken; | ||
const user = result.user; | ||
console.log("user", user); | ||
}) | ||
.catch((err) => { | ||
const errorCode = err.code; | ||
const errorMessage = err.message; | ||
const email = err.customData.email; | ||
const credential = GoogleAuthProvider.credentialFromError(err); | ||
console.log("errorCode", errorCode); | ||
console.log("errorMessage", errorMessage); | ||
console.log("email", email); | ||
console.log("credential", credential); | ||
}); | ||
}; | ||
|
||
const logout = async () => { | ||
await signOut(auth); | ||
}; | ||
|
||
const order = useSWRSubscription( | ||
["orders", "nTMWm6GuT6ZuSGxOu2iP"], | ||
documentSub({ converter: orderConverter }), | ||
); | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<Button onClick={login}>ログイン</Button> | ||
<Button onClick={logout}>ログアウト</Button> | ||
</div> | ||
<div> | ||
<h3>ログイン情報</h3> | ||
<pre>{user?.displayName}</pre> | ||
<pre>{user?.email}</pre> | ||
<img src={user?.photoURL ?? ""} alt={user?.displayName ?? ""} /> | ||
</div> | ||
<div>{JSON.stringify(order.data?.toOrder(), null, 2)}</div> | ||
</div> | ||
); | ||
} |