-
I'm trying to understand how to validate+create a user in one route action, and then authenticate the newly-created user in another via remix-auth form strategy. I have this // services/auth.server.ts
authenticator.use(
new FormStrategy(async ({ form }) => {
const email = form.get('email');
const password = form.get('password');
invariant(email, 'Email is required');
invariant(password, 'Password is required');
const user = await userModel.verifyLogin(email, password);
invariant(user, 'Provided email or password was incorrect.');
return user;
})
); and this auth route: // routes/auth/$provider.ts
export const loader: LoaderFunction = ({ request, params }) =>
login(request, params);
export const action: ActionFunction = ({ request, params }) =>
login(request, params);
async function login(request, params) {
invariant(params.provider, "Must specify a provider.");
try {
return await authenticator.authenticate(params.provider, request, {
successRedirect: "/dashboard",
failureRedirect: "/login",
});
} catch (error) {
throw error;
}
} I have this // routes/join
export const action = async ({ request }) => {
const result = await serverSideSignupValidator.validate(await request.formData());
if (result.error) return validationError(result.error);
await createUser(result.email, result.password);
// what here?
};
function Join() {
return (
<Form>
<Input type="email" name="email" />
<Input type="password" name="password" />
</Form>
);
} This successfully creates the user, but now I want their session to be authenticated. How do I route the user from I've tried making another POST from the export const action = async ({ request }) => {
const result = await serverSideSignupValidator.validate(await request.formData());
if (result.error) return validationError(result.error);
await createUser(result.email, result.password);
// doesn't work
return fetch('/auth/form', request);
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Once // routes/join
export const action = async ({ request }) => {
const result = await serverSideSignupValidator.validate(await request.formData());
if (result.error) return validationError(result.error);
let user = await createUser(result.email, result.password);
let session = await getSession(request.headers.get("cookie"));
session.set(authenticator.sessionKey, user);
return redirect("/somewhere", {
headers: { "Set-Cookie": await commitSession(session) }
});
};
function Join() {
return (
<Form>
<Input type="email" name="email" />
<Input type="password" name="password" />
</Form>
);
} |
Beta Was this translation helpful? Give feedback.
Once
createUser
returns a new user you can manually store it in the session yourself. You only need to be sure the key of the session is the same used by Remix Auth, you can do that by usingauthenticator.sessionKey
.