Skip to content

Commit

Permalink
improve protected layout
Browse files Browse the repository at this point in the history
  • Loading branch information
OrJDev committed Dec 20, 2022
1 parent f0ccd8f commit 35443b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
17 changes: 0 additions & 17 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,4 @@ All addons are optional, you may select some, you may select all and you may sel

![Screenshot_1](https://user-images.githubusercontent.com/91349014/201010596-4578b981-4183-4197-be43-6e01ed582954.png)

### Folders And Why

#### /env

It adds nice type safety check and zod validation to your env (both client and server), so it is recommended to use it in your code (can be used to store JWT secret, etc).

- /env/client - Client side env - `imort.meta.env`
- /env/server - Server side env - `process.env`
- /env/schema - Schema for env - `Zod Client Schema & Zod Server Schema`

#### /server

The server folder is where you put all of your server side code (anything that will not be imported by client), you may see this folder when you use `tRPC` or `Prisma`.

- /server/trpc - tRPC server side code: routers, context, utils, etc.
- /server/db - `client.ts` = Where the prisma client is being created.

This project was inspired by [create t3 app](https://github.com/t3-oss/create-t3-app) and was created in order to make it easier to create a solid app. (and replace `npm init solid`)
23 changes: 17 additions & 6 deletions src/installers/SolidAuth/utils/getProtectedLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { IUtil } from "~types";

const getProtectedLayout: IUtil = (ctx) => {
const usePrisma = ctx.installers.includes("Prisma");
return `import { Match, Switch, type Component } from "solid-js";
return `import { ${
ctx.ssr ? "Show" : "Match, Switch"
}, type Component } from "solid-js";
import { useRouteData } from "solid-start";
import { createServerData$, redirect } from "solid-start/server";
import { authenticator${
Expand All @@ -26,11 +28,7 @@ export const withProtected = (Component: ProtectedRouter) => {
Page: () => {
const current = useRouteData<typeof routeData>();
return (
<Switch fallback={<Component {...(current() as User)} />}>
<Match when={current.loading || current() instanceof Response}>
<h1>Loading...</h1>
</Match>
</Switch>
${getInnerJsx(ctx.ssr)}
);
},
};
Expand All @@ -40,4 +38,17 @@ export type ProtectedRouter = Component<User>;
`;
};

const getInnerJsx = (ssr?: boolean) => {
if (ssr) {
return ` <Show when={current()} keyed>
{(user) => <Component {...user} />}
</Show>`;
}
return ` <Switch fallback={<Component {...(current() as User)} />}>
<Match when={current.loading || current() instanceof Response}>
<h1>Loading...</h1>
</Match>
</Switch>`;
};

export default getProtectedLayout;

0 comments on commit 35443b9

Please sign in to comment.