|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +"use client"; |
| 18 | + |
| 19 | +import { multiFactor, sendEmailVerification, signOut } from "firebase/auth"; |
| 20 | +import { useRouter } from "next/navigation"; |
| 21 | +import { useUser } from "@/lib/firebase/hooks"; |
| 22 | +import { auth } from "@/lib/firebase/clientApp"; |
| 23 | +import { type User } from "firebase/auth"; |
| 24 | + |
| 25 | +export function AuthenticatedApp({ initialUser }: { initialUser: User | null }) { |
| 26 | + const user = useUser(initialUser); |
| 27 | + const router = useRouter(); |
| 28 | + |
| 29 | + if (!user) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + const mfa = multiFactor(user); |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className="max-w-sm mx-auto pt-36 space-y-6 pb-36"> |
| 37 | + <div className="border border-neutral-200 dark:border-neutral-800 rounded-md p-4 space-y-4"> |
| 38 | + <h1 className="text-md font-medium">Welcome, {user.displayName || user.email || user.phoneNumber}</h1> |
| 39 | + {user.email ? ( |
| 40 | + <> |
| 41 | + {user.emailVerified ? ( |
| 42 | + <div className="text-green-500">Email verified</div> |
| 43 | + ) : ( |
| 44 | + <button |
| 45 | + className="bg-red-500 text-white px-3 py-1.5 rounded text-sm" |
| 46 | + onClick={async () => { |
| 47 | + try { |
| 48 | + await sendEmailVerification(user); |
| 49 | + alert("Email verification sent, please check your email"); |
| 50 | + } catch (error) { |
| 51 | + console.error(error); |
| 52 | + alert("Error sending email verification, check console"); |
| 53 | + } |
| 54 | + }} |
| 55 | + > |
| 56 | + Verify Email → |
| 57 | + </button> |
| 58 | + )} |
| 59 | + </> |
| 60 | + ) : null} |
| 61 | + |
| 62 | + <hr className="opacity-30" /> |
| 63 | + <h2 className="text-sm font-medium">Multi-factor Authentication</h2> |
| 64 | + {mfa.enrolledFactors.map((factor) => { |
| 65 | + return ( |
| 66 | + <div key={factor.factorId}> |
| 67 | + {factor.factorId} - {factor.displayName} |
| 68 | + </div> |
| 69 | + ); |
| 70 | + })} |
| 71 | + <button |
| 72 | + className="bg-blue-500 text-white px-3 py-1.5 rounded text-sm" |
| 73 | + onClick={() => { |
| 74 | + router.push("/screens/mfa-enrollment-screen"); |
| 75 | + }} |
| 76 | + > |
| 77 | + Add MFA Factor → |
| 78 | + </button> |
| 79 | + <hr className="opacity-30" /> |
| 80 | + <button |
| 81 | + className="bg-blue-500 text-white px-3 py-1.5 rounded text-sm" |
| 82 | + onClick={async () => await signOut(auth)} |
| 83 | + > |
| 84 | + Sign Out → |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
0 commit comments