0

Basically, a login process works as follows:

  • the user enters his informations in the login form and submits them,
  • the server (ruby, php, nodejs, whatever) analyses these infos and,
    • a) redirects to the logged in page,
    • b) redirects to the loggin page with an error message (which can be handled via the url or session…),

If the process is to be done in ajax, the server can't redirect (plus it's not the desired behaviour).

Therefore what is to way to go? The server sends a message (like 'logged' or 'wrongpass') and the JavaScript handles the logic accordingly?

I couldn't find any info on the subjet.

2
  • If it redirects, what's the point of using ajax for the login ? Commented Apr 22, 2015 at 14:18
  • It doesn't redirect. Even if it would, I find it more seemless to redirect only if the loggin succeeds and to display an error message without to have to reload the page if it doesn't. Moreover, in the case of a login through an API it's json request only. Commented Apr 22, 2015 at 14:25

3 Answers 3

1

You can send to your client a json response with something like:

if ok:

{
   "status": "ok"
}

set session, cookies....

or if error:

{
   "status": "error",
   "message": "username not found or wathever"
}

and then with javascript do all you want acording to what you received from server.

Ex: redirect to loggedin page, show the message error...

Sign up to request clarification or add additional context in comments.

Comments

1

In most cases, the idea is to give the client a proof that he's authenticated (like a token for instance). The client will then be able to include it in further requests to be allowed to use 'reserved' operations...

I can develop if you need more info to dig this up.

Comments

0

Typically the server responds with a "success" or "failed" message. The JavaScript application should then handle this message accordingly.

It depends on your application if a redirect is needed after login, but if it's needed, the JavaScript should trigger it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.