Here’s my first real use of ports in an Elm application. It’ll be light on the commentary, heavy on code.
This example will use the Google API client for JavaScript to perform OAuth2 authentication using a user’s Google credentials. This will integrate into an Elm application using ports. Specifically:
Interop.elm
There’ll be a small amount of JavaScript glue code in index.js
which will take care of marshalling the ports to and from JavaScript functions.
On the Elm side of things, the application will provide the bare minimum components to display the results of these calls via the ports mechanism to the user.
Note that I use the following packages/tools in this project:
The application can be run by typing make
at the command line:
.gitignore
You’ll need to create your google-api-client-id.txt
file by grabbing your Google API client ID after consulting the following resources:
elm-package.json
This file is typically updated using the elm-package
command.
index.html
<body>
element where Elm application is renderedindex.js
This is the main JavaScript application code:
"use strict";
because you should always do this!Interop.elm
Main.elm
main
functionMakefile
Model.elm
message
field for displaying error messages to the userMsg.elm
Msg
typeNativeError
is used to report errors back from JavaScriptAuthStateChanged
is fired whenever the user signs in or outSignInClicked
is triggered when the user clicks the sign-in buttonSignOutClicked
is triggered when the user clicks the sign-out buttonSubscriptions.elm
Update.elm
update
function for updating the state of the appView.elm
Check out the full project here.
Content © 2024 Richard Cook. All rights reserved.