all repos

onasty @ 39d6b8ed38db7cc38f0cf0ea3f297ad7265f40cb

a one-time notes service

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

Olexandr Smirnov Olexandr Smirnov
ss2316544@gmail.com
web: general refactor (#158)..., 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 :
79
    { onResponse : Result Api.Error () -> msg
80
    , email : String
81
    , password : String
82
    }
83
    -> Effect msg
84
resendVerificationEmail options =
85
    let
86
        body : Encode.Value
87
        body =
88
            Encode.object
89
                [ ( "email", Encode.string options.email )
90
                , ( "password", Encode.string options.password )
91
                ]
92
    in
93
    Effect.sendApiRequest
94
        { endpoint = "/api/v1/auth/resend-verification-email"
95
        , method = "POST"
96
        , body = Http.jsonBody body
97
        , onResponse = options.onResponse
98
        , decoder = Decode.succeed ()
99
        }