I'm trying to redirect from the login page to the home page in the POST method, but the form doesn't redirect to any page after clicking on the submit button.
I'm using the MVC structure of Iris Framework and the Ctx.Redirect
method to redirect to the desired page, but it's not working.
// login_controllers.go
package controllers
import (
"github.com/kataras/iris/mvc"
"github.com/kataras/iris"
"fmt"
)
type LoginFormData struct {
Email string
Password string
}
type LoginController struct {
mvc.C
}
func (c *LoginController) Get() mvc.Result {
return mvc.View{
Name: "login.html",
}
}
func (c *LoginController) Post() {
userLoginData := LoginFormData{}
err := c.Ctx.ReadForm(&userLoginData)
if err != nil {
c.Ctx.StatusCode(iris.StatusInternalServerError)
c.Ctx.WriteString(err.Error())
}
if userLoginData.Email == "[email protected]" && userLoginData.Password == "123" {
c.Ctx.Redirect("/", iris.StatusSeeOther)
fmt.Printf("UserInner: %#v", userLoginData)
c.Ctx.Writef("UserInner: %#v", userLoginData)
}
}
The form HTML code is:
<form class="m-login__form m-form" action="/login" method="post">
<div class="form-group m-form__group">
<input class="form-control m-input" type="text" placeholder="Email" name="Email" autocomplete="off">
</div>
<div class="form-group m-form__group">
<input class="form-control m-input m-login__form-input--last" type="password" placeholder="Password" name="Password">
</div>
<div class="row m-login__form-sub">
<div class="col m--align-left">
<label class="m-checkbox m-checkbox--focus">
<input type="checkbox" name="remember">
Remember me
<span></span>
</label>
</div>
<div class="col m--align-right">
<a href="javascript:;" id="m_login_forget_password" class="m-link">
Forgotten password ?
</a>
</div>
</div>
<div class="m-login__form-action">
<button id="m_login_signin_submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air">
Login
</button>
</div>
</form>
Isn't possible to redirect in a POST method in Iris Framework/Go?
As @mkopriva said you have to call .Redirect and return from the function body, Redirect should be the last call because it sends the status code and a header location back to the client, you don't have to write anything else, browser/client will redirect and write the content of the redirect path.
Although
iris.StatusSeeOther
(http status code: 303) works on the mvc examples the iris repository has, you may want to try other statuses and check if that works (clear your browser's cache on each try for any case), for exampleiris.StatusTemporaryRedirect
(http status code: 307) or search on google for the correct redirect status code that suits your needs better.Please take a look at the https://github.com/kataras/iris/tree/master/_examples as well.