scrimba / learn-react-router-6

Starter code for each scrim in Bob Ziroll's Learn React Router 6 course on Scrimba! ⚛️
https://scrimba.com/learn/reactrouter6/
245 stars 123 forks source link

Reassinging body in requireAuth function fails with Netlify. #7

Open LoyalEnv0y opened 1 year ago

LoyalEnv0y commented 1 year ago

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?

CodeAnk2829 commented 1 year ago

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

LoyalEnv0y commented 1 year ago

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.