Authentication with FT4 accounts
In this section, we'll explore authentication with FT4 accounts.
Authentication with FT4 accounts is straightforward. Import the auth
module:
import lib.ft4.auth;
Now, add a call to auth.authenticate
in your operations:
operation make_post(content: text) {
val account = auth.authenticate();
require(content.size() <= 255, "A post cannot be longer than 255 letters");
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 };
}
Authentication is handled by the auth.authenticate
function, making it easier and more secure. You no longer need to
pass the user's pub key to operations.
To activate authentication, we must register an auth handler. We add the following to our Rell module:
@extend(auth.auth_handler)
function () = auth.add_auth_handler(
flags = ["MySession"]
);
This extension function registers a new handler with the "MySession" flag, which matches the flag used when creating the
account. It's registered globally and will work for all operations using the auth.authenticate
function in your app.
You can add a scope argument to auth.add_auth_handler
if you want a specific handler for a particular operation or
module.