Skip to content

Reassinging body in requireAuth function fails with Netlify. #7

@LoyalEnv0y

Description

@LoyalEnv0y

The workaround for redirect not having a body property works fine on local.

export async function requireAuth() {
	const isLoggedIn = false;

	if (!isLoggedIn) {
		const response = redirect('/login?message=You must be logged in');
		response.body = true; 
		throw response;
	}

	return null;
}

However reasigning the body is problametic because it throws this error

Cannot assign to 'body' because it is a read-only property.ts(2540)

It doesn't effect the local build but it fails on Netlify build step saying that

11:49:54 AM: Netlify Build                                                 
11:49:54 AM: ────────────────────────────────────────────────────────────────
11:49:54 AM: ​
11:49:54 AM: ❯ Version
11:49:54 AM:   @netlify/build 29.20.12
11:49:54 AM: ​
11:49:54 AM: ❯ Flags
11:49:54 AM:   baseRelDir: true
11:49:54 AM:   buildId: 64f98ea51381b90008fae837
11:49:54 AM:   deployId: 64f98ea51381b90008fae839
11:49:54 AM: ​
11:49:54 AM: ❯ Current directory
11:49:54 AM:   /opt/build/repo
11:49:54 AM: ​
11:49:54 AM: ❯ Config file
11:49:54 AM:   No config file was defined: using default values.
11:49:54 AM: ​
11:49:54 AM: ❯ Context
11:49:54 AM:   production
11:49:54 AM: ​
11:49:54 AM: Build command from Netlify app                                
11:49:54 AM: ────────────────────────────────────────────────────────────────
11:49:54 AM: ​
11:49:54 AM: $ npm run build
11:49:55 AM: > react-router@0.0.0 build
11:49:55 AM: > tsc && vite build
11:49:56 AM: src/utils/index.ts(30,12): error TS2540: Cannot assign to 'body' because it is a read-only property.
11:49:57 AM: ​
11:49:57 AM: build.command failed                                        
11:49:57 AM: ────────────────────────────────────────────────────────────────
11:49:57 AM: ​
11:49:57 AM:   Error message
11:49:57 AM:   Command failed with exit code 2: npm run build (https://ntl.fyi/exit-code-2)
11:49:57 AM: ​
11:49:57 AM:   Error location
11:49:57 AM:   In Build command from Netlify app:
11:49:57 AM:   npm run build
11:49:57 AM: ​
11:49:57 AM:   Resolved config
11:49:57 AM:   build:
11:49:57 AM:     command: npm run build
11:49:57 AM:     commandOrigin: ui
11:49:57 AM:     publish: /opt/build/repo/dist
11:49:57 AM:     publishOrigin: ui
11:49:57 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
11:49:57 AM: Failing build: Failed to build site
11:49:58 AM: Finished processing build request in 15.608s

Is there a possible fix for this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions