-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from /issues/23
Issues/23
- Loading branch information
Showing
27 changed files
with
2,046 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ESA_APP_CLIENT_ID = "xxxxxxxxxxxxxxxxxx" | ||
ESA_APP_CLIENT_SECRET = "xxxxxxxxxxxxxxxxxx" | ||
ESA_APP_REDIRECT_URI = "xxxxxxxxxxxxxxxxxx" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VITE_ESA_APP_CLIENT_ID=xxxxxxxxxxxxxxxxxx | ||
VITE_ESA_APP_REDIRECT_URI=xxxxxxxxxxxxxxxxxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
vite.config.mts | ||
src/vite-env.d.ts | ||
functions/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,8 @@ dist-ssr | |
*.sw? | ||
*.env | ||
*.env.local | ||
!.env.example | ||
!.env.example | ||
|
||
src/lib/services/esa.gen.ts | ||
.wrangler/ | ||
.dev.vars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type Env = { | ||
ESA_APP_CLIENT_ID: string; | ||
ESA_APP_CLIENT_SECRET: string; | ||
ESA_APP_REDIRECT_URI: string; | ||
}; | ||
|
||
export function getEnv<T extends keyof Env>(env: Partial<Env>, key: T): Env[T] { | ||
const value = env[key]; | ||
if (value == null) { | ||
throw new Error(`Environment variable ${key} is not set`); | ||
} | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { PagesFunction } from "@cloudflare/workers-types"; | ||
import { string, object } from "yup"; | ||
import { Env } from "../__lib/consts.js"; | ||
|
||
export const onRequestOptions: PagesFunction<Env> = async ({ env }) => { | ||
return new Response(null, { | ||
status: 204, | ||
headers: { | ||
"Access-Control-Allow-Origin": new URL(env.ESA_APP_REDIRECT_URI).origin, | ||
"Access-Control-Allow-Headers": "*", | ||
"Access-Control-Allow-Methods": "GET, OPTIONS", | ||
"Access-Control-Max-Age": "86400", | ||
}, | ||
}); | ||
}; | ||
|
||
export const onRequest: PagesFunction<Env> = async ({ request, env }) => { | ||
if (request.method !== "POST") { | ||
return new Response("Method not allowed.", { status: 405 }); | ||
} | ||
|
||
const requestAccessTokenSchema = object().shape({ | ||
code: string(), | ||
}); | ||
|
||
let code: string; | ||
try { | ||
code = (await requestAccessTokenSchema.validate(await request.json())).code; | ||
} catch (err) { | ||
return new Response(err, { status: 400 }); | ||
} | ||
|
||
const res = await fetch("https://api.esa.io/oauth/token", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
client_id: env.ESA_APP_CLIENT_ID, | ||
client_secret: env.ESA_APP_CLIENT_SECRET, | ||
code: code, | ||
grant_type: "authorization_code", | ||
redirect_uri: env.ESA_APP_REDIRECT_URI, | ||
}), | ||
}); | ||
|
||
if (!res.ok) { | ||
return new Response("Failed to request access token!", { | ||
status: 500, | ||
}); | ||
} | ||
|
||
return new Response(await res.text(), { | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"lib": [ | ||
"esnext" | ||
], | ||
"types": [ | ||
"@cloudflare/workers-types" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
|
||
export const useEsa = () => { | ||
const getAchievement = async (access_token: string, team_name: string) => { | ||
console.log(access_token); | ||
console.log(team_name); | ||
const data = await fetch(`https://api.esa.io/v1/teams/${team_name}/members`, { | ||
method: "GET", | ||
mode: "cors", | ||
headers: { | ||
Authorization: `Bearer ${access_token}`, | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
}, | ||
}); | ||
return await data.json(); | ||
} | ||
return { getAchievement }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import styled from "styled-components"; | ||
|
||
export const Center = styled.div` | ||
height: 100%; | ||
width: 100%; | ||
display: grid; | ||
place-items: center; | ||
place-content: center; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import { type ReactNode } from "react"; | ||
import { useLocation } from "react-router-dom"; | ||
import { $hasAuthenticated } from "@/lib/stores/auth"; | ||
import { redirect } from "@/router"; | ||
|
||
export function Redirects({ children }: { children: ReactNode }): ReactNode { | ||
// const { pathname } = useLocation(); | ||
const { pathname } = useLocation(); | ||
|
||
const isInPublicPaths = pathname === "/" || pathname.startsWith("/auth"); | ||
if (!$hasAuthenticated.get() && !isInPublicPaths) { | ||
redirect("/auth/login"); | ||
} | ||
|
||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { P } from "ts-pattern"; | ||
import { type Env } from "@/types/env"; | ||
|
||
export async function waitMs(ms: number): Promise<void> { | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
|
||
export function getEnv(key: keyof Env): string { | ||
const value = import.meta.env[key]; | ||
if (value == null) { | ||
throw new Error(`Environment variable ${key} is not set`); | ||
} | ||
return value; | ||
} | ||
|
||
export const S = { | ||
Error: { error: P.not(undefined) }, | ||
Loading: { isLoading: true }, | ||
Success: { data: P.not(undefined) }, | ||
}; | ||
|
||
export const APP_NAME = "esachievement"; | ||
export const LOCAL_STORAGE_VERSION = "1"; | ||
export function getLocalStorageKey(key: string, trailingColon = false): string { | ||
return `${APP_NAME}.v${LOCAL_STORAGE_VERSION}.${key}${trailingColon ? ":" : ""}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import createClient from "openapi-fetch"; | ||
import { type paths } from "./esa.gen"; | ||
import { getEnv } from "@/lib/consts"; | ||
import { $accessTokenData } from "@/lib/stores/auth"; | ||
import { type AccessTokenData } from "@/types/auth"; | ||
|
||
export function getAuthorizePageUrl(): string { | ||
const query = new URLSearchParams({ | ||
client_id: getEnv("VITE_ESA_APP_CLIENT_ID"), | ||
redirect_uri: getEnv("VITE_ESA_APP_REDIRECT_URI"), | ||
response_type: "code", | ||
scope: "read write", | ||
}); | ||
|
||
return `https://api.esa.io/oauth/authorize?${query.toString()}`; | ||
} | ||
|
||
export async function requestAccessTokenData( | ||
code: string, | ||
): Promise<AccessTokenData> { | ||
const res = await fetch("/auth/token", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ code }), | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error( | ||
`Failed to request access token!: ${res.status} ${res.statusText}`, | ||
); | ||
} | ||
|
||
return await res.json(); | ||
} | ||
|
||
export const esaClient = createClient<paths>({ | ||
baseUrl: "https://api.esa.io/v1", | ||
}); | ||
|
||
esaClient.use({ | ||
onRequest: async (req) => { | ||
const token = $accessTokenData.get(); | ||
if (token == null) throw new Error("Access token has not been set"); | ||
|
||
req.headers.set("Authorization", `Bearer ${token.access_token}`); | ||
return req; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { persistentAtom } from "@nanostores/persistent"; | ||
import { computed } from "nanostores"; | ||
import { getLocalStorageKey } from "@/lib/consts"; | ||
import { type AccessTokenData } from "@/types/auth"; | ||
import { type Nullable } from "@/types/utils"; | ||
|
||
export const $accessTokenData = persistentAtom<Nullable<AccessTokenData>>( | ||
getLocalStorageKey("accessTokenData"), | ||
undefined, | ||
{ | ||
encode: JSON.stringify, | ||
decode: JSON.parse, | ||
}, | ||
); | ||
|
||
export const $hasAuthenticated = computed( | ||
$accessTokenData, | ||
(data) => data != null, | ||
); |
Oops, something went wrong.