This repository shows an example use of Fable.OidcClient library to carry out user authentication and authorization in Elmish single-page application.
The solution consists of 3 projects:
Api
: Saturn framework web server that returns user's predefined to-do list when called underGET /todos
.IdentityServer
: Identity Provider service that issues access and identity tokens to client apps.Client
: Elmish single-page application - the API client.
You'll need to install the following pre-requisites in order to build the application:
- The .NET Core SDK 3.1 or higher.
- npm package manager.
- Node LTS.
Before you run the project for the first time only you must install dotnet "local tools" with this command:
dotnet tool restore
To concurrently run the server, IdentityServer4 (OpenID Connect identity provider) and the client components in watch mode use the following command:
dotnet fake build -t run
Then open http://localhost:8080
in your browser.
The application is very simple. Depending on whether the user is authenticated or not - it displays a login
button, or user's name, ID, hardcoded TODO list (retrieved over an API - different for every user) and a logout
button.
There are 2 predefined users that you can try out:
alice
(password:alice
)bob
(password:bob
)
Note that the Client app is not served over https with Webpack. You can change it by:
- modifying
webpack.config.js
configuration file by settingCONFIG.httpsEnabled
totrue
- modify the client by changing
src/Client/App.fs
:settings: UserManagerSettings
record fieldsredirect_uri
andpost_logout_redirect_uri
should be prefixed withhttps
- modify IdentityServer configuration by changing
src/IdentityServer/Config.cs
: JavaScript client fieldsRedirectUris
,PostLogoutRedirectUris
andAllowedCorsOrigins
should be prefixed withhttps
- modify the API server by changing
src/Api/App.fs
: modify theuse_cors
operation of theapp
computation expression (it should mentionhttps
)
You will need to accept invalid certificate risk when navigating between pages on Chrome. Firefox will not even allow you to temporarily accept the invalid certificate.