Skip to content

Commit 0f1fce3

Browse files
committed
Implement OAuth
1 parent 75a3104 commit 0f1fce3

27 files changed

+213
-118
lines changed

src/components/LoginForm.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { useNavigate, useSubmission } from "@solidjs/router";
1+
import {
2+
redirect,
3+
RouteDefinition,
4+
useNavigate,
5+
useSubmission,
6+
} from "@solidjs/router";
27
import { createSignal, Show } from "solid-js";
38
import { login, register } from "~/lib";
49
import { ExclamationCircleIcon } from "@deploy-cat/heroicons-solid/24/solid/esm";
510
import { signIn, createSession } from "@solid-mediakit/auth/client";
611

712
export const LoginForm = () => {
8-
const session = createSession();
913
const loginStatus = useSubmission(login);
1014
const registerStatus = useSubmission(register);
1115

@@ -177,7 +181,12 @@ export const LoginForm = () => {
177181
<div class="divider">OR</div>
178182
<button
179183
class="btn btn-secondary w-full"
180-
onClick={() => signIn("github", { redirectTo: "/cloud" })}
184+
onClick={() =>
185+
signIn("github").then(() => {
186+
console.log("login");
187+
// throw redirect("/user");
188+
})
189+
}
181190
>
182191
Sign in with GitHub
183192
</button>

src/components/ModalWrapper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createServerAction$, redirect } from "solid-start/server";
2-
import { k8sCustomObjects } from "~/k8s";
3-
import { knative } from "~/k8s";
2+
import { k8sCustomObjects } from "~/lib/k8s";
3+
import { knative } from "~/lib/k8s";
44
import { JSXElement, Show } from "solid-js";
55
import { createStore } from "solid-js/store";
66

src/components/Protected.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, Show } from "solid-js";
22
import { useSupabase } from "solid-supabase";
33
import { useNavigate } from "@solidjs/router";
44
import { createAsync, cache } from "@solidjs/router";
5-
import { k8sCore } from "~/k8s";
5+
import { k8sCore } from "~/lib/k8s";
66
import { User } from "@supabase/supabase-js";
77

88
// const Protected = (Comp: IProtectedComponent) => {

src/components/RegisterServerAction.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createServerAction$, redirect } from "solid-start/server";
2-
import { k8sCustomObjects } from "~/k8s";
3-
import { knative } from "~/k8s";
2+
import { k8sCustomObjects } from "~/lib/k8s";
3+
import { knative } from "~/lib/k8s";
44
import { JSXElement, Show } from "solid-js";
55
import { createStore } from "solid-js/store";
66

src/components/cloud/CreateServiceForm.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Show } from "solid-js";
22
import { action, redirect, useSubmission } from "@solidjs/router";
3-
import { knative } from "~/k8s";
3+
import { knative } from "~/lib/k8s";
44
import { EnvVarsInput } from "../EnvVarsInput";
55
import { ScalingInput } from "../ScalingInput";
66
import { ResourcesInput } from "../ResourcesInput";
7-
import { getUser } from "~/lib/server";
8-
import type { Service } from "~/knative";
9-
import { toNumber } from "~/knative";
7+
import { getUser } from "~/lib/auth";
8+
import type { Service } from "~/lib/knative";
9+
import { toNumber } from "~/lib/knative";
1010

1111
const createServiceFromForm = async (form: FormData) => {
1212
"use server";
@@ -25,7 +25,7 @@ const createServiceFromForm = async (form: FormData) => {
2525
envVars: JSON.parse(form.get("env") as string) as { [key: string]: string },
2626
} as Service;
2727
const user = await getUser();
28-
await knative.createService(service, user.username);
28+
await knative.createService(service, user.name);
2929
};
3030

3131
const createServiceAction = action(createServiceFromForm, "createService");

src/components/cloud/service/Service.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { For, Show } from "solid-js";
22
import { Status } from "./Status";
3-
import { knative } from "~/k8s";
3+
import { knative } from "~/lib/k8s";
44
import { TrashIcon } from "@deploy-cat/heroicons-solid/24/solid/esm";
5-
import { getUser } from "~/lib/server";
5+
import { getUser } from "~/lib/auth";
66
import { action, useSubmission, A } from "@solidjs/router";
7-
import type { Service as KnativeService } from "~/knative";
7+
import type { Service as KnativeService } from "~/lib/knative";
88

99
const deleteServiceFromForm = async (form: FormData) => {
1010
"use server";
1111
const service = {
1212
name: form.get("name") as string,
1313
};
1414
const user = await getUser();
15-
await knative.deleteService(service.name, user.username);
15+
await knative.deleteService(service.name, user.name);
1616
};
1717

1818
const deleteServiceAction = action(deleteServiceFromForm, "createService");

