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.elmThere’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:
.gitignoreYou’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.jsonThis file is typically updated using the elm-package command.
index.html<body> element where Elm application is renderedindex.jsThis is the main JavaScript application code:
"use strict"; because you should always do this!Interop.elmMain.elmmain functionMakefileModel.elmmessage field for displaying error messages to the userMsg.elmMsg 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.elmUpdate.elmupdate function for updating the state of the appView.elmCheck out the full project here.
Content © 2025 Richard Cook. All rights reserved.