all repos

onasty @ 327757c9371a333d3436f0a204677742a04236cf

a one-time notes service

onasty/web/src/Api/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
module Api.Auth exposing (refreshToken, signin)

import Data.Credentials as Credentials exposing (Credentials)
import Effect exposing (Effect)
import Http
import Json.Encode as Encode


signin :
    { onResponse : Result Http.Error Credentials -> msg
    , email : String
    , password : String
    }
    -> Effect msg
signin options =
    let
        body : Encode.Value
        body =
            Encode.object
                [ ( "email", Encode.string options.email )
                , ( "password", Encode.string options.password )
                ]
    in
    Effect.sendApiRequest
        { endpoint = "/api/v1/auth/signin"
        , method = "POST"
        , body = Http.jsonBody body
        , onResponse = options.onResponse
        , decoder = Credentials.decode
        }


refreshToken :
    { onResponse : Result Http.Error Credentials -> msg
    , refreshToken : String
    }
    -> Effect msg
refreshToken options =
    let
        body : Encode.Value
        body =
            Encode.object
                [ ( "refresh_token", Encode.string options.refreshToken ) ]
    in
    Effect.sendApiRequest
        { endpoint = "/api/v1/auth/refresh-tokens"
        , method = "POST"
        , body = Http.jsonBody body
        , onResponse = options.onResponse
        , decoder = Credentials.decode
        }