src/components/cloud/service/TrafficChart.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { clientOnly } from "@solidjs/start";
2-
import { knative } from "~/k8s";
3-
import { getUser } from "~/lib/server";
2+
import { knative } from "~/lib/k8s";
3+
import { getUser } from "~/lib/auth";
44
import { rangeQuery } from "~/lib/prometheus";
55
import { cache, createAsync } from "@solidjs/router";
66
import { graphic } from "echarts";
@@ -11,9 +11,9 @@ const getData = cache(async (app: string) => {
1111
"use server";
1212

1313
const user = await getUser();
14-
const service = await knative.getService(app, user.username);
14+
const service = await knative.getService(app, user.name);
1515

16-
const namespace = user.username;
16+
const namespace = user.name;
1717
const revision = service.raw.status.latestReadyRevisionName;
1818

1919
const end = new Date();

src/composables/solidauth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getSession } from "@solid-auth/base";
22
import { createServerData$ } from "solid-start/server";
3-
import { authOptions } from "~/server/auth";
3+
import { authOptions } from "~/lib/auth";
44

55
export const useSession = () =>
66
createServerData$(

src/lib/auth.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { auth, getSession, type SolidAuthConfig } from "@solid-mediakit/auth";
2+
import GitHub from "@auth/core/providers/github";
3+
import { config } from "~/lib/config";
4+
import type { Provider } from "@auth/core/providers";
5+
import { getWebRequest } from "vinxi/http";
6+
import { redirect } from "@solidjs/router";
7+
import { z } from "zod";
8+
9+
declare module "@auth/core/types" {
10+
export interface Session {
11+
user?: DefaultSession["user"];
12+
}
13+
}
14+
15+
const providers = [] as Array<Provider>;
16+
17+
if (config?.oauth.github) {
18+
const github = GitHub({
19+
clientId: config.oauth.github.id,
20+
clientSecret: config.oauth.github.secret,
21+
});
22+
23+
github.profile = (profile) => {
24+
return {
25+
id: profile.id.toString(),
26+
name: profile.login,
27+
// name: `gh-${profile.login}`,
28+
email: profile.email,
29+
image: profile.avatar_url,
30+
};
31+
};
32+
providers.push(github);
33+
}
34+
35+
export const authOptions: SolidAuthConfig = {
36+
providers,
37+
debug: false,
38+
trustHost: true,
39+
basePath: "/api/auth",
40+
};
41+
42+
const schemaUser = z.object({
43+
name: z.string(),
44+
id: z.string().optional(),
45+
email: z.string().optional(),
46+
image: z.string().optional(),
47+
});
48+
49+
export const getUser = async () => {
50+
"use server";
51+
const request = getWebRequest();
52+
const session = await getSession(request, authOptions);
53+
try {
54+
return schemaUser.parse(session?.user);
55+
} catch (e) {
56+
throw redirect("/");
57+
}
58+
};

src/lib/config.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from "zod";
22
import fs from "fs/promises";
33
import merge from "deepmerge";
44
import { isServer } from "solid-js/web";
5+
import x from "../../dist/public/assets/user-bdc93350";
56

67
const configPath = process.cwd?.() && `${process.cwd()}/config.json`;
78

@@ -19,24 +20,34 @@ const schemaConfig = z.object({
1920
prometheus: z.object({
2021
url: z.string(),
2122
}),
23+
oauth: z.object({
24+
github: z
25+
.object({
26+
id: z.string(),
27+
secret: z.string(),
28+
})
29+
.optional(),
30+
}),
2231
});
2332

2433
const parseEnv = ({ prefix = "", envs = process.env ?? {} } = {}) => {
2534
const parsed = {} as any;
26-
Object.entries(envs).forEach(([key, value]) => {
27-
const seq = (obj: any, arr: Array<string>, v: string | undefined) => {
28-
if (typeof obj === "string") return;
29-
if (arr.length > 1) {
30-
const [pos] = arr.splice(0, 1);
31-
if (!obj[pos]) obj[pos] = {};
32-
seq(obj[pos], arr, v);
33-
} else {
34-
obj[arr[0]] = v;
35-
}
36-
};
37-
const keyArr = key.split("_").map((e) => e.toLocaleLowerCase());
38-
seq(parsed, keyArr, value);
39-
});
35+
Object.entries(envs)
36+
.filter(([key]) => key?.startsWith(prefix.toUpperCase()))
37+
.forEach(([key, value]) => {
38+
const seq = (obj: any, arr: Array<string>, v: string | undefined) => {
39+
if (typeof obj === "string") return;
40+
if (arr.length > 1) {
41+
const [pos] = arr.splice(0, 1);
42+
if (!obj[pos]) obj[pos] = {};
43+
seq(obj[pos], arr, v);
44+
} else {
45+
obj[arr[0]] = v;
46+
}
47+
};
48+
const keyArr = key.split("_").map((e) => e.toLocaleLowerCase());
49+
seq(parsed, keyArr, value);
50+
});
4051
return prefix !== "" ? parsed?.[prefix] ?? {} : parsed;
4152
};
4253

@@ -56,4 +67,4 @@ const getConfig = async () => {
5667
return config;
5768
};
5869

59-
export const config = isServer ? schemaConfig.parse(await getConfig()) : {};
70+
export const config = isServer ? schemaConfig.parse(await getConfig()) : null;

0 commit comments

Comments
 (0)