Giter VIP home page Giter VIP logo

Comments (7)

andreapavoni avatar andreapavoni commented on September 17, 2024 1

hi @SamuelBonilla thank you very much, it works! My bad I've only tried into() and forgetting about the into_response() function.

I think this issue might be closed. Doo you think it might be useful to add this info in README/doc? If so, I might open a PR for that :)

from graphul.

SamuelBonilla avatar SamuelBonilla commented on September 17, 2024

@andreapavoni Sure, you can try with

use graphul::{Graphul, http::{response::Redirect, Methods}, Context };

#[tokio::main]
async fn main() {
    let mut app = Graphul::new();

    app.get("/", || async {
        // Redirect::temporary(uri) Create a new Redirect that uses a 307 Temporary Redirect status code.
        // Redirect::permanent(uri) Create a new Redirect that uses a 308 Permanent Redirect status code.
        Redirect::to("/hi/samuel") // Create a new Redirect that uses a 303 See Other status code.
    });

    app.get("/hi/:name", |c: Context| async move {
        format!("hello, {}", c.params("name"))
    });

    app.run("127.0.0.1:8000").await;
}

from graphul.

andreapavoni avatar andreapavoni commented on September 17, 2024

Thank you! It somewhat works, but I still have some problem in using it in practice. I need to check authentication when loading a page, if it's not authenticated, the user should be redirected on another page. I tried:

  • inside the handler, I call the redirect if not authenticated, or render a template -> I get a compiler error:
expected struct `Redirect`, found struct `HtmlTemplate`
  • using a middleware: redirect if not authenticated, or call next.run(request).await -> I a similar compiler error:
expected struct `http::response::Response`, found struct `Redirect`

I'm mostly a Rust beginner and maybe I'm just doing some silly mistake or overlooking some detail. I understand there's something about mismatching return types, but I've seen that in axum there should be some conversion into a compatible response object even if using a redirect (not sure about this, I've build 2 micro-apps with axum, but I've never needed redirects until now)

from graphul.

SamuelBonilla avatar SamuelBonilla commented on September 17, 2024

Hmm 🤔, Could you share some example code to try to reproduce the error?

from graphul.

SamuelBonilla avatar SamuelBonilla commented on September 17, 2024

@andreapavoni try with this example, you need to use .into_response

use graphul::{Graphul, http::{response::Redirect, Methods}, Context, IntoResponse };

#[tokio::main]
async fn main() {
    let mut app = Graphul::new();

    // http://127.0.0.1:8000?redirect=true
    app.get("/", |c: Context| async move {
        // Redirect::temporary(uri) Create a new Redirect that uses a 307 Temporary Redirect status code.
        // Redirect::permanent(uri) Create a new Redirect that uses a 308 Permanent Redirect status code.
        if c.query("redirect") == "true" {
            return Redirect::to("/hi/samuel").into_response() ; // Create a new Redirect that uses a 303 See Other status code.
        }
        "index".into_response()
    });

    app.get("/hi/:name", |c: Context| async move {
        format!("hello, {}", c.params("name"))
    });

    app.run("127.0.0.1:8000").await;
}

from graphul.

andreapavoni avatar andreapavoni commented on September 17, 2024

Sure, here's an example of my use case (the real code is really messed with other stuff as of now):

app.get("/", |_c: Context| async move {
    let authenticated = false;
    if !authenticated {
        Redirect::to("/login")
    } else {
        let template = HomeTemplate {
            message: "Hello".to_string(),
        };
        HtmlTemplate(template)
    }
});

As you can see, I'm trying to redirect the user, rather than showing the template. The other case using a middleware is similar:

async fn check_user_is_authenticated(request: Req, next: Next) -> Response {
    let authenticated = false;
    if !authenticated {
        return Redirect::to("/login");
    } else {
        next.run(request).await
    }
}

The error is similar, in both cases the compiler complains about mismatching return types. Thank you for your help!

from graphul.

SamuelBonilla avatar SamuelBonilla commented on September 17, 2024

like my previous comment try with .into_reponse, this is an implementation for your example code

middleware

async fn check_user_is_authenticated(request: Req, next: Next) -> Response {
    let authenticated = false;
    if !authenticated {
        return Redirect::to("/login").into_response() ;
    } else {
        next.run(request).await
    }
}

Handler

app.get("/", |_c: Context| async move {
    let authenticated = false;
    if !authenticated {
        Redirect::to("/login").into_response()
    } else {
        let template = HomeTemplate {
            message: "Hello".to_string(),
        };
        HtmlTemplate(template).into_response()
    }
});

from graphul.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.