From 990e173d67f4e3a8b46391fa6a6c3943392134c0 Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 28 Mar 2024 21:39:55 +0100 Subject: [PATCH] feat: test-auth --- docs/src/components/GitHubStats.svelte | 11 ++---- docs/src/components/SiteTitle.astro | 12 +++--- .../src/components/github/AnsweredUser.svelte | 8 +++- docs/src/components/github/SignUp.svelte | 38 +++++++++++++++++++ docs/src/components/github/github-store.ts | 23 ++++++++++- docs/src/middleware.ts | 32 ++++++++++++++++ docs/src/pages/auth/authorize.js | 11 ++++++ docs/src/pages/auth/authorized.js | 12 ++++++ 8 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 docs/src/components/github/SignUp.svelte create mode 100644 docs/src/middleware.ts create mode 100644 docs/src/pages/auth/authorize.js create mode 100644 docs/src/pages/auth/authorized.js diff --git a/docs/src/components/GitHubStats.svelte b/docs/src/components/GitHubStats.svelte index f834f71..73b91cb 100644 --- a/docs/src/components/GitHubStats.svelte +++ b/docs/src/components/GitHubStats.svelte @@ -31,15 +31,15 @@ {#if !error && !loading}
- +
{stargazersCount}
- +
{/if} @@ -56,14 +56,11 @@ align-items: center; font-size: 12px; gap: 0.25rem; - } - - .star { color: var(--sl-color-text); text-decoration: none; } - .star:hover { + .category:hover { color: var(--sl-color-accent-high); } diff --git a/docs/src/components/SiteTitle.astro b/docs/src/components/SiteTitle.astro index f878f6b..bee77b6 100644 --- a/docs/src/components/SiteTitle.astro +++ b/docs/src/components/SiteTitle.astro @@ -2,16 +2,18 @@ import Default from '@astrojs/starlight/components/SiteTitle.astro'; import GitHubStats from './GitHubStats.svelte'; import MyIcon from './MyIcon.astro'; - +import SignUp from './github/SignUp.svelte'; --- - - - - + + + + + + diff --git a/docs/src/components/github/AnsweredUser.svelte b/docs/src/components/github/AnsweredUser.svelte index 11501c1..75b1107 100644 --- a/docs/src/components/github/AnsweredUser.svelte +++ b/docs/src/components/github/AnsweredUser.svelte @@ -1,6 +1,6 @@ +token: {$token} {#if $isLoaded}
Answered by
- {#each $data as { user }} + {#each $data as { user, html_url }} + + {/each}
{/if} diff --git a/docs/src/components/github/SignUp.svelte b/docs/src/components/github/SignUp.svelte new file mode 100644 index 0000000..046ca98 --- /dev/null +++ b/docs/src/components/github/SignUp.svelte @@ -0,0 +1,38 @@ + + +{$test} + + + + diff --git a/docs/src/components/github/github-store.ts b/docs/src/components/github/github-store.ts index 8103c23..0c5a5c0 100644 --- a/docs/src/components/github/github-store.ts +++ b/docs/src/components/github/github-store.ts @@ -1,6 +1,6 @@ import { derived, writable } from 'svelte/store'; -export const count = writable(0); +export const token = writable(null); export const isLoading = writable(true); export const error = writable(false); @@ -11,3 +11,24 @@ export const isLoaded = derived( [isLoading, error], ([$isLoading, $error]) => !$isLoading && !$error, ); + +const TOKEN_KEY = 'TOKEN'; + +export function loadToken() { + // Get the current value from localStorage if it exists, otherwise use the startValue + const persistedValue = localStorage.getItem(TOKEN_KEY); + if (persistedValue) { + token.set(JSON.parse(persistedValue)); + return; + } + + token.set('API call'); +} + +token.subscribe((value) => { + if (value) { + localStorage.setItem(TOKEN_KEY, JSON.stringify(value)); + } +}); + +export const test = writable('test'); diff --git a/docs/src/middleware.ts b/docs/src/middleware.ts new file mode 100644 index 0000000..193b7cf --- /dev/null +++ b/docs/src/middleware.ts @@ -0,0 +1,32 @@ +import { defineMiddleware } from 'astro/middleware'; + +const GITHUB_OAUTH_AUTHORIZE_URL = 'https://github.com/login/oauth/authorize'; + +export const onRequest = defineMiddleware((context, next) => { + console.log(context.url.pathname); + if (context.url.pathname !== '/auth') { + return next(); + } + + const appReturnUrl = context.request.url; + const url = new URL(context.request.url); + + console.log('je rentre ici'); + console.log(context.url); + console.log(context.site); + // console.dir(context.params); + + // if (!appReturnUrl) { + // res.status(400).json({ error: '`redirect_uri` is required.' }); + // return; + // } + + // const { client_id } = env; + // const redirect_uri = `http://${context.headers.host}/api/oauth/authorized`; + // const state = await encodeState(appReturnUrl, env.encryption_password); + // + // const oauthParams = new URLSearchParams({ client_id, redirect_uri, state }); + // context.redirect(`${GITHUB_OAUTH_AUTHORIZE_URL}?${oauthParams}`, 302); + + return Response.redirect(new URL('/', context.url)); +}); diff --git a/docs/src/pages/auth/authorize.js b/docs/src/pages/auth/authorize.js new file mode 100644 index 0000000..bfc45db --- /dev/null +++ b/docs/src/pages/auth/authorize.js @@ -0,0 +1,11 @@ +const GITHUB_OAUTH_AUTHORIZE_URL = 'https://github.com/login/oauth/authorize'; + +export async function GET({params, redirect}) { + + console.log('Authorize request', params); + + const redirect_uri = 'http://localhost:4321/auth/localized' + + const oauthParams = new URLSearchParams({ client_id:'Iv1.711903007f608691' , redirect_uri, state: 'lqsksqd' }); + return redirect(`${GITHUB_OAUTH_AUTHORIZE_URL}?${oauthParams}`, 302) +} diff --git a/docs/src/pages/auth/authorized.js b/docs/src/pages/auth/authorized.js new file mode 100644 index 0000000..36a4cdf --- /dev/null +++ b/docs/src/pages/auth/authorized.js @@ -0,0 +1,12 @@ +const GITHUB_OAUTH_AUTHORIZE_URL = 'https://github.com/login/oauth/authorize'; + +export async function GET({params, request}) { + + console.log('Authorized', params); + + + return new Response({ + status: 302, + path: `/`, + }); +}