all repos

onasty @ 7dc6a9689bae42247cc787cb0c89bc60942077b6

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
web: prompt user to check email to verifiy after signing up (#139)..., 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 : Encode.Value
67
        body =
68
            Encode.object
69
                [ ( "refresh_token", Encode.string options.refreshToken ) ]
70
    in
71
    Effect.sendApiRequest
72
        { endpoint = "/api/v1/auth/refresh-tokens"
73
        , method = "POST"
74
        , body = Http.jsonBody body
75
        , onResponse = options.onResponse
76
        , decoder = Credentials.decode
77
        }
78
79
80
resendVerificationEmail :
81
    { onResponse : Result Api.Error () -> msg
82
    , email : String
83
    , password : String
84
    }
85
    -> Effect msg
86
resendVerificationEmail options =
87
    let
88
        body : Encode.Value
89
        body =
90
            Encode.object
91
                [ ( "email", Encode.string options.email )
92
                , ( "password", Encode.string options.password )
93
                ]
94
    in
95
    Effect.sendApiRequest
96
        { endpoint = "/api/v1/auth/resend-verification-email"
97
        , method = "POST"
98
        , body = Http.jsonBody body
99
        , onResponse = options.onResponse
100
        , decoder = Decode.succeed ()
101
        }