summaryrefslogtreecommitdiff
path: root/src/Main/Auth.purs
blob: 04fe6e136a05c5c79c8f7290b7552e45b372a893 (plain)
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
module Main.Auth where

import Prelude

import Auth (getQueryRedirect, getResponseRedirect)
import Data.HTTP.Method (Method(..))
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Effect.Console (log)
import Fetch (fetch)
import UI as UI
import Utils (byId, getValue, listen, removeClass)
import Web.Event.Event as Ev

main :: Effect Unit
main = launchAff_ do
  liftEffect $ log "hello from auth"
  container <- byId "lcolonq-auth-login"
  removeClass "lcolonq-invisible" container
  form <- byId "lcolonq-auth-form"
  listen form "submit" \ev -> launchAff_ do
    liftEffect $ Ev.preventDefault ev
    usernameInp <- byId "lcolonq-auth-username"
    passwordInp <- byId "lcolonq-auth-password"
    username <- getValue usernameInp
    password <- getValue passwordInp
    rd <- getQueryRedirect
    { json: resp } <- fetch "/api/firstfactor"
      { method: POST
      , headers: { "Content-Type": "application/json" }
      , body: UI.toJSON
        { username
        , password
        , targetURL: case rd of
            Just r -> r
            Nothing -> "https://secure.colonq.computer"
        }
      }
    res <- resp
    getResponseRedirect res >>= case _ of
      Nothing -> do
        err <- byId "lcolonq-auth-error"
        removeClass "lcolonq-invisible" err
      Just r -> UI.redirect r