onasty/web/src/Api/Auth.elm (view raw)
Olexandr Smirnov
Olexandr Smirnov
ss2316544@gmail.com web: button component; improve code consistency (#168)..., 10 months ago
ss2316544@gmail.com web: button component; improve code consistency (#168)..., 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 = |
| 20 | Encode.object |
| 21 | [ ( "email", Encode.string options.email ) |
| 22 | , ( "password", Encode.string options.password ) |
| 23 | ] |
| 24 | in |
| 25 | Effect.sendApiRequest |
| 26 | { endpoint = "/api/v1/auth/signin" |
| 27 | , method = "POST" |
| 28 | , body = Http.jsonBody body |
| 29 | , onResponse = options.onResponse |
| 30 | , decoder = Credentials.decode |
| 31 | } |
| 32 | |
| 33 | |
| 34 | signup : |
| 35 | { onResponse : Result Api.Error () -> msg |
| 36 | , email : String |
| 37 | , password : String |
| 38 | } |
| 39 | -> Effect msg |
| 40 | signup options = |
| 41 | let |
| 42 | body = |
| 43 | Encode.object |
| 44 | [ ( "email", Encode.string options.email ) |
| 45 | , ( "password", Encode.string options.password ) |
| 46 | ] |
| 47 | in |
| 48 | Effect.sendApiRequest |
| 49 | { endpoint = "/api/v1/auth/signup" |
| 50 | , method = "POST" |
| 51 | , body = Http.jsonBody body |
| 52 | , onResponse = options.onResponse |
| 53 | , decoder = Decode.succeed () |
| 54 | } |
| 55 | |
| 56 | |
| 57 | refreshToken : { onResponse : Result Api.Error Credentials -> msg, refreshToken : String } -> Effect msg |
| 58 | refreshToken options = |
| 59 | Effect.sendApiRequest |
| 60 | { endpoint = "/api/v1/auth/refresh-tokens" |
| 61 | , method = "POST" |
| 62 | , body = Encode.object [ ( "refresh_token", Encode.string options.refreshToken ) ] |> Http.jsonBody |
| 63 | , onResponse = options.onResponse |
| 64 | , decoder = Credentials.decode |
| 65 | } |
| 66 | |
| 67 | |
| 68 | forgotPassword : { onResponse : Result Api.Error () -> msg, email : String } -> Effect msg |
| 69 | forgotPassword options = |
| 70 | Effect.sendApiRequest |
| 71 | { endpoint = "/api/v1/auth/reset-password" |
| 72 | , method = "POST" |
| 73 | , body = Encode.object [ ( "email", Encode.string options.email ) ] |> Http.jsonBody |
| 74 | , onResponse = options.onResponse |
| 75 | , decoder = Decode.succeed () |
| 76 | } |
| 77 | |
| 78 | |
| 79 | resetPassword : { onResponse : Result Api.Error () -> msg, token : String, password : String } -> Effect msg |
| 80 | resetPassword options = |
| 81 | Effect.sendApiRequest |
| 82 | { endpoint = "/api/v1/auth/reset-password/" ++ options.token |
| 83 | , method = "POST" |
| 84 | , body = Encode.object [ ( "password", Encode.string options.password ) ] |> Http.jsonBody |
| 85 | , onResponse = options.onResponse |
| 86 | , decoder = Decode.succeed () |
| 87 | } |
| 88 | |
| 89 | |
| 90 | resendVerificationEmail : { onResponse : Result Api.Error () -> msg, email : String } -> Effect msg |
| 91 | resendVerificationEmail options = |
| 92 | Effect.sendApiRequest |
| 93 | { endpoint = "/api/v1/auth/resend-verification-email" |
| 94 | , method = "POST" |
| 95 | , body = Encode.object [ ( "email", Encode.string options.email ) ] |> Http.jsonBody |
| 96 | , onResponse = options.onResponse |
| 97 | , decoder = Decode.succeed () |
| 98 | } |