-
Hi, I have a form that will update an existing user data (if already logged in), or create a new user and log him (by generating a password and creating a new user in the db). Follow by a redirection to the next page. In the second case, I'd like to use the
const genPassword = await register(formData, catalog as CatalogType);
return await authenticator.authenticate('form', /* what should I pass here ? */, {
successRedirect: '/next-page',
failureRedirect: '.', // <-- in order to redirect to the same page ? or skipping this parameter will defaulted to current page ?
}); Any help would be greatly appreciated 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
I've been trying this, but get a TS error: const { email, password } = await register(formData, catalog as CatalogType);
const data = new FormData();
data.append('email', email);
data.append('password', password);
// TS error on body:
// "Type 'FormData' is missing the following properties from type 'ReadableStream':
// readable, read, setEncoding, pause, and 22 more."
const loginRequest = new Request({ body: data });
return await authenticator.authenticate('form', loginRequest, {
successRedirect: '/next-page',
failureRedirect: '/login',
}); |
Beta Was this translation helpful? Give feedback.
-
Second unsuccessful attempt: export const action: ActionFunction = async ({ request, params }) => {
const copy = request.clone();
const formData = await request.formData();
const action = formData.get('action');
try {
switch (action) {
case 'register':
const { email, password } = await register(formData, catalog as CatalogType);
const data = new FormData();
data.append('email', email);
data.append('password', password);
/*
data NodeFormData {
_fields: { email: [ 'gnjdkgbdkjg@gmail.com' ], password: [ 'bl26kxc4jj' ] }
}
*/
const loginRequest = new Request(copy, { body: data });
return await authenticator.authenticate('form', loginRequest, {
successRedirect: '/next-page',
throwOnError: true,
}); authenticator.use(
new FormStrategy(async ({ form }) => {
const email = form.get('email') as string;
const password = form.get('password') as string;
console.log({ email, password }); // <-- returns { email: null, password: null }
let user = await verifyUser(email, password);
return user;
}),
'form',
); |
Beta Was this translation helpful? Give feedback.
-
Is there a reason why you can't call authenticator.use(
new FormStrategy(async ({ form }) => {
let email = form.get("email")
let password = await form.get("password");
if (!password) {
let result = await register(form, catalog)
return await verifyUser(email, result.password
}
return await verifyUser(email, password);
}),
'form',
); |
Beta Was this translation helpful? Give feedback.
-
And specifically about your error. To pass a It creates a FormData, set the value and creates a Request with the URL (you can use the same of your action request), the body is the FormData and sets the method to POST. |
Beta Was this translation helpful? Give feedback.
-
I tried this: export const action: ActionFunction = async ({ request, params }) => {
...
const formData = await request.formData();
const action = formData.get('action');
const { pathname } = new URL(request.url);
try {
switch (action) {
case 'register':
const { email, password } = await register(formData, catalog as CatalogType);
const body = new FormData();
body.append('email', email);
body.append('password', password);
const loginRequest = new Request('', { body, method: 'POST' });
const redirectUrl = pathname.split('/').slice(0, -1).concat('checkout').join('/');
await authenticator.authenticate('form', loginRequest, {
successRedirect: '/ok',
failureRedirect: '/login',
});
break;
...
default:
return json('Not implemented', { status: 500 });
}
} catch (error) {
console.error('error', error);
if (error instanceof Error) {
return json(JSON.parse(error.message), { status: 400 });
}
}
// setSuccessMessage(session, 'Changes has been saved');
return json('ok');
}; authenticator.use(
new FormStrategy(async ({ form }) => {
console.log({ form });
const email = form.get('email') as string;
const password = form.get('password') as string;
console.log({ email, password });
let user = await findOrCreateUser(email, password);
console.log({ user });
return user;
}),
'form',
); Seems the form/body on an empty request (
|
Beta Was this translation helpful? Give feedback.
Is there a reason why you can't call
register
inside the FormStrategy?