Issue with Remix-auth verification

Recently, I added a simple login feature using remix-auth and prisma. However, in an unusual manner, within my action function, the correct credentials trigger a catch block error with a Response. Everything works fine when incorrect credentials are entered or there is a CSRF issue (using remix-utils). Here's how it's implemented:

/services/session.server.ts

import { createCookieSessionStorage } from "@remix-run/node";

if (!process.env.SESSION_SECRET) {
  throw new Error("SESSION_SECRET is not set");
}

// export the entire sessionStorage object
export const sessionStorage = createCookieSessionStorage({
  cookie: {
    name: "_vSession",
    sameSite: "lax",
    path: "/",
    httpOnly: true, 
    secrets: [process.env.SESSION_SECRET], 
    secure: process.env.NODE_ENV === "production", 
  },
});

export const { getSession, commitSession, destroySession } = sessionStorage;

/services/auth-strategies/form.strategy.server.ts

import type { UserSession } from "types/auth";
import { FormStrategy } from "remix-auth-form";
import prisma from "lib/prisma";
import { AuthorizationError } from "remix-auth";
import { compare } from "bcrypt";

export const formStrategy = new FormStrategy<UserSession>(async ({ form }) => {
  const username = form.get("username") as string;
  const password = form.get("password") as string;
  //more code here...
});

More implementation details can be found in various service files. The setup is being utilized normally.

root.tsx:

// More code here...

and in the _index.tsx, I redirect to the dashboard page.

// More code here...

The login page contains components for loader, page layout, and actions triggering the authentication flow.

// More code here...

The Form component handles user input during the login process.

// More code here...

In the login page code where the issue occurs, marked by @!@ FLOWS HERE @!@, strange behavior happens that triggers the ErrorBoundary. Why does this happen? When the authenticator function is removed from the try/catch block, the redirection works correctly even on wrong credentials.

Answer №1

Every configuration is accurate. When remix-auth successfully authenticates, it throws the Response, which then flows to the catch block. This behavior is expected in a remix application to redirect and halt loaders and actions from continuing execution. You have the option to either re-throw or return the response with this code snippet:

if (error instanceof Response) {
      // @!@ FLOWS HERE @!@
      return error;
    }

By doing this, you will achieve successful authentication.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guidelines for Nestjs class-validator exception - implementing metadata information for @IsNotIn validator error handling

I have a NestJs data transfer object (dto) structured like this import { IsEmail, IsNotEmpty, IsNotIn } from 'class-validator'; import { AppService } from './app.service'; const restrictedNames = ['Name Inc', 'Acme Inc&ap ...

The declaration file for the module 'vue-html-to-paper' was not located

Struggling to make a sample project work with HTML to PDF, but encountering an error message stating: Could not find a declaration file for module 'vue-html-to-paper' Even though it resides in my node_modules index.js import Vue from 'vue& ...

There was a problem rendering the error view configuration callback for the RCTModalHostView component - it must be a function, but it was received as 'undefined'

Working on a mobile application in React Native, specifically using Expo SDK version ~51.0.18 for development. The project involves utilizing the Expo router to manage all routing within the app. Now, I'm looking to implement a new SDK called Lean t ...

Heroku error: unable to locate tsc despite exhaustive troubleshooting efforts

I've been attempting to deploy a basic nodejs app on heroku, but I keep encountering the error mentioned above. Despite trying various solutions provided here, nothing seems to resolve the issue. Here's a summary of what I've attempted so fa ...

What is the best way to prevent jest.mock from being hoisted and only use it in a single jest unit test?

My goal is to create a mock import that will be used only in one specific jest unit test, but I am encountering some challenges. Below is the mock that I want to be restricted to just one test: jest.mock("@components/components-chat-dialog", () ...

Injecting dynamic templates in Angular 7

Let me simplify my issue: I am currently using NgxDatatable to display a CRUD table. I have a base component named CrudComponent, which manages all CRUD operations. This component was designed to be extended for all basic entities. The challenge I am en ...

Executing mouseenter and mouseleave events when dragging in Angular

Is there a proper way to trigger mouseenter and mouseleave events while dragging in the Angular framework? I've searched through the documentation but haven't found a solution yet. I've experimented with different approaches, none of which ...

Something went wrong trying to retrieve the compiled source code of https://deno.land/[email protected]/path/mod.ts. It seems the system is unable to locate the specified path. (os error 3)

Upon executing my main.ts script using deno, I encounter the following error message: error: Failed to retrieve compiled source code from https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfcccbdbff8f918a86918f"& ...

Error: The React component throws a TypeError because it is unable to read the property 'map' from an undefined source

I encountered the following error TypeError: Cannot read property 'map' of undefined at ListItemFactory.ts:84:57 at The specific line where the error occurs is: return announcementitems=json.value.map((v,i)=>( To provide mor ...

"Vuex actions fail to return any meaningful value, leading to undefined

Hello everyone, I'm currently working on a login feature using Vue, Vuex, and Axios. However, I'm facing an issue where instead of displaying the user's first name upon logging in, it only shows the access token. Additionally, when I inspect ...

The file path and installed npm package intellisense does not appear in VS Code until I press Ctrl + space for TypeScript

Before pressing ctrl + space, intellisense shows: click here for image description After pressing ctrl + space, intellisense displays: click here for image description Upon updating to vs code version 1.11.0 and opening my project, I encountered an issu ...

Using ngTemplateOutlet to pass ng-template to a child component in Angular 5

I am looking to develop a versatile component that can utilize custom templates for data rendering, while consolidating the business logic to prevent redundancy. Imagine this use case: a paginated list. The pagination logic should be housed within the com ...

An instance of an object is being added instead of parameters

I'm having some trouble making a server call using promises. Whenever I try to add my parameters, they end up showing as 'object%20Object' Here's the code snippet for the call: import { Injectable } from '@angular/core'; imp ...

Implement a loading spinner for autocompletion by utilizing an array as data source instead of relying on an

If you're interested in implementing autocomplete using an array instead of an in-memory web API, check out this thread for an example: Here's the updated search function in autocomplete.service.ts: search(filter: {name: string} = {name: '& ...

I'm experiencing an issue with redirect in Nextjs that's causing an error message to appear. The error reads: "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I'm currently diving into the world of NextJS and working on creating a simple recipe application. Utilizing the new App Router has been smooth sailing for the most part, except for one hiccup with the login function. After successfully logging in (us ...

Typescript: parameter must be included if another is also required

In the user interface, the parameter c is mandatory only if the parameter a is set to true. interface IArguments { a: boolean, b: string, c: string } The solution below seems to be effective, but how can I exclude the c parameter in the first scenar ...

What is the method for restricting object property names to valid CSS properties when applying typing?

I am working with an object that represents CSS styling: { font-size: "16px", font-family: "Arial, sans-serif", text-align: "center" } The keys in this object such as font-size, font-family, text-align are dynamic and can change. I need to find a way to ...

Encountering a Type Error while using Typescript in conjunction with React-Redux

I've been experimenting with react-redux and typescript, but I encountered a type error when using connect() and mapStateToProps to inject props. Here's how my component is structured: function mapStateToProps(state) { return { coun ...

What could be causing the availability of a response in a service, but showing as undefined in the component?

Currently, I am facing a problem with my service and component setup. While the service can successfully read the response as a JSON object, the component is returning res: undefined. service: constructor( private http: Http, private fbuilder: Fo ...

Error: The field 'password' is not found in the specified type

Hey, I'm fairly new to TypeScript and encountering an error with my express and MongoDB application. Let's take a look at my User.ts model. import mongoose from "mongoose"; interface IUser { username: string; password: string ...