Skip to content

Commit

Permalink
Added examples folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ciber94 committed Sep 20, 2023
1 parent 437be96 commit 269a960
Show file tree
Hide file tree
Showing 24 changed files with 725 additions and 44 deletions.
13 changes: 13 additions & 0 deletions examples/sveltekit-mal-auth/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example

# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock
30 changes: 30 additions & 0 deletions examples/sveltekit-mal-auth/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
root: true,
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:svelte/recommended',
'prettier'
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
extraFileExtensions: ['.svelte']
},
env: {
browser: true,
es2017: true,
node: true
},
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser'
}
}
]
};
10 changes: 10 additions & 0 deletions examples/sveltekit-mal-auth/.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-*
2 changes: 2 additions & 0 deletions examples/sveltekit-mal-auth/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
engine-strict=true
resolution-mode=highest
13 changes: 13 additions & 0 deletions examples/sveltekit-mal-auth/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example

# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock
9 changes: 9 additions & 0 deletions examples/sveltekit-mal-auth/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte"],
"pluginSearchDirs": ["."],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
38 changes: 38 additions & 0 deletions examples/sveltekit-mal-auth/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.
36 changes: 36 additions & 0 deletions examples/sveltekit-mal-auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "sveltekit-mal-auth",
"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",
"test": "vitest",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write ."
},
"dependencies": {
"@mal/auth-sveltekit": "*"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.20.4",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte": "^2.30.0",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.10.1",
"svelte": "^4.0.5",
"svelte-check": "^3.4.3",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.4.2",
"vitest": "^0.34.0"
},
"type": "module"
}
12 changes: 12 additions & 0 deletions examples/sveltekit-mal-auth/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 examples/sveltekit-mal-auth/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" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
7 changes: 7 additions & 0 deletions examples/sveltekit-mal-auth/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Handle } from "@sveltejs/kit";



export const handle: Handle = () => {
return new Response();
}
7 changes: 7 additions & 0 deletions examples/sveltekit-mal-auth/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { describe, it, expect } from 'vitest';

describe('sum test', () => {
it('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
});
1 change: 1 addition & 0 deletions examples/sveltekit-mal-auth/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.
2 changes: 2 additions & 0 deletions examples/sveltekit-mal-auth/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Welcome to SvelteKit</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
Binary file added examples/sveltekit-mal-auth/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 examples/sveltekit-mal-auth/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import adapter from '@sveltejs/adapter-auto';
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;
17 changes: 17 additions & 0 deletions examples/sveltekit-mal-auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true
}
// 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
}
9 changes: 9 additions & 0 deletions examples/sveltekit-mal-auth/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
plugins: [sveltekit()],
test: {
include: ['src/**/*.{test,spec}.{js,ts}']
}
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"private": true,
"license": "MIT",
"workspaces": [
"packages/*"
"packages/*",
"examples/*"
],
"dependencies": {},
"devDependencies": {
Expand Down
107 changes: 107 additions & 0 deletions packages/mal-auth-sveltekit/src/client/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { dev } from "$app/environment";
import { type User } from "@mal/core";
import { getSession } from "@mal/auth/client";
import { get, writable } from "svelte/store";

let initialized = false;

export type SessionState = {
user: User | null;
accessToken: string | null;
loading: boolean;
}

const sessionStore = writable<SessionState>({
user: null,
accessToken: null,
loading: false
});

type InitializeSession = Omit<SessionState, 'loading'>;

function setUserSession(session: InitializeSession | null) {
if (session) {
initialized = true;
sessionStore.set({
loading: false,
accessToken: session.accessToken,
user: session.user
})
} else {
initialized = false;
sessionStore.set({
loading: false,
accessToken: null,
user: null
});
}
}

async function fetchUserSession() {
if (typeof window === 'undefined') {
return;
}

initialized = true;

try {
// Set state to loading
const currentSession = get(sessionStore);
sessionStore.set({
loading: true,
accessToken: currentSession.accessToken,
user: currentSession.user
});

// fetch the current user session
const session = await getSession();

if (session == null) {
return sessionStore.set({ loading: false, accessToken: null, user: null });
}

if (dev) {
console.log("🍥 User session loaded: ", JSON.stringify(session, null, 2));
}

// Currently the expiration of the access token is 31 days, which is really long,
// so we don't have reason to refresh it, each time the user log in a new token will be created.
const { accessToken, user } = session;
sessionStore.set({ user, accessToken, loading: false })
}
catch (err) {
console.error(err);
initialized = false;
sessionStore.set({ user: null, accessToken: null, loading: false })
}
}

async function initialize(session?: InitializeSession | null) {
if (session && initialized) {
return;
}

if (session !== undefined) {
setUserSession(session);
}
else {
await fetchUserSession();
}
}

function destroy() {
sessionStore.set({
accessToken: null,
loading: false,
user: null,
})
}

export default {
initialize,
destroy,
subscribe: sessionStore.subscribe,
get current() {
return get(sessionStore);
}
}
File renamed without changes.
Loading

0 comments on commit 269a960

Please sign in to comment.