Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning redirect response instead of expected leaf view

I have a route handler which returns a Future for my login page, defined as follows :

func boot(router: Router) throws {
    let authSessionRoutes = router.grouped(User.authSessionsMiddleware())
    authSessionRoutes.get("/login", use: loginHandler)
}

func loginHandler(_ req: Request) throws -> Future<View> {
    return try req.view().render("loginPage")
}

This works well when the user is not connected, but I'd like to add logic so that any authenticated user trying to access this page would be redirected to their homepage instead.

Here's what I tried :

func loginHandler(_ req: Request) throws -> Future<View> {

    if let _ = try req.authenticated(User.self) {
        return req.redirect(to: "/") <<< Cannot convert return expression of type 'Response' to return type 'EventLoopFuture<View>'
    }

    return try req.view().render("loginPage")
}

This tells me that since my loginHandler is expected to return a Future<View>, returning a Response does not conform to the method's definition.

What would be the best way to handle this case?

EDIT :

Thanks for the answer ! My final code looks like so :

func loginHandler(_ req: Request) throws -> Future<Response> {
    if let _ = try req.authenticated(User.self) {
        return req.future().map() {
            return req.redirect(to: "/")
        }
    }
    return try req.view().render("loginPage").encode(for: req)
}
like image 968
user2700551 Avatar asked Nov 12 '18 17:11

user2700551


People also ask

What is the difference between return view and return redirecttoaction?

1) Return View doesn’t make a new requests, it renders the view without changing URLs in the browser’s address bar. (ie., simple like Server.Transfer () ) 2) Return RedirectToAction makes a new requests and URL in the browser’s address bar is changed (ie., simple like Response.Redirect () )

What does the Redirect () Method of the response interface return?

The redirect () method of the Response interface returns a Response resulting in a redirect to the specified URL. Note: This is mainly relevant to the ServiceWorker API. A controlling service worker could intercept a page's request and redirect it as desired.

What is return redirecttoaction in Laravel?

2) Return RedirectToAction makes a new requests and URL in the browser’s address bar is changed (ie., simple like Response.Redirect () ) In this above,it will generate the URL for you based on your route table.CustomerID will pass to the customerDetail action method under the CustomerFolder

How do I redirect a redirect in redirectview?

Redirect With the RedirectView Let's start with this simple approach – and go straight to an example: Behind the scenes, RedirectView will trigger a HttpServletResponse.sendRedirect () – which will perform the actual redirect.


1 Answers

You have two options - you can either use the Abort.redirect(to:) error and throw that instead of calling the redirect.

Otherwise you can change the return type to Future<Response> and convert the rendered view to a response.

like image 126
0xTim Avatar answered Nov 15 '22 07:11

0xTim