onasty/web/src/Pages/Oauth/Callback.elm (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com feat(web): add oauth login (#213)..., 8 months ago
olexsmir@gmail.com feat(web): add oauth login (#213)..., 8 months ago
| 1 | module Pages.Oauth.Callback exposing (Model, Msg, page) |
| 2 | |
| 3 | import Components.Box |
| 4 | import Components.Utils |
| 5 | import Dict exposing (Dict) |
| 6 | import Effect exposing (Effect) |
| 7 | import Layouts |
| 8 | import Page exposing (Page) |
| 9 | import Route exposing (Route) |
| 10 | import Shared |
| 11 | import View exposing (View) |
| 12 | |
| 13 | |
| 14 | type alias Msg = |
| 15 | {} |
| 16 | |
| 17 | |
| 18 | page : Shared.Model -> Route () -> Page Model Msg |
| 19 | page _ route = |
| 20 | Page.new |
| 21 | { init = init route.query |
| 22 | , update = \_ m -> ( m, Effect.none ) |
| 23 | , subscriptions = \_ -> Sub.none |
| 24 | , view = view |
| 25 | } |
| 26 | |> Page.withLayout (\_ -> Layouts.Header {}) |
| 27 | |
| 28 | |
| 29 | type alias Model = |
| 30 | { error : String } |
| 31 | |
| 32 | |
| 33 | init : Dict String String -> () -> ( Model, Effect Msg ) |
| 34 | init query () = |
| 35 | case |
| 36 | ( Dict.get "access_token" query |
| 37 | , Dict.get "refresh_token" query |
| 38 | , Dict.get "error" query |
| 39 | ) |
| 40 | of |
| 41 | ( Just at, Just rt, _ ) -> |
| 42 | ( { error = "" }, Effect.signin { accessToken = at, refreshToken = rt } ) |
| 43 | |
| 44 | ( _, _, Just error ) -> |
| 45 | ( { error = error }, Effect.none ) |
| 46 | |
| 47 | _ -> |
| 48 | ( { error = "Invalid server response" }, Effect.none ) |
| 49 | |
| 50 | |
| 51 | view : Model -> View msg |
| 52 | view model = |
| 53 | { title = "Oauth" |
| 54 | , body = |
| 55 | [ Components.Utils.commonContainer |
| 56 | [ Components.Box.error model.error ] |
| 57 | ] |
| 58 | } |