auto login after register

This commit is contained in:
Swrup 2022-03-08 23:05:38 +01:00
parent 53617f1077
commit 47e5c95b86
2 changed files with 28 additions and 31 deletions

View file

@ -46,8 +46,16 @@ let register_get request = render_unsafe (Register.f request) request
let register_post request = let register_post request =
match%lwt Dream.form request with match%lwt Dream.form request with
| `Ok [ ("email", email); ("nick", nick); ("password", password) ] -> | `Ok [ ("email", email); ("nick", nick); ("password", password) ] -> (
render_unsafe (Register.f ~nick ~email ~password request) request match User.register ~email ~nick ~password with
| Error e -> render_unsafe e request
| Ok () ->
let res =
Result.fold ~error:Fun.id
~ok:(fun _ -> "User created ! Welcome !")
(User.login ~login:nick ~password request)
in
render_unsafe res request )
| `Ok _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _ | `Ok _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
| `Wrong_session _ | `Expired _ | `Wrong_content_type -> | `Wrong_session _ | `Expired _ | `Wrong_content_type ->
Dream.empty `Bad_Request Dream.empty `Bad_Request

View file

@ -1,29 +1,18 @@
let f ?email ?nick ?password request = let f request =
<%s! Dream.form_tag ~action:"/register" request %>
% begin match email, nick, password with <div class="mb-3">
% | Some email, Some nick, Some password -> <label for="nick" class="form-label">Nick</label>
% begin match User.register ~email ~nick ~password with <input name="nick" type="text" class="form-control" id="nick" aria-describedby="nick-help">
% | Error e -> <div id="nick-help" class="form-text">Choose a nickname</div>
Error: <%s e %> </div>
% | Ok _ (* TODO: do not match on _ *) -> <div class="mb-3">
User created ! <label for="email" class="form-label">Email address</label>
% end; <input name="email" type="email" class="form-control" id="email" aria-describedby="email-help">
% | _ -> <div id="email-help" class="form-text">We'll never share your email with anyone else.</div>
<%s! Dream.form_tag ~action:"/register" request %> </div>
<div class="mb-3"> <div class="mb-3">
<label for="nick" class="form-label">Nick</label> <label for="password" class="form-label">Password</label>
<input name="nick" type="text" class="form-control" id="nick" aria-describedby="nickHelp"> <input name="password" type="password" class="form-control" id="password">
<div id="nickHelp" class="form-text">Who are u ?</div> </div>
</div> <button type="submit" class="btn btn-primary">Submit</button>
<div class="mb-3"> </form>
<label for="email" class="form-label">Email address</label>
<input name="email" type="email" class="form-control" id="email" aria-describedby="emailHelp">
<div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input name="password" type="password" class="form-control" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
% end;