remix-demo
react router (remix) demo
git clone https://9o.is/git/remix-demo.git
create-user.ts
(1039B)
1 /*
2 Use this to create a new user and login with that user
3 Simply call this with:
4
5 npx ts-node --require tsconfig-paths/register ./cypress/support/create-user.ts username@example.com
6
7 and it will log out the cookie value you can use to interact with the server
8 as that new user.
9 */
10
11 import { installGlobals } from "@remix-run/node";
12 import { parse } from "cookie";
13
14 import { createUser } from "~/models/user.server";
15 import { createUserSession } from "~/session.server";
16
17 installGlobals();
18
19 async function createAndLogin(name: string, email: string) {
20 const user = await createUser({ name, email });
21
22 const response = await createUserSession({
23 request: new Request("test://test"),
24 userId: user.id,
25 });
26
27 const cookieValue = response.headers.get("Set-Cookie");
28 if (!cookieValue) {
29 throw new Error("Cookie missing from createUserSession response");
30 }
31 const parsedCookie = parse(cookieValue);
32 console.log(`\n<cookie>${parsedCookie.__session}</cookie>\n`.trim());
33 }
34
35 createAndLogin(process.argv[2], process.argv[3]);