user7429643
user7429643

Reputation:

scala akka does not redirect

I have the following backend. If going to the "localhost:8080", the login page is loaded by redirecting from "/" to "login". Login page is loaded. At submitting the login form, the "perform-login" is called. However, for some reason, there is no redirect to "storage" page. Why?

P.S. If requesting the page "storage" manually, it is loaded. The problem is with the redirect from "login" page to "storage" page. Probably, it has something to do with setting the cookies, as this command also has the return type Route.

Scala version: 2.13.6, Akka HTTP version: 10.2.6

object Backend {
    def main(args: Array[String]) = {
        implicit val system = ActorSystem(Behaviors.empty, "lowlevel")
        // needed for the future map/flatmap in the end
        implicit val executionContext: ExecutionContext = system.executionContext
        val topLevelRoute: Route = 
            concat(
                pathSingleSlash {
                    redirect("login", StatusCodes.PermanentRedirect)
                },
                path("login") {
                    complete("my login page")
                },
                path("storage") {
                    cookie("token") { tokenCookie =>
                        println("you managed to login, token:" + tokenCookie.value + "ENDLINE")
                        complete("my storage page")
                    }
                },
                path("perform-login") {
                    formFields("Username", "Password") { (username, password) =>
                        var isAbleToLogin = database.isUserLoggedIn(username, password)
                        if (isAbleToLogin == true) {
                            setCookie(HttpCookie("token", value="ThisIsMyStrongAccessToken")) {
                                //TODO: debug why does not redirect to main page
                                redirect("storage", StatusCodes.PermanentRedirect)
                            }
                        }
                        else {
                            reject(ValidationRejection("bad credentials"))
                        }
                    }
                },
                path(Remaining) { pathRest =>
                    reject(ValidationRejection("topLevelRoute, unknown path:" + pathRest + "ENDLINE"))
                }
        )

    val binding = Http().newServerAt("localhost", 8080).bind(topLevelRoute)

    println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
    StdIn.readLine() // let it run until user presses return
    binding
    .flatMap(_.unbind()) // trigger unbinding from the port
    .onComplete(_ => system.terminate()) // and shutdown when done        
}

Upvotes: 1

Views: 140

Answers (1)

user7429643
user7429643

Reputation:

Solution: When I was navigating to the "/" page, the redirect to the "/login" page happens due to the "document / redirect" request type (if analysing the network). But, in case of redirecting from "/login" page to "/storage" page, the request is of type "xhr /redirect" that cannot be done on the server side, i.e. I had to add $(location).attr('href', 'storage') to my jQuery script to make it work.

Upvotes: 1

Related Questions