summaryrefslogtreecommitdiff
path: root/elm/User
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-10-01 14:53:13 +0200
committerYorhel <git@yorhel.nl>2019-10-01 14:53:21 +0200
commit9f5d85706c04bda063dfb2a7c47bfaa8fca26eb4 (patch)
treefed6047fc225194eb62a59f828ff9fa7f277299b /elm/User
parent0ee5f7e30f9ba10fdd870117d410c80f3e0a6c4a (diff)
v2rw: Convert registration, account activation and password reset
This is largely copy-paste from v3.
Diffstat (limited to 'elm/User')
-rw-r--r--elm/User/Login.elm2
-rw-r--r--elm/User/PassReset.elm89
-rw-r--r--elm/User/PassSet.elm94
-rw-r--r--elm/User/Register.elm116
4 files changed, 300 insertions, 1 deletions
diff --git a/elm/User/Login.elm b/elm/User/Login.elm
index 6a35f690..0adaef4f 100644
--- a/elm/User/Login.elm
+++ b/elm/User/Login.elm
@@ -72,7 +72,7 @@ type Msg
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
- Username n -> ({ model | username = n }, Cmd.none)
+ Username n -> ({ model | username = String.toLower n }, Cmd.none)
Password n -> ({ model | password = n }, Cmd.none)
Newpass1 n -> ({ model | newpass1 = n, noteq = False }, Cmd.none)
Newpass2 n -> ({ model | newpass2 = n, noteq = False }, Cmd.none)
diff --git a/elm/User/PassReset.elm b/elm/User/PassReset.elm
new file mode 100644
index 00000000..f1c36058
--- /dev/null
+++ b/elm/User/PassReset.elm
@@ -0,0 +1,89 @@
+module User.PassReset exposing (main)
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+import Json.Encode as JE
+import Browser
+import Lib.Api as Api
+import Gen.Api as GApi
+import Gen.RegReset as GRR
+import Lib.Html exposing (..)
+
+
+main : Program () Model Msg
+main = Browser.element
+ { init = always (init, Cmd.none)
+ , subscriptions = always Sub.none
+ , view = view
+ , update = update
+ }
+
+
+type alias Model =
+ { email : String
+ , state : Api.State
+ , success : Bool
+ }
+
+
+init : Model
+init =
+ { email = ""
+ , state = Api.Normal
+ , success = False
+ }
+
+
+encodeForm : Model -> JE.Value
+encodeForm o = JE.object
+ [ ("email", JE.string o.email) ]
+
+
+type Msg
+ = EMail String
+ | Submit
+ | Submitted GApi.Response
+
+
+update : Msg -> Model -> (Model, Cmd Msg)
+update msg model =
+ case msg of
+ EMail n -> ({ model | email = n }, Cmd.none)
+
+ Submit -> ( { model | state = Api.Loading }
+ , Api.post "/u/newpass" (encodeForm model) Submitted )
+
+ Submitted GApi.Success -> ({ model | success = True }, Cmd.none)
+ Submitted e -> ({ model | state = Api.Error e }, Cmd.none)
+
+
+view : Model -> Html Msg
+view model =
+ if model.success
+ then
+ div [ class "mainbox" ]
+ [ h1 [] [ text "New password" ]
+ , div [ class "notice" ]
+ [ p [] [ text "Your password has been reset and instructions to set a new one should reach your mailbox in a few minutes." ] ]
+ ]
+ else
+ Html.form [ onSubmit Submit ]
+ [ div [ class "mainbox" ]
+ [ h1 [] [ text "Forgot Password" ]
+ , p []
+ [ text "Forgot your password and can't login to VNDB anymore? "
+ , text "Don't worry! Just give us the email address you used to register on VNDB "
+ , text " and we'll send you instructions to set a new password within a few minutes!"
+ ]
+ , table [ class "formtable" ]
+ [ tr [ class "newfield" ]
+ [ td [ class "label" ] [ label [ for "email" ] [ text "E-Mail" ]]
+ , td [ class "field" ] [ inputText "email" model.email EMail GRR.valEmail ]
+ ]
+ ]
+ ]
+ , div [ class "mainbox" ]
+ [ fieldset [ class "submit" ] [ submitButton "Submit" model.state True False ]
+ ]
+ ]
diff --git a/elm/User/PassSet.elm b/elm/User/PassSet.elm
new file mode 100644
index 00000000..0756196d
--- /dev/null
+++ b/elm/User/PassSet.elm
@@ -0,0 +1,94 @@
+module User.PassSet exposing (main)
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+import Json.Encode as JE
+import Browser
+import Browser.Navigation exposing (load)
+import Lib.Api as Api
+import Gen.Api as GApi
+import Gen.RegReset as GRR
+import Lib.Html exposing (..)
+
+
+main : Program String Model Msg
+main = Browser.element
+ { init = \url -> (init url, Cmd.none)
+ , subscriptions = always Sub.none
+ , view = view
+ , update = update
+ }
+
+
+type alias Model =
+ { url : String
+ , newpass1 : String
+ , newpass2 : String
+ , state : Api.State
+ , noteq : Bool
+ }
+
+
+init : String -> Model
+init url =
+ { url = url
+ , newpass1 = ""
+ , newpass2 = ""
+ , state = Api.Normal
+ , noteq = False
+ }
+
+
+encodeForm : Model -> JE.Value
+encodeForm o = JE.object
+ [ ("password", JE.string o.newpass1) ]
+
+
+type Msg
+ = Newpass1 String
+ | Newpass2 String
+ | Submit
+ | Submitted GApi.Response
+
+
+update : Msg -> Model -> (Model, Cmd Msg)
+update msg model =
+ case msg of
+ Newpass1 n -> ({ model | newpass1 = n, noteq = False }, Cmd.none)
+ Newpass2 n -> ({ model | newpass2 = n, noteq = False }, Cmd.none)
+
+ Submit ->
+ if model.newpass1 /= model.newpass2
+ then ( { model | noteq = True }, Cmd.none)
+ else ( { model | state = Api.Loading }
+ , Api.post model.url (encodeForm model) Submitted )
+
+ Submitted GApi.Success -> (model, load "/")
+ Submitted e -> ({ model | state = Api.Error e }, Cmd.none)
+
+
+view : Model -> Html Msg
+view model =
+ Html.form [ onSubmit Submit ]
+ [ div [ class "mainbox" ]
+ [ h1 [] [ text "Set your password" ]
+ , p [] [ text "Now you can set a password for your account. You will be logged in automatically after your password has been saved." ]
+ , table [ class "formtable" ]
+ [ tr [ class "newfield" ]
+ [ td [ class "label" ] [ label [ for "newpass1" ] [ text "New password" ]]
+ , td [ class "field" ] [ inputPassword "newpass1" model.newpass1 Newpass1 GRR.valPassword ]
+ ]
+ , tr [ class "newfield" ]
+ [ td [ class "label" ] [ label [ for "newpass2" ] [ text "Repeat" ]]
+ , td [ class "field" ]
+ [ inputPassword "newpass2" model.newpass2 Newpass2 GRR.valPassword
+ , if model.noteq then b [ class "standout" ] [ text "Passwords do not match" ] else text ""
+ ]
+ ]
+ ]
+ ]
+ , div [ class "mainbox" ]
+ [ fieldset [ class "submit" ] [ submitButton "Submit" model.state True False ]
+ ]
+ ]
diff --git a/elm/User/Register.elm b/elm/User/Register.elm
new file mode 100644
index 00000000..6e65893f
--- /dev/null
+++ b/elm/User/Register.elm
@@ -0,0 +1,116 @@
+module User.Register exposing (main)
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+import Json.Encode as JE
+import Browser
+import Lib.Api as Api
+import Gen.Api as GApi
+import Gen.RegReset as GRR
+import Lib.Html exposing (..)
+
+
+main : Program () Model Msg
+main = Browser.element
+ { init = always (init, Cmd.none)
+ , subscriptions = always Sub.none
+ , view = view
+ , update = update
+ }
+
+
+type alias Model =
+ { username : String
+ , email : String
+ , vns : Int
+ , state : Api.State
+ , success : Bool
+ }
+
+
+init : Model
+init =
+ { username = ""
+ , email = ""
+ , vns = 0
+ , state = Api.Normal
+ , success = False
+ }
+
+
+encodeForm : Model -> JE.Value
+encodeForm o = JE.object
+ [ ("username", JE.string o.username)
+ , ("email", JE.string o.email)
+ , ("vns", JE.int o.vns) ]
+
+
+type Msg
+ = Username String
+ | EMail String
+ | VNs String
+ | Submit
+ | Submitted GApi.Response
+
+
+update : Msg -> Model -> (Model, Cmd Msg)
+update msg model =
+ case msg of
+ Username n -> ({ model | username = String.toLower n }, Cmd.none)
+ EMail n -> ({ model | email = n }, Cmd.none)
+ VNs n -> ({ model | vns = Maybe.withDefault model.vns (String.toInt n) }, Cmd.none)
+
+ Submit -> ( { model | state = Api.Loading }
+ , Api.post "/u/register" (encodeForm model) Submitted )
+
+ Submitted GApi.Success -> ({ model | success = True }, Cmd.none)
+ Submitted e -> ({ model | state = Api.Error e }, Cmd.none)
+
+
+view : Model -> Html Msg
+view model =
+ if model.success
+ then
+ div [ class "mainbox" ]
+ [ h1 [] [ text "Account created" ]
+ , div [ class "notice" ]
+ [ p [] [ text "Your account has been created! In a few minutes, you should receive an email with instructions to set your password." ] ]
+ ]
+ else
+ Html.form [ onSubmit Submit ]
+ [ div [ class "mainbox" ]
+ [ h1 [] [ text "Create an account" ]
+ , table [ class "formtable" ]
+ [ tr [ class "newfield" ]
+ [ td [ class "label" ] [ label [ for "username" ] [ text "Username" ]]
+ , td [ class "field" ] [ inputText "username" model.username Username GRR.valUsername ]
+ ]
+ , tr []
+ [ td [] []
+ , td [ class "field" ] [ text "Preferred username. Must be lowercase and can only consist of alphanumeric characters." ]
+ ]
+ , tr [ class "newfield" ]
+ [ td [ class "label" ] [ label [ for "email" ] [ text "E-Mail" ]]
+ , td [ class "field" ] [ inputText "email" model.email EMail GRR.valEmail ]
+ ]
+ , tr []
+ [ td [] []
+ , td [ class "field" ]
+ [ text "Your email address will only be used in case you lose your password. "
+ , text "We will never send spam or newsletters unless you explicitly ask us for it or we get hacked."
+ , br [] []
+ , br [] []
+ , text "Anti-bot question: How many visual novels do we have in the database? (Hint: look to your left)"
+ ]
+ ]
+ , tr [ class "newfield" ]
+ [ td [ class "label" ] [ label [ for "vns" ] [ text "Answer" ]]
+ , td [ class "field" ] [ inputText "vns" (if model.vns == 0 then "" else String.fromInt model.vns) VNs GRR.valVns ]
+ ]
+ ]
+ ]
+ , div [ class "mainbox" ]
+ [ fieldset [ class "submit" ] [ submitButton "Submit" model.state True False ]
+ ]
+ ]