all repos

onasty @ d309c75ca6e308b5c70a6c9a27cbbfc4dd74098f

a one-time notes service

onasty/web/src/Api/Auth.elm (view raw)

Olexandr Smirnov Olexandr Smirnov
ss2316544@gmail.com
refactor: require only email for resending verification email (#165)..., 11 months ago
1
module Api.Auth exposing (refreshToken, resendVerificationEmail, signin, signup)
2
3
import Api
4
import Data.Credentials as Credentials exposing (Credentials)
5
import Effect exposing (Effect)
6
import Http
7
import Json.Decode as Decode
8
import Json.Encode as Encode
9
10
11
signin :
12
    { onResponse : Result Api.Error Credentials -> msg
13
    , email : String
14
    , password : String
15
    }
16
    -> Effect msg
17
signin options =
18
    let
19
        body : Encode.Value
20
        body =
21
            Encode.object
22
                [ ( "email", Encode.string options.email )
23
                , ( "password", Encode.string options.password )
24
                ]
25
    in
26
    Effect.sendApiRequest
27
        { endpoint = "/api/v1/auth/signin"
28
        , method = "POST"
29
        , body = Http.jsonBody body
30
        , onResponse = options.onResponse
31
        , decoder = Credentials.decode
32
        }
33
34
35
signup :
36
    { onResponse : Result Api.Error () -> msg
37
    , email : String
38
    , password : String
39
    }
40
    -> Effect msg
41
signup options =
42
    let
43
        body : Encode.Value
44
        body =
45
            Encode.object
46
                [ ( "email", Encode.string options.email )
47
                , ( "password", Encode.string options.password )
48
                ]
49
    in
50
    Effect.sendApiRequest
51
        { endpoint = "/api/v1/auth/signup"
52
        , method = "POST"
53
        , body = Http.jsonBody body
54
        , onResponse = options.onResponse
55
        , decoder = Decode.succeed ()
56
        }
57
58
59
refreshToken :
60
    { onResponse : Result Api.Error Credentials -> msg
61
    , refreshToken : String
62
    }
63
    -> Effect msg
64
refreshToken options =
65
    let
66
        body =
67
            Encode.object [ ( "refresh_token", Encode.string options.refreshToken ) ]
68
    in
69
    Effect.sendApiRequest
70
        { endpoint = "/api/v1/auth/refresh-tokens"
71
        , method = "POST"
72
        , body = Http.jsonBody body
73
        , onResponse = options.onResponse
74
        , decoder = Credentials.decode
75
        }
76
77
78
resendVerificationEmail : { onResponse : Result Api.Error () -> msg, email : String } -> Effect msg
79
resendVerificationEmail options =
80
    let
81
        body =
82
            Encode.object [ ( "email", Encode.string options.email ) ]
83
    in
84
    Effect.sendApiRequest
85
        { endpoint = "/api/v1/auth/resend-verification-email"
86
        , method = "POST"
87
        , body = Http.jsonBody body
88
        , onResponse = options.onResponse
89
        , decoder = Decode.succeed ()
90
        }