Fable library for secured api request, work with the User and Tokens.
Add following into paket.references
Alma.Fable.Authorization
User is set to a local storage with default key user. You should change it to a app specific key.
open Alma.Fable.Authorization
User.setUserKey "my-domain.user"First of all you need to have an Api (defined in Shared project of your SAFE app).
// Shared
open Alma.Authorization.Common
type IMyApi = {
// Public actions
Login: Username * Password -> AsyncResult<User, string>
// Secured actions
LoadData: SecuredApiCall<unit, Data list, string> // Where unit is a request data, Data list is the response and string is an error
}Define in the server.
open Feather.ErrorHandling
open Feather.ErrorHandling.Result.Operators
open Alma.Authorization
open Alma.Authorization.Common
// Server
let inline private (>?>) authorize action =
Authorize.authorizeAction
currentApplication.Authorization
id
logAuthorizationError
authorize
action
let (api: IMyApi) = {
Login = fun (username, password) -> asyncResult {
let! credentials =
(username, password)
|> Credentials.deserialize <@> CredentialsError.format
return!
credentials
|> User.login currentApplication.Authorize
}
LoadData = Authorize.withLogin >?> fun () -> asyncResult {
return [ (* list of Data *) ]
}
}Then define a proxy to your Api.
// Client
open Alma.Authorization.Common
open Alma.Fable.Authorization
open Shared
type MyErrorType = MyErrorType of string
module private Server =
open Fable.Remoting.Client
/// A proxy you can use to talk to server directly
let api : IMyApi =
Remoting.createApi()
|> Remoting.withRouteBuilder Route.builderForClient
|> Remoting.buildProxy<IMyApi>
//
// Public actions
//
let login = Server.api.Login
//
// Secured actions
//
let loadData: unit -> AsyncResult<Data list, Secure.SecureError<MyErrorType>> = Secure.api MyErrorType Server.api.LoadData- Increment version in
Alma.Fable.Profiler.fsproj - Update
CHANGELOG.md - Commit new version and tag it
./build.sh build./build.sh -t tests