├── .npmrc ├── README.md ├── .gitattributes ├── static └── favicon.png ├── src ├── routes │ ├── +page.svelte │ ├── privatedashboard │ │ └── +page.svelte │ └── +layout.svelte ├── app.html ├── lib │ └── firebase │ │ └── firebase.client.js ├── stores │ └── authStore.js └── components │ ├── AuthReset.svelte │ └── Auth.svelte ├── vite.config.js ├── .gitignore ├── svelte.config.js ├── .eslintignore ├── .prettierignore ├── .prettierrc ├── .eslintrc.cjs └── package.json /.npmrc: -------------------------------------------------------------------------------- 1 | engine-strict=true 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sveltekit-auth 2 | SvelteKit Auth W. Firebase 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /static/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jamezmca/sveltekit-auth/HEAD/static/favicon.png -------------------------------------------------------------------------------- /src/routes/+page.svelte: -------------------------------------------------------------------------------- 1 | 5 | -------------------------------------------------------------------------------- /vite.config.js: -------------------------------------------------------------------------------- 1 | import { sveltekit } from '@sveltejs/kit/vite'; 2 | 3 | const config = { 4 | plugins: [sveltekit()] 5 | }; 6 | 7 | export default config; 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /build 4 | /.svelte-kit 5 | /package 6 | .env 7 | .env.* 8 | !.env.example 9 | vite.config.js.timestamp-* 10 | vite.config.ts.timestamp-* 11 | -------------------------------------------------------------------------------- /svelte.config.js: -------------------------------------------------------------------------------- 1 | import adapter from '@sveltejs/adapter-auto'; 2 | 3 | /** @type {import('@sveltejs/kit').Config} */ 4 | const config = { 5 | kit: { 6 | adapter: adapter() 7 | } 8 | }; 9 | 10 | export default config; 11 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /build 4 | /.svelte-kit 5 | /package 6 | .env 7 | .env.* 8 | !.env.example 9 | 10 | # Ignore files for PNPM, NPM and YARN 11 | pnpm-lock.yaml 12 | package-lock.json 13 | yarn.lock 14 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /build 4 | /.svelte-kit 5 | /package 6 | .env 7 | .env.* 8 | !.env.example 9 | 10 | # Ignore files for PNPM, NPM and YARN 11 | pnpm-lock.yaml 12 | package-lock.json 13 | yarn.lock 14 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "useTabs": true, 3 | "singleQuote": true, 4 | "trailingComma": "none", 5 | "printWidth": 100, 6 | "plugins": ["prettier-plugin-svelte"], 7 | "pluginSearchDirs": ["."], 8 | "overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }] 9 | } 10 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['eslint:recommended', 'prettier'], 4 | plugins: ['svelte3'], 5 | overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }], 6 | parserOptions: { 7 | sourceType: 'module', 8 | ecmaVersion: 2020 9 | }, 10 | env: { 11 | browser: true, 12 | es2017: true, 13 | node: true 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /src/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %sveltekit.head% 8 | 17 | 18 | 19 |
%sveltekit.body%
20 | 21 | 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sveltekit-auth", 3 | "version": "0.0.1", 4 | "private": true, 5 | "scripts": { 6 | "dev": "vite dev", 7 | "build": "vite build", 8 | "preview": "vite preview", 9 | "lint": "prettier --plugin-search-dir . --check . && eslint .", 10 | "format": "prettier --plugin-search-dir . --write ." 11 | }, 12 | "devDependencies": { 13 | "@sveltejs/adapter-auto": "^1.0.0", 14 | "@sveltejs/kit": "^1.0.0", 15 | "eslint": "^8.28.0", 16 | "eslint-config-prettier": "^8.5.0", 17 | "eslint-plugin-svelte3": "^4.0.0", 18 | "prettier": "^2.8.0", 19 | "prettier-plugin-svelte": "^2.8.1", 20 | "svelte": "^3.54.0", 21 | "vite": "^4.0.0" 22 | }, 23 | "type": "module", 24 | "dependencies": { 25 | "firebase": "^9.17.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/routes/privatedashboard/+page.svelte: -------------------------------------------------------------------------------- 1 | 12 | 13 | {#if $authStore.currentUser} 14 |
15 |

CURRENT USER: {email}

16 | 17 | 18 | 19 |
20 | {:else} 21 |
Loading....
22 | {/if} 23 | 24 | 37 | -------------------------------------------------------------------------------- /src/routes/+layout.svelte: -------------------------------------------------------------------------------- 1 | 26 | 27 |
28 | 29 |
30 | 31 | 38 | -------------------------------------------------------------------------------- /src/lib/firebase/firebase.client.js: -------------------------------------------------------------------------------- 1 | // Import the functions you need from the SDKs you need 2 | import { deleteApp, getApp, getApps, initializeApp } from "firebase/app"; 3 | // TODO: Add SDKs for Firebase products that you want to use 4 | // https://firebase.google.com/docs/web/setup#available-libraries 5 | import { getAuth, setPersistence, inMemoryPersistence } from 'firebase/auth' 6 | 7 | // Your web app's Firebase configuration 8 | const firebaseConfig = { 9 | apiKey: import.meta.env.VITE_APIKEY, 10 | authDomain: import.meta.env.VITE_AUTHDOMAIN, 11 | projectId: import.meta.env.VITE_PROJECTID, 12 | storageBucket: import.meta.env.VITE_STORAGEBUCKET, 13 | messagingSenderId: import.meta.env.VITE_MESSAGINGSENDERID, 14 | appId: import.meta.env.VITE_APPID 15 | }; 16 | 17 | // Initialize Firebase 18 | let firebaseApp; 19 | if (!getApps().length) { 20 | firebaseApp = initializeApp(firebaseConfig) 21 | } else { 22 | firebaseApp = getApp() 23 | deleteApp(firebaseApp) 24 | firebaseApp = initializeApp(firebaseConfig) 25 | } 26 | 27 | export const auth = getAuth(firebaseApp) -------------------------------------------------------------------------------- /src/stores/authStore.js: -------------------------------------------------------------------------------- 1 | import { createUserWithEmailAndPassword, sendPasswordResetEmail, signInWithEmailAndPassword, signOut, updateEmail, updatePassword } from "firebase/auth"; 2 | import { writable } from "svelte/store"; 3 | import { auth } from "../lib/firebase/firebase.client"; 4 | 5 | export const authStore = writable({ 6 | isLoading: true, 7 | currentUser: null 8 | }) 9 | 10 | export const authHandlers = { 11 | login: async (email, password) => { 12 | await signInWithEmailAndPassword(auth, email, password) 13 | }, 14 | signup: async (email, password) => { 15 | await createUserWithEmailAndPassword(auth, email, password) 16 | }, 17 | logout: async () => { 18 | await signOut(auth) 19 | }, 20 | resetPassword: async (email) => { 21 | console.log('WE ARE HERE', email) 22 | if (!email) { 23 | console.log('inHERE') 24 | return 25 | } 26 | await sendPasswordResetEmail(auth, email) 27 | }, 28 | updateEmail: async (email) => { 29 | authStore.update(curr => { 30 | return { 31 | ...curr, currentUser: { 32 | ...curr.currentUser, email: email 33 | } 34 | } 35 | }) 36 | await updateEmail(auth.currentUser, email) 37 | }, 38 | updatePassword: async (password) => { 39 | await updatePassword(auth.currentUser, password) 40 | } 41 | } -------------------------------------------------------------------------------- /src/components/AuthReset.svelte: -------------------------------------------------------------------------------- 1 | 23 | 24 |
25 |
26 | 31 | 36 |
37 | {#if action === 'updatePass'} 38 |
39 | 42 | 43 | 44 |
45 | {/if} 46 | {#if action === 'updateEmail'} 47 |
48 | 51 | 52 |
53 | {/if} 54 |
55 | 56 | 75 | -------------------------------------------------------------------------------- /src/components/Auth.svelte: -------------------------------------------------------------------------------- 1 | 32 | 33 |
34 |

{register ? 'Register' : 'Log in'}

35 |
36 | 39 | 42 | {#if register} 43 | 46 | {/if} 47 | 48 |
49 | {#if register} 50 |
{ 52 | register = false; 53 | }} 54 | on:keydown={() => {}} 55 | > 56 | Already have an account? 57 |

Log in

58 |
59 | {:else} 60 |
{ 62 | register = true; 63 | }} 64 | on:keydown={() => {}} 65 | > 66 | Don't have an account?

Sign Up

67 |
68 |
{ 70 | authHandlers.resetPassword(email) 71 | }} 72 | on:keydown={() => {}} 73 | > 74 | Forgot Password? 75 |
76 | {/if} 77 |
78 | 79 | 93 | --------------------------------------------------------------------------------