Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
thishussein committed Dec 9, 2023
0 parents commit bfdf68c
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# create-svelte

Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).

## Creating a project

If you're seeing this, you've probably already done this step. Congrats!

```bash
# create a new project in the current directory
npm create svelte@latest

# create a new project in my-app
npm create svelte@latest my-app
```

## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

To create a production version of your app:

```bash
npm run build
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
Binary file added bun.lockb
Binary file not shown.
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "test",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.27.4",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.4.2"
},
"type": "module",
"trustedDependencies": [
"sharp"
],
"dependencies": {
"@faker-js/faker": "^8.3.1",
"@sveltejs/adapter-netlify": "^2.0.8",
"jose": "^5.1.3",
"sharp": "^0.33.0"
}
}
12 changes: 12 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
}
}

export {};
12 changes: 12 additions & 0 deletions src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// place files you want to import through the `$lib` alias in this folder.
48 changes: 48 additions & 0 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sharp from "sharp";
import { faker } from "@faker-js/faker";
import { EncryptJWT, jwtDecrypt } from "jose";
import { SECRET } from "$env/static/private";
import { fail } from "@sveltejs/kit";

const encodedSecret = new TextEncoder().encode(SECRET);

export const actions = {
default: async (e) => {
const data = Object.fromEntries(await e.request.formData());

if (typeof data.jwt === "string" && typeof data.answer === "string") {
try {
const { payload } = await jwtDecrypt(data.jwt, encodedSecret);

if (payload.answer === data.answer.trim()) {
return { success: true };
} else {
return fail(400, { error: "You are wrong" });
}
} catch {
return fail(400, { error: "Invalid JWT" });
}
}
},
};

export async function load() {
const answer = faker.word.noun();

const captcha = sharp({
text: { text: answer, width: 640, height: 480 },
}).png();

const jwt = await new EncryptJWT({ answer })
.setProtectedHeader({
enc: "A128CBC-HS256",
alg: "dir",
})
.encrypt(encodedSecret);

const buffer = await captcha.toBuffer();

const img = `data:image/png;base64,${buffer.toString("base64")}`;

return { img, jwt };
}
33 changes: 33 additions & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script>
import { enhance } from "$app/forms";
export let data;
export let form;
</script>

<svelte:head>
<title>Captcha Test</title>
</svelte:head>

<div>What does this image say?</div>
{#if form?.success}
<p>You are right!</p>
{:else if form?.error}
<p class="error">{form?.error}</p>
{/if}
<img src={data.img} />
<form method="POST" use:enhance>
<input hidden value={data.jwt} name="jwt" />
<input placeholder="Answer" required name="answer" />
<button>Submit</button>
</form>

<style>
:global(body) {
background-color: gray;
}
.error {
color: red;
}
</style>
Binary file added static/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import adapter from "@sveltejs/adapter-netlify";
import { vitePreprocess } from "@sveltejs/kit/vite";

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: vitePreprocess(),

kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter(),
},
};

export default config;
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler"
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}
6 changes: 6 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
plugins: [sveltekit()]
});

0 comments on commit bfdf68c

Please sign in to comment.