-
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.
Merge pull request #12 from MIERUNE/demo-email-password
demo: signinWithEmailAndPassword
- Loading branch information
Showing
10 changed files
with
13,942 additions
and
23 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -1,14 +1,42 @@ | ||
<script lang="ts"> | ||
import { FirebaseError } from 'firebase/app'; | ||
import { sendEmailVerification } from 'firebase/auth'; | ||
import { | ||
signInWithGoogle, | ||
signInWithTwitter, | ||
waitForRedirectResult | ||
waitForRedirectResult, | ||
signInWithEmailAndPassword, | ||
createUserWithEmailAndPassword | ||
} from '$lib/firebase-auth/client'; | ||
import { page } from '$app/stores'; | ||
const redirectResult = waitForRedirectResult(); | ||
let { data } = $props(); | ||
let email = $state(''); | ||
let password = $state(''); | ||
let errorCode = $state(''); | ||
async function signInWithPassword() { | ||
try { | ||
await signInWithEmailAndPassword(email, password); | ||
} catch (error) { | ||
if (error instanceof FirebaseError) { | ||
errorCode = error.code; | ||
} | ||
} | ||
} | ||
async function signUpWithPassword() { | ||
try { | ||
const cred = await createUserWithEmailAndPassword(email, password); | ||
await sendEmailVerification(cred.user, { url: $page.url.origin + '/verify_email' }); | ||
} catch (error) { | ||
if (error instanceof FirebaseError) { | ||
errorCode = error.code; | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<h1>Login</h1> | ||
|
@@ -20,11 +48,42 @@ | |
{#if $page.url.searchParams.get('next')} | ||
<p>You need to log in.</p> | ||
{/if} | ||
|
||
<button onclick={signInWithGoogle} disabled={data.currentIdToken !== undefined} | ||
>Sign-in with Google</button | ||
> | ||
<!-- | ||
<button onclick={signInWithTwitter} disabled={data.currentIdToken !== undefined} | ||
>Sign-in with Twitter</button | ||
> | ||
--> | ||
<hr /> | ||
<div> | ||
{#if errorCode} | ||
<p style="color: red;">{errorCode}</p> | ||
{/if} | ||
<p> | ||
<label | ||
>Email: <input | ||
type="text" | ||
size="30" | ||
bind:value={email} | ||
placeholder="[email protected]" | ||
/></label | ||
> | ||
<label | ||
>Password: <input | ||
type="password" | ||
size="30" | ||
bind:value={password} | ||
placeholder="your password" | ||
/></label | ||
> | ||
</p> | ||
<p> | ||
<button onclick={signInWithPassword}>Sign-In</button> | ||
<button onclick={signUpWithPassword}>Sign-Up</button> | ||
</p> | ||
</div> | ||
{/if} | ||
{/await} |
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,7 @@ | ||
<script lang="ts"> | ||
let { data } = $props(); | ||
</script> | ||
|
||
<h1>Email Verification Page</h1> | ||
|
||
<pre>{JSON.stringify(data.currentIdToken)}</pre> |