Skip to content

Commit

Permalink
fix: saving session
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Dec 26, 2023
1 parent 6581a18 commit 4535f21
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 31 deletions.
9 changes: 9 additions & 0 deletions examples/simple-form/src/components/Counter.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
import {BButton} from '@astro-utils/forms/forms.js';
function inc() {
Astro.locals.session.count ??= 0;
this.innerText = Astro.locals.session.count++;
}
---
<BButton onClick={inc}>Counter</BButton>
2 changes: 2 additions & 0 deletions examples/simple-form/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Layout from '../layouts/Layout.astro';
import {BButton, Bind, BindForm, BInput, FormErrors} from '@astro-utils/forms/forms.js';
import {Button} from 'reactstrap';
import 'bootstrap/dist/css/bootstrap.css';
import Counter from '../components/Counter.astro';
const form = Bind({age: 0, name: '', about: ''});
Expand Down Expand Up @@ -48,5 +49,6 @@ function makeBackgroundRed() {
<BInput type="int" name="age" required/>

<BButton as={Button} props={{color: 'info'}} onClick={formSubmit} whenFormOK>Submit</BButton>
<Counter/>
</BindForm>
</Layout>
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/forms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"csrf": "^3.1.0",
"deepmerge": "^4.3.1",
"jsonwebtoken": "^9.0.0",
"promise-timeout": "^1.3.0",
"snappy": "^7.2.2",
"superjson": "^2.2.1",
"uuid": "^9.0.0",
Expand Down
6 changes: 2 additions & 4 deletions packages/forms/src/components/WebForms.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import {asyncContext} from '@astro-utils/context';
import {createFormToken} from '../form-tools/csrf.js';
const utils = Astro.locals.__formsInternalUtils;
import {getFormOptions} from '../settings.js';
export interface Props extends astroHTML.JSX.FormHTMLAttributes {}
Expand All @@ -22,10 +22,8 @@ if (webFormsSettings.haveFileUpload) {
props.enctype = 'multipart/form-data';
}
const useSession = utils?.FORM_OPTIONS.session?.cookieOptions?.maxAge;
const useSession = getFormOptions(Astro).session?.cookieOptions?.maxAge;
const formRequestToken = useSession && (await createFormToken(Astro));
await utils?.onWebFormClose?.();
const clientScript = Astro.locals.forms.scriptToRun;
---

Expand Down
26 changes: 8 additions & 18 deletions packages/forms/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {JWTSession} from './jwt-session.js';
import {FORM_OPTIONS, type FormsSettings} from './settings.js';
import {v4 as uuid} from 'uuid';
import deepmerge from 'deepmerge';
import {timeout} from 'promise-timeout';
import FormsReact from './form-tools/forms-react.js';

const DEFAULT_FORM_OPTIONS: FormsSettings = {
Expand All @@ -20,7 +19,6 @@ const DEFAULT_FORM_OPTIONS: FormsSettings = {
maxAge: 1000 * 60 * 60 * 24 * 7
}
},
pageLoadTimeoutMS: 1000 * 5,
secret: uuid(),
logs: (type, message) => {
console[type](message);
Expand All @@ -36,30 +34,22 @@ export default function astroForms(settings: Partial<FormsSettings> = {}) {
locals.session = session.sessionData;
locals.forms = new FormsReact(likeAstro);

let response: Response;
let pageFinished: (data?: any) => void;

locals.__formsInternalUtils = {
onWebFormClose() {
session.setCookieHeader(response.headers);
pageFinished();
},
FORM_OPTIONS: FORM_OPTIONS
};

await ensureValidationSecret(likeAstro);
response = await next();
const response = await next();

const isHTML = response.headers.get('Content-Type')?.includes('text/html');
if (!locals.webFormOff && isHTML) {
try {
const pageFinishedPromise = new Promise(resolve => pageFinished = resolve);
await timeout(pageFinishedPromise, FORM_OPTIONS.pageLoadTimeoutMS);
} catch {
FORM_OPTIONS.logs?.('warn', 'WebForms page load timeout (are you sure you are using WebForms?)');
}
if (locals.webFormOff || !isHTML) {
return response;
}

return response;
const content = await response.text();
const newResponse = new Response(content, response);
session.setCookieHeader(newResponse.headers);

return newResponse;
} as MiddlewareHandler;
}
1 change: 0 additions & 1 deletion packages/forms/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export type FormsSettings = {
}
},
secret?: string,
pageLoadTimeoutMS?: number
logs?: (type: 'warn' | 'error' | 'log', message: string) => void
}

Expand Down
1 change: 0 additions & 1 deletion packages/forms/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ declare global {
* @internal
*/
__formsInternalUtils: {
onWebFormClose(): void;
FORM_OPTIONS: FormsSettings;
};
forms: FormsReact;
Expand Down

0 comments on commit 4535f21

Please sign in to comment.