onasty/web/src/Api/Auth.elm (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com web: handle api errors (#138)..., 11 months ago
ss2316544@gmail.com web: handle api errors (#138)..., 11 months ago
| 1 | module Api.Auth exposing (refreshToken, 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 | } |