all repos

onasty @ c3cd6686a990dcd8870c4f1db6073bb495b66842

a one-time notes service

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

Olexandr Smirnov Olexandr Smirnov
ss2316544@gmail.com
web: forgot password (#169)..., 10 months ago
1
module Api.Auth exposing (forgotPassword, refreshToken, resendVerificationEmail, resetPassword, 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
forgotPassword : { onResponse : Result Api.Error () -> msg, email : String } -> Effect msg
79
forgotPassword options =
80
    Effect.sendApiRequest
81
        { endpoint = "/api/v1/auth/reset-password"
82
        , method = "POST"
83
        , body = Encode.object [ ( "email", Encode.string options.email ) ] |> Http.jsonBody
84
        , onResponse = options.onResponse
85
        , decoder = Decode.succeed ()
86
        }
87
88
89
resetPassword : { onResponse : Result Api.Error () -> msg, token : String, password : String } -> Effect msg
90
resetPassword options =
91
    Effect.sendApiRequest
92
        { endpoint = "/api/v1/auth/reset-password/" ++ options.token
93
        , method = "POST"
94
        , body = Encode.object [ ( "password", Encode.string options.password ) ] |> Http.jsonBody
95
        , onResponse = options.onResponse
96
        , decoder = Decode.succeed ()
97
        }
98
99
100
resendVerificationEmail : { onResponse : Result Api.Error () -> msg, email : String } -> Effect msg
101
resendVerificationEmail options =
102
    let
103
        body =
104
            Encode.object [ ( "email", Encode.string options.email ) ]
105
    in
106
    Effect.sendApiRequest
107
        { endpoint = "/api/v1/auth/resend-verification-email"
108
        , method = "POST"
109
        , body = Http.jsonBody body
110
        , onResponse = options.onResponse
111
        , decoder = Decode.succeed ()
112
        }