v icon indicating copy to clipboard operation
v copied to clipboard

x.vweb: before_request is no more triggered after adding vweb.cors

Open MedLabs opened this issue 5 months ago • 3 comments

Describe the bug

First I was checking Authorization in the before_request middleware, it worked very well. That was before the vweb.cors is committed to x.vweb Then I upgraded to weekly.2024.06 and added app.use(vweb.cors[...](...)) And before_request doesn't work anymore. I even deleted the cors code, still the same issue.

Reproduction Steps

pub struct Context {
    vweb.Context
}

pub struct App {
   vweb.Middleware[Context]
}

pub fn (mut ctx Context) before_request() {
    println("before request...")
    key := ctx.get_header(.authorization) or {"Basic none"}
    if key.split(" ")[1] != "secretkey" {
        ctx.request_error("Authorization failed")
    }
}

pub fn (app &App) index(mut ctx Context) vweb.Result {
    return ctx.ok("Hello World")
}

fn main() {
    mut app := &App{}
    app.use(vweb.cors[Context](vweb.CorsOptions{
        origins: ["*"]
        allowed_methods: [.get, .head, .put, .patch, .post, .delete]
    }))
    vweb.run[App, Context](mut app, 8080)
}

Expected Behavior

when accessing localhost:8080 on a browser, you should get "Authorization Failed" and x.vweb should log "before request..."

Current Behavior

browser shows "hello world" and nothing is printed in terminal output

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.4 de1c431

Environment details (OS name and version, etc.)

ArcoLinux gcc 13.2.1

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

MedLabs avatar Feb 08 '24 12:02 MedLabs

Meanwhile: I created a middleware check_auth and I have to use it in every handler if check_auth { // some code } else { ctx.request_error("Auth failed") }

MedLabs avatar Feb 08 '24 12:02 MedLabs

in /vlib/x/vweb/vweb.v

// first execute before_request
    $if A is HasBeforeRequest {
		app.before_request()
	}
// user_context.before_request()
	if user_context.Context.done {
		return
	}

it should run ctx.before_request() before any other middleware.

MedLabs avatar Feb 13 '24 14:02 MedLabs

Yes, but need to wait for #20762 to be fixed before a proper solution can be implemented 👍

Casper64 avatar Feb 13 '24 14:02 Casper64