title: User registration/authentication flow on a REST API
body: <p>I know this is not the first time the topic is treated in StackOverflow, however, I have some questions I couldn't find an answer to or other questions have opposed answers.</p>
  <p>I am doing a rather simple REST API (Silex-PHP) to be consumed initially by just one SPA (backbone app). I don't want to comment all the several authentication methods in this question as that topic is already fully covered on SO. I'll basically create a token for each user, and this token will be attached in every request that requires authentication by the SPA. All the SPA-Server transactions will run under HTTPS. For now, my decision is that the token doesn't expire. Tokens that expire/tokens per session are not complying with the statelessness of REST, right? I understand there's a lot of room for security improvement but that's my scope for now.</p>
  <p>I have a model for Tokens, and thus a table in the database for tokens with a FK to user_id. By this I mean the token is not part of my user model.</p>
  <h2>REGISTER</h2>
  <p>I have a POST /users (requires no authentication) that creates a user in the database and returns the new user. This complies with the one request one resource rule. However, this brings me certain doubts:</p>
  <ul>
  <li><p>My idea is that at the time to create a new user, create a new token for the user, to immediately return it with the Response, and thus, improving the UX. The user will immediately be able to start using the web app. However, returning the token for such response would break the rule of returning just the resource. Should I instead make two requests together? One to create the user and one to retrieve the token without the user needing to reenter credentials?</p>
  </li>
  <li><p>If I decided to return the token together with the user, then I believe POST /users would be confusing for the API consumer, and then something like POST /auth/register appears. Once more, I dislike this idea because involves a verb. I really like the simplicity offered in <a href="https://stackoverflow.com/a/25953066/2129043">this answer</a>. But then again, I'd need to do two requests together, a POST /users and a POST /tokens. How wrong is it to do two requests together and also, how would I exactly send the relevant information for the token to be attached to a certain user if both requests are sent together?</p>
  </li>
  </ul>
  <p>For now my flow works like follows:</p>
  <pre><code>1. Register form makes a POST /users request
  2. Server creates a new user and a new token, returns both in the response (break REST rule)
  3. Client now attaches token to every Request that needs Authorization
  </code></pre>
  <p>The token never expires, preserving REST statelessness.</p>
  <h2>EMAIL VALIDATION</h2>
  <p>Most of the current webapps require email validation without breaking the UX for the users, i.e the users can immediately use the webapp after registering. On the other side, if I return the token with the register request as suggested above, users will immediately have access to every resource without validating emails.</p>
  <p>Normally I'd go for the following workflow:</p>
  <pre><code>1. Register form sends POST /users request.
  2. Server creates a new user with validated_email set to false and stores an email_validation_token. Additionally, the server sends an email generating an URL that contains the email_validation_token. 
  3. The user clicks on the URL that makes a request: For example POST /users/email_validation/{email_validation_token}
  4. Server validates email, sets validated_email to true, generates a token and returns it in the response, redirecting the user to his home page at the same time.
  </code></pre>
  <p>This looks overcomplicated and totally ruins the UX. How'd you go about it?</p>
  <h2>LOGIN</h2>
  <p>This is quite simple, for now I am doing it this way so please correct me if wrong:</p>
  <pre><code>1. User fills a log in form which makes a request to POST /login sending Basic Auth credentials.
  2. Server checks Basic Auth credentials and returns token for the given user.
  3. Web app attached the given token to every future request.
  </code></pre>
  <p>login is a verb and thus breaks a REST rule, everyone seems to agree on doing it this way though.</p>
  <h2>LOGOUT</h2>
  <p>Why does everyone seem to need a /auth/logout endpoint? From my point of view clicking on &quot;logout&quot; in the web app should basically remove the token from the application and not send it in further requests. The server plays no role in this.</p>
  <p>As it is possible that the token is kept in localStorage to prevent losing the token on a possible page refresh, logout would also imply removing the token from the localStorage. But still, this doesn't affect the server. I understand people who need to have a POST /logout are basically working with session tokens, which again break the statelessness of REST.</p>
  <h2>REMEMBER ME</h2>
  <p>I understand the remember me basically refers to saving the returned token to the localStorage or not in my case. Is this right?</p>
  <p>If you'd recommend any further reading on this topic I'd very much appreciate it. Thanks!</p>