Miru.Userfy.Invitable
is a Miru extension that provides API for user invitation.
It covers saving a new user, sending email invitation,
updating password and other information set by the user.
Miru.Userfy is an extension component part of Miru. It provides authentication, authorization, and other user management features.
.NET >= 6.0
Miru >= 0.9.6
Add the library into your Miru App project:
miru app dotnet add package Miru.Userfy.Invitable
Build the App project:
miru app dotnet build
Run UserfyInvitable installer:
miru userfy.invitable.install
It will add into your project:
/{app}/Database/Migrations/{datetime}_AlterUsersAddInvitable.cs
/{app}/Admin/Users/Invitations/_Invitation.mail.cshtml
/{app}/Users/Invitations/Register.cshtml
/{app}/Users/Invitations/UserRegister.cs
If you use different paths, you can move the files into your directory conventions.
Decorate your entity User.cs
with IInvitable
:
public class User : UserfyUser, IInvitable
{
public string InvitationToken { get; set; }
public DateTime? InvitationAcceptedAt { get; set; }
// from IInactivable
public bool IsInactive { get; set; }
}
Note that IInvitable depends on IInactivable.
Add into your Startup.cs
or Program.cs
ConfigureServices:
.AddUserfyInvitable<User>(opt =>
{
// set true if the email will be queued and send later. otherwise, it will be sent now
opt.SendEmailLater = false; // false by default
// customize the invitation email
opt.MailConfig((mail, user) =>
{
mail.TemplateAt("/Features/Accounts", "_Invitation", user);
});
})
User invitation features are provided by the class UserInvitation
.
AddAndInviteAsync
saves a new User into Users
table and send the email inviting the user:
public async Task<Result> Handle(Command request, CancellationToken ct)
{
var user = new User
{
Email = request.Email,
UserName = request.Email
};
await _userInvitation.AddAndInviteAsync(user, ct);
return new Result { Email = request.Email };
}
AcceptInvitation
saves the password set by the own user:
public async Task<FeatureResult> Handle(Command request, CancellationToken ct)
{
var user = await _db.Users.ByInvitationTokenOrFailAsync(request.Token, ct);
user.AcceptInvitation();
await _userInvitation.SetPasswordAsync(user, request.Password, ct);
await _userLogin.LoginAsync(user.Email, request.Password);
return new FeatureResult(_userfyOptions.AfterLoginFeature);
}
ResendInvitationAsync
sends a new email invitation to an user:
public async Task<Result> Handle(Command request, CancellationToken ct)
{
var user = await _db.Users.ByIdOrFailAsync(request.UserId, ct);
await _userInvitation.ResendInvitationAsync(user, ct);
return new Result() { UserId = user.Id };
}
Miru.Userfy.Invitable
is licensed under the MIT license.