Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie but missing user in db, ends up being a promise and not redirecting to login #287

Closed
STRUDSO opened this issue Jul 11, 2023 · 2 comments · Fixed by #288
Closed

Cookie but missing user in db, ends up being a promise and not redirecting to login #287

STRUDSO opened this issue Jul 11, 2023 · 2 comments · Fixed by #288

Comments

@STRUDSO
Copy link
Contributor

STRUDSO commented Jul 11, 2023

I got to this step:
https://remix.run/docs/en/1.18.1/tutorials/jokes#expected-errors

  1. I was logged in
  2. And then I "deleted" my db, and didn't seed,
  3. So my cookie was still present
    Ended up with an app error, but expected to be redirected to "login" page instead.
image

work around was this:
in session.server.ts

  export async function getUser(request: Request) {
    const userId = await getUserId(request);
    if (typeof userId !== "string") {
      return null;
    }
    
    const user = await db.user.findUnique({
      select: { id: true, username: true },
      where: { id: userId },
    });
    if (!user) {
      return logout(request);
    }
  
    return user;

and this in jokes.tsx

  export const loader = async ({ request }: LoaderArgs) => {
    const jokeListItems = await db.joke.findMany({
      orderBy: { createdAt: "desc" },
      select: { id: true, name: true },
      take: 5,
    });
    const user = await getUser(request);
    if(user instanceof Response){
        throw user;
    }
    return json({ jokeListItems, user });
  };

Very new to Remix, but this caught me a bit suprised, that it wouldn't result in redirect, because it was wrapped with async / await

@STRUDSO
Copy link
Contributor Author

STRUDSO commented Jul 11, 2023

{
  "private": true,
  "sideEffects": false,
  "scripts": {
    "build": "remix build",
    "dev": "remix dev",
    "start": "remix-serve build",
    "typecheck": "tsc"
  },
  "dependencies": {
    "@prisma/client": "^4.16.2",
    "@remix-run/css-bundle": "^1.18.1",
    "@remix-run/node": "^1.18.1",
    "@remix-run/react": "^1.18.1",
    "@remix-run/serve": "^1.18.1",
    "bcryptjs": "^2.4.3",
    "isbot": "^3.6.8",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@remix-run/dev": "^1.18.1",
    "@remix-run/eslint-config": "^1.18.1",
    "@types/bcryptjs": "^2.4.2",
    "@types/react": "^18.0.35",
    "@types/react-dom": "^18.0.11",
    "eslint": "^8.38.0",
    "prisma": "^4.16.2",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.0.4"
  },
  "engines": {
    "node": ">=14.0.0"
  },
  "prisma": {
    "seed": "ts-node --require tsconfig-paths/register prisma/seed.ts"
  }
}

@STRUDSO
Copy link
Contributor Author

STRUDSO commented Jul 12, 2023

Okay found another "workaround" which is probably more correct:
throw await logout(request);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant