Sign Up
Implementation
Sign-Up Payload
Create a class for your Sign-Up payload, similarly to how you would for the Sign-In payload. This class will be used to pass the user's sign-up request details to the IAuthenticationService
implementation.
Note
This class needs to be serializable, so it can be passed between the Client and Server projects over http.
// Example Sign-Up payload
public class SignUpPayload
{
public string Email { get; set; }
public string Password { get; set; }
}
Authentication Service
Use your Sign-Up payload class as a second generic parameter in your IAuthenticationService
implementation. You can inherit from the base AuthenticationService
class, or implement the IAuthenticationService
interface directly.
// Example Authentication Service
public class MyAmazingAuthenticationService : AuthenticationService<SignInPayload, SignUpPayload>
{
// Sign-In and other methods omitted for brevity
public override Task<AuthenticationResult> SignUpAsync(SignUpPayload signUpPayload, CancellationToken cancellationToken = default)
{
var jwtPair = BuildJwtPair();
var authResult = Success(jwtPair);
return Task.FromResult(authResult);
}
// ...
}
User Service
You can now use IUserService
in your Blazor Pages to sign the user up:
// MyPage.razor
[Inject] IUserService<SignInPayload, SignUpPayload> UserService { get; set; }
private async Task SignUpAsync()
{
// Simulating user input
var signUpPayload = new SignUpPayload("some data");
// Signing the user up
var authenticationResult = await UserService.SignUpAsync(signUpPayload);
// Refresh the page after signing up
// to let Blazor know that the user's authentication state has changed
NavigationManager.NavigateTo(NavigationManager.Uri, true);
}
For additional guidance, see sample flows in Use Cases section.