Skip to content

Commit

Permalink
upgrade implicit-flow example to elm-0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Sep 3, 2018
1 parent 3a60354 commit ef85924
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 242 deletions.
90 changes: 52 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,64 +46,79 @@ import OAuth.Implicit
##### Authorizing & Authenticating

```elm
type alias Model =
{ oauth :
{ clientId : String
, redirectUri : Url
}
-- [ ... ]
}


type Msg
= ClientIdSubmitted
-- [ ... ]


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
update msg ({ oauth } as model) =
case msg of
NoOp ->
model ! []

Authorize ->
model
! [ OAuth.Implicit.authorize
{ clientId = "clientId"
, redirectUri = "redirectUri"
, responseType = OAuth.Token -- Use the OAuth.Token response type
, scope = [ "whatever" ]
, state = Nothing
, url = "authorizationEndpoint"
}
]
ClientIdSubmitted ->
( model
, OAuth.Implicit.authorize
{ clientId = model.oauth.clientId
, redirectUri = model.oauth.redirectUri
, responseType = OAuth.Token
, scope = [ "email", "profile" ]
, state = Nothing
, url = authorizationEndpoint
}
)

-- [ ... ]
```

##### Parsing the token

```elm
init : Navigation.Location -> ( Model, Cmd Msg )
init location =
init : () -> Url -> Key -> ( Model, Cmd Msg )
init _ origin navKey =
let
model = {}
model =
{ oauth = { clientId = "", redirectUri = origin }
, error = Nothing
, token = Nothing
, profile = Nothing
}
in
case OAuth.Implicit.parse location of
-- A token has been parsed
Ok { token } ->
{ model | token = Just token } ! []

-- Nothing to parse, unauthenticated
Err OAuth.Empty ->
model ! []

-- An other type of error (invalid parsing or an actual OAuth error)
Err _ ->
model ! []
case OAuth.Implicit.parse origin of
Ok { token } ->
( { model | token = Just token }
, getUserProfile profileEndpoint token
)

Err err ->
( { model | error = showParseErr err }
, Cmd.none
)
```


##### Using the token

```elm
let
req =
getUserProfile : Url -> OAuth.Token -> Cmd Msg
getUserProfile endpoint token =
Http.send GotUserInfo <|
Http.request
{ method = "GET"
, body = Http.emptyBody
, headers = OAuth.use token [] -- Add the token to the http headers
, headers = OAuth.use token []
, withCredentials = False
, url = "whatever"
, expect = Http.expectJson decoder
, url = Url.toString endpoint
, expect = Http.expectJson profileDecoder
, timeout = Nothing
}
in
{ model | token = Just token } ! [ Http.send handleResponse req ]
```


Expand Down Expand Up @@ -272,4 +287,3 @@ getToken code =
## Changelog

[CHANGELOG.md](./CHANGELOG.md)

Loading

0 comments on commit ef85924

Please sign in to comment.