onasty/web/src/Pages/Auth.elm(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
module Pages.Auth exposing (Banner, FormVariant, Model, Msg, page)
import Api
import Api.Auth
import Auth.User
import Components.Box
import Components.Form
import Components.Utils
import Data.Credentials exposing (Credentials)
import Dict
import Effect exposing (Effect)
import Html as H exposing (Html)
import Html.Attributes as A
import Html.Events as E
import Layouts
import Page exposing (Page)
import Route exposing (Route)
import Route.Path
import Shared
import Time exposing (Posix)
import View exposing (View)
page : Shared.Model -> Route () -> Page Model Msg
page shared route =
Page.new
{ init = init shared route
, update = update
, subscriptions = subscriptions
, view = view
}
|> Page.withLayout (\_ -> Layouts.Header {})
-- INIT
type alias Model =
{ email : String
, password : String
, passwordAgain : String
, isSubmittingForm : Bool
, banner : Banner
, formVariant : FormVariant
, lastClicked : Maybe Posix
, now : Maybe Posix
}
init : Shared.Model -> Route () -> () -> ( Model, Effect Msg )
init shared route () =
let
formVariant =
case Dict.get "token" route.query of
Just token ->
SetNewPassword token
Nothing ->
SignIn
in
( { formVariant = formVariant
, isSubmittingForm = False
, email = ""
, password = ""
, passwordAgain = ""
, lastClicked = Nothing
, banner = Hidden
, now = Nothing
}
, case shared.user of
Auth.User.SignedIn _ ->
Effect.pushRoutePath Route.Path.Home_
_ ->
Effect.none
)
-- UPDATE
type Msg
= Tick Posix
| UserUpdatedInput Field String
| UserChangedFormVariant FormVariant
| UserClickedSubmit
| UserClickedResendActivationEmail
| ApiSignInResponded (Result Api.Error Credentials)
| ApiSignUpResponded (Result Api.Error ())
| ApiForgotPasswordResponded (Result Api.Error ())
| ApiSetNewPasswordResponded (Result Api.Error ())
| ApiResendVerificationEmail (Result Api.Error ())
type Field
= Email
| Password
| PasswordAgain
type alias ResetPasswordToken =
String
type FormVariant
= SignIn
| SignUp
| ForgotPassword
| SetNewPassword ResetPasswordToken
type Banner
= Hidden
| ResendVerificationEmail
| Error Api.Error
| CheckEmail
update : Msg -> Model -> ( Model, Effect Msg )
update msg model =
case msg of
Tick now ->
( { model | now = Just now }, Effect.none )
UserClickedSubmit ->
( { model | isSubmittingForm = True }
, case model.formVariant of
SignIn ->
Api.Auth.signin
{ onResponse = ApiSignInResponded
, email = model.email
, password = model.password
}
SignUp ->
Api.Auth.signup
{ onResponse = ApiSignUpResponded
, email = model.email
, password = model.password
}
ForgotPassword ->
Api.Auth.forgotPassword { onResponse = ApiForgotPasswordResponded, email = model.email }
SetNewPassword token ->
Api.Auth.resetPassword { onResponse = ApiSetNewPasswordResponded, token = token, password = model.password }
)
UserClickedResendActivationEmail ->
( { model | lastClicked = model.now }
, Api.Auth.resendVerificationEmail
{ onResponse = ApiResendVerificationEmail
, email = model.email
}
)
UserChangedFormVariant variant ->
( { model | formVariant = variant }, Effect.none )
UserUpdatedInput Email email ->
( { model | email = email }, Effect.none )
UserUpdatedInput Password password ->
( { model | password = password }, Effect.none )
UserUpdatedInput PasswordAgain passwordAgain ->
( { model | passwordAgain = passwordAgain }, Effect.none )
ApiSignInResponded (Ok credentials) ->
( { model | isSubmittingForm = False }, Effect.signin credentials )
ApiSignInResponded (Err err) ->
if Api.isNotVerified err then
( { model | isSubmittingForm = False, banner = ResendVerificationEmail }, Effect.none )
else
( { model | isSubmittingForm = False, banner = Error err }, Effect.none )
ApiSignUpResponded (Ok ()) ->
( { model | isSubmittingForm = False, banner = ResendVerificationEmail }, Effect.none )
ApiSignUpResponded (Err err) ->
( { model | isSubmittingForm = False, banner = Error err }, Effect.none )
ApiResendVerificationEmail (Ok ()) ->
( model, Effect.none )
ApiResendVerificationEmail (Err err) ->
( { model | banner = Error err }, Effect.none )
ApiSetNewPasswordResponded (Ok ()) ->
( { model | isSubmittingForm = False, formVariant = SignIn, password = "", passwordAgain = "" }, Effect.replaceRoutePath Route.Path.Auth )
ApiSetNewPasswordResponded (Err err) ->
( { model | isSubmittingForm = False, banner = Error err }, Effect.none )
ApiForgotPasswordResponded (Ok ()) ->
( { model | isSubmittingForm = False, banner = CheckEmail }, Effect.none )
ApiForgotPasswordResponded (Err err) ->
( { model | isSubmittingForm = False, banner = Error err }, Effect.none )
subscriptions : Model -> Sub Msg
subscriptions model =
if model.banner == ResendVerificationEmail then
Time.every 1000 Tick
else
Sub.none
-- VIEW
view : Model -> View Msg
view model =
{ title = "Authentication"
, body =
[ H.div [ A.class "min-h-screen flex items-center justify-center bg-gray-50 p-4" ]
[ H.div [ A.class "w-full max-w-md bg-white rounded-lg border border-gray-200 shadow-sm" ]
-- TODO: add oauth buttons
[ viewBanner model
, viewBoxHeader model.formVariant
, H.div [ A.class "px-6 pb-6 space-y-4" ]
[ viewChangeVariant model.formVariant
, H.div [ A.class "border-t border-gray-200" ] []
, viewForm model
]
]
]
]
}
viewBanner : Model -> Html Msg
viewBanner model =
case model.banner of
Hidden ->
H.text ""
Error err ->
Components.Box.error (Api.errorMessage err)
CheckEmail ->
Components.Box.success { header = "Check your email!", body = "To continue with resetting your password please check the email we've sent." }
ResendVerificationEmail ->
viewVerificationBanner model.now model.lastClicked
viewVerificationBanner : Maybe Posix -> Maybe Posix -> Html Msg
viewVerificationBanner now lastClicked =
let
timeLeftSeconds =
case ( now, lastClicked ) of
( Just now_, Just last ) ->
let
elapsedMs =
Time.posixToMillis now_ - Time.posixToMillis last
in
max 0 ((30 * 1000 - elapsedMs) // 1000)
_ ->
0
canClick : Bool
canClick =
timeLeftSeconds == 0
in
Components.Box.successBox
[ H.div [ A.class "font-medium text-green-800 mb-2" ] [ H.text "Check your email!" ]
, H.p [ A.class "text-green-800 text-sm" ] [ H.text "Please verify your account to continue. We've sent a verification link to your email — click it to activate your account." ]
, Components.Form.button
{ text = "Resend verification email"
, onClick = UserClickedResendActivationEmail
, disabled = not canClick
, style = Components.Form.SecondaryDisabled canClick
}
, Components.Utils.viewIf (not canClick)
(H.p [ A.class "text-gray-600 text-xs mt-2" ]
[ H.text ("You can request a new verification email in " ++ String.fromInt timeLeftSeconds ++ " seconds.") ]
)
]
viewBoxHeader : FormVariant -> Html Msg
viewBoxHeader variant =
let
( title, description ) =
case variant of
SignIn ->
( "Welcome Back", "Enter your credentials to access your account" )
SignUp ->
( "Create Account", "Enter your information to create your account" )
ForgotPassword ->
( "Forgot Password", "Enter your email to reset your password" )
SetNewPassword _ ->
( "Set New Password", "Enter your new password to reset your account" )
in
H.div [ A.class "p-6 pb-4" ]
[ H.h1 [ A.class "text-2xl font-bold text-center mb-2" ] [ H.text title ]
, H.p [ A.class "text-center text-gray-600 text-sm" ] [ H.text description ]
]
viewChangeVariant : FormVariant -> Html Msg
viewChangeVariant variant =
H.div [ A.class "flex [&>*]:flex-1 gap-2" ]
[ Components.Form.button
{ text = "Sign In"
, onClick = UserChangedFormVariant SignIn
, style = Components.Form.Primary (variant == SignIn)
, disabled = variant == SignIn
}
, Components.Form.button
{ text = "Sign Up"
, disabled = variant == SignUp
, style = Components.Form.Primary (variant == SignUp)
, onClick = UserChangedFormVariant SignUp
}
]
viewForm : Model -> Html Msg
viewForm model =
H.form
[ A.class "space-y-4"
, E.onSubmit UserClickedSubmit
]
(case model.formVariant of
SignIn ->
[ viewFormInput { field = Email, value = model.email, error = validateEmail model.email }
, viewFormInput { field = Password, value = model.password, error = validatePassword model.password }
, viewForgotPassword
, viewSubmitButton model
]
SignUp ->
[ viewFormInput { field = Email, value = model.email, error = validateEmail model.email }
, viewFormInput { field = Password, value = model.password, error = validatePassword model.password }
, viewFormInput { field = PasswordAgain, value = model.passwordAgain, error = validatePasswords model.password model.passwordAgain }
, viewSubmitButton model
]
ForgotPassword ->
[ viewFormInput { field = Email, value = model.email, error = validateEmail model.email }
, viewSubmitButton model
]
SetNewPassword _ ->
[ viewFormInput { field = Password, value = model.password, error = validatePassword model.password }
, viewFormInput { field = PasswordAgain, value = model.passwordAgain, error = validatePasswords model.password model.passwordAgain }
, viewSubmitButton model
]
)
viewFormInput : { field : Field, value : String, error : Maybe String } -> Html Msg
viewFormInput opts =
Components.Form.input
{ style = Components.Form.Simple
, id = (fromFieldToFieldInfo opts.field).label
, error = opts.error
, label = (fromFieldToFieldInfo opts.field).label
, type_ = (fromFieldToFieldInfo opts.field).type_
, placeholder = (fromFieldToFieldInfo opts.field).label
, onInput = UserUpdatedInput opts.field
, field = opts.field
, value = opts.value
, required = True
}
viewForgotPassword : Html Msg
viewForgotPassword =
H.div [ A.class "text-right" ]
[ H.button
[ A.class "text-sm text-black hover:underline focus:outline-none"
, A.type_ "button"
, E.onClick (UserChangedFormVariant ForgotPassword)
]
[ H.text "Forgot password?" ]
]
viewSubmitButton : Model -> Html Msg
viewSubmitButton model =
Components.Form.submitButton
{ class = "w-full"
, text = fromVariantToLabel model.formVariant
, style = Components.Form.Primary (isFormDisabled model)
, disabled = isFormDisabled model
}
isFormDisabled : Model -> Bool
isFormDisabled model =
case model.formVariant of
SignIn ->
model.isSubmittingForm
|| (validateEmail model.email /= Nothing)
|| (validatePassword model.password /= Nothing)
SignUp ->
model.isSubmittingForm
|| (validateEmail model.email /= Nothing)
|| (validatePassword model.password /= Nothing)
|| (validatePasswords model.password model.passwordAgain /= Nothing)
ForgotPassword ->
model.isSubmittingForm || (validateEmail model.email /= Nothing)
SetNewPassword _ ->
model.isSubmittingForm
|| (validateEmail model.email /= Nothing)
|| (validatePassword model.password /= Nothing)
|| (validatePasswords model.password model.passwordAgain /= Nothing)
validateEmail : String -> Maybe String
validateEmail email =
if
not (String.isEmpty email)
&& (not (String.contains "@" email) || not (String.contains "." email))
then
Just "Please enter a valid email address."
else
Nothing
validatePassword : String -> Maybe String
validatePassword passwd =
if not (String.isEmpty passwd) && String.length passwd < 8 then
Just "Password must be at least 8 characters long."
else
Nothing
validatePasswords : String -> String -> Maybe String
validatePasswords passowrd1 password2 =
if not (String.isEmpty passowrd1) && passowrd1 /= password2 then
Just "Passwords do not match."
else
Nothing
fromVariantToLabel : FormVariant -> String
fromVariantToLabel variant =
case variant of
SignIn ->
"Sign In"
SignUp ->
"Sign Up"
ForgotPassword ->
"Forgot Password"
SetNewPassword _ ->
"Set new password"
fromFieldToFieldInfo : Field -> { label : String, type_ : String }
fromFieldToFieldInfo field =
case field of
Email ->
{ label = "Email address", type_ = "email" }
Password ->
{ label = "Password", type_ = "password" }
PasswordAgain ->
{ label = "Confirm password", type_ = "password" }
|