Authentication with FT4 accounts
In this section, authentication with FT4 accounts will be explored.
Authentication with FT4 accounts is simple and efficient. First, import the auth
module:
import lib.ft4.auth;
Next, a call to auth.authenticate
is added in the operations:
operation make_post(content: text) {
val account = auth.authenticate();
require(content.size() <= 255, "A post cannot be longer than 255 characters");
val user = user @ { account };
create post ( user, content );
}
operation follow_user(follow_id: byte_array) {
val account = auth.authenticate();
val user = user @ { account };
val follow = require_user(follow_id);
create follower ( user = follow, follower = user );
}
operation unfollow_user(unfollow_id: byte_array) {
val account = auth.authenticate();
val user = user @ { account };
val follow = require_user(unfollow_id);
delete follower @? { .user == follow, .follower == user };
}
The auth.authenticate
function handles authentication, streamlining the process and enhancing security. The user’s public key no longer needs to be passed to the operations.
To activate authentication, register an auth handler. Add the following code to your Rell module:
@extend(auth.auth_handler)
function () = auth.add_auth_handler(
flags = ["S"]
);
This extension function registers a new handler with the "S" flag,
matching the flag used during account creation.
It registers globally, enabling it to work with all operations that utilize the auth.authenticate
function
in your app. If you wish to assign a specific handler for a particular operation or module,
you can include a scope argument in the auth.add_auth_handler
.