-
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 #14 from MIERUNE/demo-resetmail-magiclink
password reset and magic link
- Loading branch information
Showing
16 changed files
with
120 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dotenv |
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 |
---|---|---|
|
@@ -7,6 +7,9 @@ node_modules | |
/dist | ||
.wrangler | ||
|
||
# npm | ||
package-lock.json | ||
|
||
# OS | ||
.DS_Store | ||
Thumbs.db | ||
|
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,2 +1,3 @@ | ||
<h1>Welcome to SvelteKit</h1> | ||
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p> | ||
|
||
<p>Home</p> |
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,13 +1,21 @@ | ||
<script lang="ts"> | ||
import { FirebaseError } from 'firebase/app'; | ||
import { getAuth, sendEmailVerification, sendPasswordResetEmail } from 'firebase/auth'; | ||
import { | ||
getAuth, | ||
sendEmailVerification, | ||
sendPasswordResetEmail, | ||
sendSignInLinkToEmail, | ||
signInWithEmailLink, | ||
isSignInWithEmailLink | ||
} from 'firebase/auth'; | ||
import { | ||
signInWithGoogle, | ||
waitForRedirectResult, | ||
signInWithEmailAndPassword, | ||
createUserWithEmailAndPassword | ||
} from '$lib/firebase-auth/client'; | ||
import { page } from '$app/stores'; | ||
import { browser } from '$app/environment'; | ||
const redirectResult = waitForRedirectResult(); | ||
|
@@ -17,6 +25,21 @@ | |
let password = $state(''); | ||
let errorCode = $state(''); | ||
if (browser) { | ||
const auth = getAuth(); | ||
if (isSignInWithEmailLink(auth, window.location.href)) { | ||
let email = window.localStorage.getItem('emailForSignIn'); | ||
if (!email) { | ||
email = window.prompt('Please provide your email for confirmation'); | ||
} | ||
if (email) { | ||
signInWithEmailLink(auth, email, window.location.href).then((result) => { | ||
window.localStorage.removeItem('emailForSignIn'); | ||
}); | ||
} | ||
} | ||
} | ||
async function signInWithPassword() { | ||
try { | ||
await signInWithEmailAndPassword(email, password); | ||
|
@@ -27,6 +50,23 @@ | |
} | ||
} | ||
async function sendMagicLink() { | ||
try { | ||
const auth = getAuth(); | ||
await sendSignInLinkToEmail(auth, email, { | ||
url: $page.url.origin + '/login?email-link', | ||
handleCodeInApp: true, | ||
dynamicLinkDomain: $page.url.hostname | ||
}); | ||
window.localStorage.setItem('emailForSignIn', email); | ||
alert("We've sent you an email with a link to sign in!"); | ||
} catch (error) { | ||
if (error instanceof FirebaseError) { | ||
errorCode = error.code; | ||
} | ||
} | ||
} | ||
async function signUpWithPassword() { | ||
try { | ||
const cred = await createUserWithEmailAndPassword(email, password); | ||
|
@@ -41,9 +81,14 @@ | |
async function resetPassword() { | ||
if (email !== '') { | ||
const auth = getAuth(); | ||
await sendPasswordResetEmail(auth, email, { | ||
url: $page.url.origin + '/login' | ||
}); | ||
try { | ||
await sendPasswordResetEmail(auth, email, { url: $page.url.origin + '/login' }); | ||
} catch (error) { | ||
if (error instanceof FirebaseError) { | ||
errorCode = error.code; | ||
return; | ||
} | ||
} | ||
alert('Password reset email sent.'); | ||
} | ||
email = ''; | ||
|
@@ -57,26 +102,23 @@ | |
{:then result} | ||
{#if !result} | ||
{#if $page.url.searchParams.get('next')} | ||
<p>You need to log in.</p> | ||
<p>You need to log in to view this page.</p> | ||
{/if} | ||
|
||
Single Sign-On: | ||
<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> | ||
<p>Or sign-in with email and password:</p> | ||
{#if errorCode} | ||
<p style="color: red;">{errorCode}</p> | ||
{/if} | ||
<p> | ||
<label | ||
>Email: <input | ||
type="text" | ||
type="email" | ||
size="30" | ||
bind:value={email} | ||
placeholder="[email protected]" | ||
|
@@ -92,9 +134,10 @@ | |
> | ||
</p> | ||
<p> | ||
<button onclick={signInWithPassword}>Sign-In</button> | ||
<button onclick={signUpWithPassword}>Sign-Up</button> | ||
<button onclick={resetPassword}>Reset Password</button> | ||
<button onclick={signInWithPassword} disabled={!email || !password}>Sign-In</button> | ||
<button onclick={signUpWithPassword} disabled={!email || !password}>Sign-Up</button> | ||
<button onclick={resetPassword} disabled={!email}>Reset Password</button> | ||
<button onclick={sendMagicLink} disabled={!email}>Send Magic Link</button> | ||
</p> | ||
</div> | ||
{/if} | ||
|
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
Oops, something went wrong.