B4J Question B4J Server Redirect After Registration Confirmation

cklester

Well-Known Member
Licensed User
I've got users clicking a confirmation link from their email. I'm processing the click, but I want to send them to a Success or Fail page, depending on the result of the confirmation check.

I've seen the SendRedirect, but I don't want to send a code 302. It should just be normal.

For example, they click:


and my confirm handler processes that, and then sends them either to


or


How do I handle that?
 

aeric

Expert
Licensed User
Longtime User
It depends on the returned result. Let say the results can be either “success” or “failed” from a DBResult type or JSON string. Base on this you can do what ever redirects you want.
 
Upvote 0

cklester

Well-Known Member
Licensed User
How do I do a Redirect without throwing a code 302? The docs say that Redirect throws a code 302. If I change the status code with something like

resp.Status = 200

will that override the 302 thrown by the Redirect?
 
Upvote 0

cklester

Well-Known Member
Licensed User
This doesn't work:

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    resp.Status = 200
    resp.SendRedirect("docs.html")
End Sub

Neither does switching those statements.

So, how do I make it serve the docs.html page WITHOUT throwing a status code of 302?

The user gets to https://mysite.com/docs, and I need to redirect (without a status code 302) to https://mysite.com/docs.html.

I guess I could put that in my nginx config file, but I shouldn't have to from what I understand about the process. I've done this with Laravel (PHP), so I know it's possible... or am I remembering wrong? :(
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Maybe I misunderstood what you are trying to do.

If you have processed user's click action, you now have 2 results, "success" or "failed". I think you can base on these 2 cases and write the HTML response with 2 different URL. I think it will give you code 200 at first run.

HTML:
<meta http-equiv="Refresh" content="0; url='https://mysite.com/docs.html'" />


B4X:
resp.ContentType = "text/html"
resp.Write($"<meta http-equiv="Refresh" content="0; url='https://mysite.com/docs.html'" />"$)

Edit: Add resp.ContentType = "text/html"
 
Last edited:
Upvote 0

cklester

Well-Known Member
Licensed User
B4X:
resp.ContentType = "text/html"
resp.Write($"<meta http-equiv="Refresh" content="0; url='https://mysite.com/docs.html'" />"$)

Yep! You got it! That works! Thank you!

@Erel , maybe we could have something like this:

B4X:
resp.SendRefresh("docs.html")

I'm using this after a person completes a registration. The user gets an email with a URL like:

B4X:
https://mysite.com/confirm?code=shfg9834hkdjfhg93845hsldkdfgh9384h987hg

They click on it, and it hits the /confirm route, but then needs to show a success.html or fail.html page, as applies.

Would it be better, instead of doing a <meta> refresh, to load the file myself and send it?

B4X:
    resp.ContentType = "text/html"
    resp.Write( File.LoadString("success.html") )
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
resp.Write(Content)
The Response.Write is very powerful and generally it can return different types of content like JSON, text, image, file and HTML.
It all depend how you set the content type.
B4X:
resp.ContentType = "CONTENT TYPE"
 
Upvote 0
Top