├── .replit ├── .prettierignore ├── .gitignore ├── static ├── robots.txt └── favicon.ico ├── src ├── routes │ ├── auth │ │ ├── unauthorized.svelte │ │ ├── login.js │ │ └── register.js │ ├── user │ │ └── index.js │ ├── profile │ │ └── index.svelte │ └── index.svelte ├── global.d.ts ├── app.html ├── lib │ ├── helpers │ │ └── db.js │ ├── Login.svelte │ └── Register.svelte └── hooks.js ├── .prettierrc ├── jsconfig.json ├── svelte.config.cjs ├── package.json └── README.md /.replit: -------------------------------------------------------------------------------- 1 | run = "npm run dev" -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .svelte/** 2 | static/** 3 | node_modules/** 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /.svelte 4 | /build 5 | /functions -------------------------------------------------------------------------------- /static/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BraydenGirard/sveltekit-cookies/HEAD/static/favicon.ico -------------------------------------------------------------------------------- /src/routes/auth/unauthorized.svelte: -------------------------------------------------------------------------------- 1 |

You are not authorized to access this page. Please login here

-------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "useTabs": true, 3 | "singleQuote": true, 4 | "trailingComma": "none", 5 | "printWidth": 100 6 | } 7 | -------------------------------------------------------------------------------- /src/global.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "paths": { 5 | "$app/*": [".svelte/dev/runtime/app/*", ".svelte/build/runtime/app/*"], 6 | "$lib/*": ["src/lib/*"] 7 | } 8 | }, 9 | "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] 10 | } 11 | -------------------------------------------------------------------------------- /src/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %svelte.head% 8 | 9 | 10 |
%svelte.body%
11 | 12 | 13 | -------------------------------------------------------------------------------- /src/lib/helpers/db.js: -------------------------------------------------------------------------------- 1 | import { Tedis } from "tedis"; 2 | 3 | let cached = global.mongo 4 | 5 | if (!cached) { 6 | cached = global.mongo = { client: null } 7 | } 8 | 9 | export default function getDbClient() { 10 | if (cached.client) { 11 | return cached.client 12 | } 13 | 14 | cached.client = new Tedis({host: "127.0.0.1", port: 6379}) 15 | 16 | return cached.client 17 | } -------------------------------------------------------------------------------- /svelte.config.cjs: -------------------------------------------------------------------------------- 1 | const node = require('@sveltejs/adapter-node'); 2 | const pkg = require('./package.json'); 3 | 4 | /** @type {import('@sveltejs/kit').Config} */ 5 | module.exports = { 6 | kit: { 7 | // By default, `npm run build` will create a standard Node app. 8 | // You can create optimized builds for different platforms by 9 | // specifying a different adapter 10 | adapter: node(), 11 | 12 | // hydrate the
element in src/app.html 13 | target: '#svelte', 14 | 15 | vite: { 16 | ssr: { 17 | external: Object.keys(pkg.dependencies || {}) 18 | } 19 | } 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /src/routes/user/index.js: -------------------------------------------------------------------------------- 1 | import { Tedis } from "tedis"; 2 | 3 | const db = new Tedis({host: "127.0.0.1", port: 6379}) 4 | 5 | export async function get({ context }) { 6 | if(!context.authenticated) { 7 | return { 8 | status: 401, 9 | body: { 10 | message: "Unauthorized" 11 | }, 12 | headers: { 13 | 'Content-Type': 'application/json' 14 | } 15 | } 16 | } 17 | 18 | const user = JSON.parse(await db.get(context.email)); 19 | 20 | delete user.password 21 | return { 22 | status: 200, 23 | body: user 24 | }; 25 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "SvelteKit-Cookies", 3 | "version": "0.0.1", 4 | "scripts": { 5 | "dev": "svelte-kit dev", 6 | "build": "svelte-kit build", 7 | "start": "svelte-kit start", 8 | "lint": "prettier --check .", 9 | "format": "prettier --write ." 10 | }, 11 | "devDependencies": { 12 | "@sveltejs/adapter-node": "next", 13 | "@sveltejs/kit": "next", 14 | "svelte": "^3.29.0", 15 | "vite": "^2.1.0", 16 | "prettier": "~2.2.1", 17 | "prettier-plugin-svelte": "^2.2.0" 18 | }, 19 | "type": "module", 20 | "engines": { 21 | "node": ">= 12.17.0" 22 | }, 23 | "dependencies": { 24 | "cookie": "^0.4.1", 25 | "string-hash": "^1.1.3", 26 | "tedis": "^0.1.12", 27 | "uuid": "^8.3.2" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/routes/profile/index.svelte: -------------------------------------------------------------------------------- 1 | 16 | 17 | 27 | 28 |

Profile Page

29 | 30 |

Welcome to your profile {name}! We got your email from the session, it should be {email} is that correct?

-------------------------------------------------------------------------------- /src/lib/Login.svelte: -------------------------------------------------------------------------------- 1 | 28 |

Login

29 | 30 | 31 | -------------------------------------------------------------------------------- /src/hooks.js: -------------------------------------------------------------------------------- 1 | import * as cookie from 'cookie' 2 | import { Tedis } from "tedis"; 3 | 4 | const db = new Tedis({host: "127.0.0.1", port: 6379}) 5 | 6 | export async function getContext({ headers }) { 7 | const cookies = cookie.parse(headers.cookie || '') 8 | 9 | if(!cookies.session_id) { 10 | return { 11 | authenticated: false 12 | } 13 | } 14 | 15 | const userSession = JSON.parse(await db.get(cookies.session_id)) 16 | 17 | if(userSession) { 18 | return { 19 | authenticated: true, 20 | email: userSession.email 21 | } 22 | } else { 23 | return { 24 | authenticated: false 25 | } 26 | } 27 | } 28 | 29 | export function getSession({ context }) { 30 | if(!context.authenticated) { 31 | return { 32 | authenticated: context.authenticated 33 | }; 34 | } 35 | return { 36 | authenticated: context.authenticated, 37 | email: context.email 38 | }; 39 | } -------------------------------------------------------------------------------- /src/lib/Register.svelte: -------------------------------------------------------------------------------- 1 | 30 |

Register

31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/routes/index.svelte: -------------------------------------------------------------------------------- 1 | 9 | 10 |
11 | 12 | 13 |
14 | 15 | 53 | -------------------------------------------------------------------------------- /src/routes/auth/login.js: -------------------------------------------------------------------------------- 1 | import stringHash from 'string-hash' 2 | import * as cookie from 'cookie'; 3 | import { v4 as uuidv4 } from 'uuid'; 4 | import { Tedis } from "tedis"; 5 | 6 | const db = new Tedis({host: "127.0.0.1", port: 6379}) 7 | 8 | export async function post({ body }) { 9 | const user = JSON.parse(await db.get(body.email)); 10 | if(!user) { 11 | return { 12 | status: 401, 13 | body: { 14 | message: "Unauthorized" 15 | } 16 | } 17 | } 18 | if(user.password !== stringHash(body.password)) { 19 | return { 20 | status: 401, 21 | body: { 22 | message: "Unauthorized" 23 | } 24 | } 25 | } 26 | 27 | const cookieId = uuidv4() 28 | await db.set(cookieId, JSON.stringify({ 29 | email: user.email 30 | })) 31 | 32 | return { 33 | status: 200, 34 | headers: { 35 | 'Set-Cookie': cookie.serialize('session_id', cookieId, { 36 | httpOnly: true, 37 | maxAge: 60 * 60 * 24 * 7, 38 | sameSite: 'lax', 39 | path: '/' 40 | }) 41 | }, 42 | body: { 43 | message: "Success" 44 | } 45 | }; 46 | } -------------------------------------------------------------------------------- /src/routes/auth/register.js: -------------------------------------------------------------------------------- 1 | import stringHash from 'string-hash' 2 | import * as cookie from 'cookie'; 3 | import { v4 as uuidv4 } from 'uuid'; 4 | import { Tedis } from "tedis"; 5 | 6 | const db = new Tedis({host: "127.0.0.1", port: 6379}) 7 | 8 | export async function post({ body }) { 9 | const user = JSON.parse(await db.get(body.email)); 10 | if(user) { 11 | return { 12 | status: 409, 13 | body: { 14 | message: "User with that email already exists" 15 | } 16 | } 17 | } 18 | 19 | await db.set(body.email, JSON.stringify({ 20 | email: body.email, 21 | password: stringHash(body.password), 22 | name: body.name 23 | })) 24 | 25 | const cookieId = uuidv4() 26 | await db.set(cookieId, JSON.stringify({ 27 | email: body.email 28 | })) 29 | const headers = { 30 | 'Set-Cookie': cookie.serialize('session_id', cookieId, { 31 | httpOnly: true, 32 | maxAge: 60 * 60 * 24 * 7, 33 | sameSite: 'lax', 34 | path: '/' 35 | }) 36 | } 37 | return { 38 | status: 200, 39 | headers, 40 | body: { 41 | message: "Success" 42 | } 43 | }; 44 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # create-svelte 2 | 3 | Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte); 4 | 5 | ## Creating a project 6 | 7 | If you're seeing this, you've probably already done this step. Congrats! 8 | 9 | ```bash 10 | # create a new project in the current directory 11 | npm init svelte@next 12 | 13 | # create a new project in my-app 14 | npm init svelte@next my-app 15 | ``` 16 | 17 | > Note: the `@next` is temporary 18 | 19 | ## Developing 20 | 21 | Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: 22 | 23 | ```bash 24 | npm run dev 25 | 26 | # or start the server and open the app in a new browser tab 27 | npm run dev -- --open 28 | ``` 29 | 30 | ## Building 31 | 32 | Svelte apps are built with _adapters_, which optimise your project for deployment to different environments. 33 | 34 | By default, `npm run build` will generate a Node app that you can run with `node build`. To use a different adapter, add it to the `devDependencies` in `package.json` making sure to specify the version as `next` and update your `svelte.config.cjs` to [specify your chosen adapter](https://kit.svelte.dev/docs#configuration-adapter). The following official adapters are available: 35 | 36 | - [@sveltejs/adapter-node](https://github.com/sveltejs/kit/tree/master/packages/adapter-node) 37 | - [@sveltejs/adapter-static](https://github.com/sveltejs/kit/tree/master/packages/adapter-static) 38 | - [@sveltejs/adapter-netlify](https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify) 39 | - [@sveltejs/adapter-vercel](https://github.com/sveltejs/kit/tree/master/packages/adapter-vercel) 40 | - ...more soon 41 | 42 | [See the adapter documentation for more detail](https://kit.svelte.dev/docs#adapters) 43 | --------------------------------------------------------------------------------