learn-react-router-6 icon indicating copy to clipboard operation
learn-react-router-6 copied to clipboard

Reassinging body in requireAuth function fails with Netlify.

Open LoyalEnv0y opened this issue 2 years ago • 2 comments

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: > [email protected] 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?

LoyalEnv0y avatar Sep 07 '23 09:09 LoyalEnv0y

Hey @LoyalEnv0y, I would like to work on this issue can you please assign me?

CodeAnk2829 avatar Oct 05 '23 19:10 CodeAnk2829

Hey @CodeAnk2829 ! First of all thank you for responding to my thread. Sadly I am not a collaborator of this repository so I cannot make you an assignee. I believe you need to ask one of the collaborators of this repository for that.

LoyalEnv0y avatar Oct 05 '23 20:10 LoyalEnv0y