Lamdera Auth
This library is experimental and a work in progress!
Example implementation
You can see a draft implementation of lamdera/auth replacing a fake user/pass auth with a real Github Auth on the Lamdera Realword repo.
Progress
Progress:
- OAuth flow
- Email magic link flow
- URL route handling
- Logout handling
- Improve the API surface area
- Actually make this an installable package with nice docs
- Stretch goal: elm-review rule to set everything up!
Vendored packages
This package vendors two other Elm packages in order to make modifications:
truqu/elm-oauth2
: license (MIT), modification notesleojpod/elm-jwt
: license (GPLv3), modification notes
Ideally these will be de-vendored into a regular Elm dependencies in future.
Install
Until this is available as a package:
- Clone this repo into your project as a git submodule (or vendor it manually by copy pasting src)
- Reference
src
in your project'selm.json:source-directories
- Install the relevant deps:
yes | elm install elm/browser
yes | elm install elm/bytes
yes | elm install elm/http
yes | elm install elm/json
yes | elm install elm/regex
yes | elm install elm/time
yes | elm install elm/url
yes | elm install elm-community/dict-extra
yes | elm install elm-community/list-extra
yes | elm install chelovek0v/bbase64
yes | elm install ktonon/elm-crypto
yes | elm install ktonon/elm-word
yes | elm install NoRedInk/elm-json-decode-pipeline
yes | elm install TSFoster/elm-sha1
You might also have luck with elm-git-install, though its not been tried yet.
Setup
- Create
src/Auth.elm
:
module Auth exposing (..)
import Auth.Common
import Auth.Method.EmailMagicLink
import Auth.Method.OAuthGithub
import Auth.Method.OAuthGoogle
import Types exposing (..)
config : Auth.Common.Config FrontendMsg ToBackend BackendMsg ToFrontend FrontendModel BackendModel
config =
{ toBackend = AuthToBackend
, toFrontend = AuthToFrontend
, backendMsg = AuthBackendMsg
, sendToFrontend = Lamdera.sendToFrontend
, sendToBackend = Lamdera.sendToBackend
, methods =
[ Auth.Method.EmailMagicLink.configuration
, Auth.Method.OAuthGithub.configuration Config.githubAppClientId Config.githubAppClientSecret
, Auth.Method.OAuthGoogle.configuration Config.googleAppClientId Config.googleAppClientSecret
]
}
- Modify the 2 core Model types in
src/Types.elm
:
import Url exposing (Url)
import Auth.Common
type alias FrontendModel =
{ ...
, authFlow : Auth.Common.Flow
, authRedirectBaseUrl : Url
}
type alias BackendModel =
{ ...
, pendingAuths : Dict Lamdera.SessionId Auth.Common.PendingAuth
}
- Modify the 4 core Msg types in
src/Types.elm
:
import Auth.Common
type FrontendMsg
...
| AuthFrontendMsg Auth.Common.FrontendMsg
type ToBackend
...
| AuthToBackend Auth.Common.ToBackend
type BackendMsg
...
| AuthBackendMsg Auth.Common.BackendMsg
type ToFrontend
...
| AuthToFrontend Auth.Common.ToFrontend
- Implement the 4 new Msg variants:
Frontend.elm
:
update msg model =
...
AuthFrontendMsg authMsg ->
Auth.Flow.frontendMsg authMsg
updateFromBackend msg model =
...
AuthToFrontend authMsg ->
Auth.Flow.fromBackend authMsg
Backend.elm
:
update msg model =
...
AuthBackendMsg authMsg ->
Auth.Flow.backendMsg authMsg
updateFromFrontend sessionId clientId msg model =
...
AuthToBackend authMsg ->
Auth.Flow.fromFrontend authMsg
- Adjust your routing handlers:
TBC