Skip to content

Commit

Permalink
update README and Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Octo8080X committed Jan 4, 2024
1 parent 3673416 commit f2700a6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,48 @@ export default defineConfig({
});
```

## permission
## Permission

- read
- write(when doing cli)

## Use of authentication information

`user` Resource authenticated information can be obtained as follows.

```ts
import { LogoutForm } from "../../mod.ts";
import type { WithPlantation } from "../../mod.ts";
import type { PageProps} from "$fresh/server.ts";

export default function MustLogin(props: PageProps<unknown, WithPlantation<"user">>) {
return (
<div class="px-4 py-8 mx-auto bg-[#86efac]">
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center">
MUST LOGIN
<p>{props.state.plantation.authUserSession.resourceName} id : {props.state.plantation.authUserSession.user.userId}</p>
</div>
</div>
);
}
```

In the case of `user`, the ID of the resource can be obtained as in `props.state.plantation.authUserSession.user.userId`.
Similarly, the handler can also retrieve this.

```ts
export const handler = {
GET(req: Request, ctx: FreshContext<WithPlantation<"user">>){

// ex. get user id
// ctx.state.plantation.authUserSession.user.userId

return ctx.render()
}
}
```


## Custom handler and component

You can use the cli tool to build your own handlers and components.
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export {
passwordSchema,
usernameSchema,
} from "./src/utils/validates.ts";
export type { PlantationParams } from "./types.ts";
export type { PlantationParams , WithPlantation } from "./types.ts";
export { LogoutForm } from "./src/components/LogoutForm.tsx";
3 changes: 2 additions & 1 deletion src/middlewares/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FreshContext } from "../../deps.ts";
import { pascalCase } from "../../deps.ts";
import type { PlantationInnerParams } from "../../types.ts";

export function getPlantationMiddleware({
Expand Down Expand Up @@ -37,7 +38,7 @@ export function getPlantationMiddleware({
}

if (session) {
ctx.state[`auth${resourceName}Session`] = session;
ctx.state["plantation"]= {[`auth${pascalCase(resourceName)}Session`]: {...session, resourceName}}
}
return await ctx.next();
};
Expand Down
14 changes: 14 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ export interface DefaultActions {
) => Handlers<unknown, WithCsrf>;
};
}


type IdName<T extends string> = `${T}Id`;
type LuciaObjectKey<T extends string> = `auth${Capitalize<T>}Session`;

export interface WithPlantation<Q extends string> extends Record<string, unknown> {
plantation:{
[K in LuciaObjectKey<Q>]: {
[KK in Q]:{
[KKK in IdName<Q>]: string;
};
} & {resourceName:string}
}
}

0 comments on commit f2700a6

Please sign in to comment.