/
Register.razor
111 lines (96 loc) · 3.14 KB
/
Register.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
@page "/register"
@using BlazorWasmAuth.Identity
@inject IAccountManagement Acct
<PageTitle>Register</PageTitle>
<h1>Register</h1>
<AuthorizeView>
<Authorized>
<div class="alert alert-success">You're already logged in as @context.User.Identity?.Name.</div>
</Authorized>
<NotAuthorized>
@if (success)
{
<div class="alert alert-success">You successfully registered. Now you can <a href="login">login</a>.</div>
}
else
{
if (errors)
{
foreach (var error in errorList)
{
<div class="alert alert-danger">@error</div>
}
}
<div class="flex-outer">
<div>
<label for="email">
Email:
</label>
<input autofocus autocomplete="on" required id="email" name="emailInput" placeholder="Enter your email address" type="email" @bind-value="email" />
</div>
<div>
<label for="password">
Password:
</label>
<input required id="password" name="passwordInput" placeholder="Enter your password" type="password" @bind-value="password" /><br />
</div>
<div>
<label for="confirmPassword">
Retype password:
</label>
<input required id="confirmPassword" name="confirmPasswordInput" placeholder="Re-enter your password" type="password" @bind-value="confirmPassword" />
</div>
<div>
<button class="btn btn-primary" @onclick="DoRegisterAsync">Register</button>
</div>
</div>
}
</NotAuthorized>
</AuthorizeView>
@code {
private bool success, errors;
private string email = string.Empty;
private string password = string.Empty;
private string confirmPassword = string.Empty;
private string[] errorList = [];
public async Task DoRegisterAsync()
{
success = errors = false;
errorList = [];
if (string.IsNullOrWhiteSpace(email))
{
errors = true;
errorList = ["Email is required."];
return;
}
if (string.IsNullOrWhiteSpace(password))
{
errors = true;
errorList = ["Password is required."];
return;
}
if (string.IsNullOrWhiteSpace(confirmPassword))
{
errors = true;
errorList = ["Please confirm your password."];
return;
}
if (password != confirmPassword)
{
errors = true;
errorList = ["Passwords don't match."];
return;
}
var result = await Acct.RegisterAsync(email, password);
if (result.Succeeded)
{
success = true;
email = password = confirmPassword = string.Empty;
}
else
{
errors = true;
errorList = result.ErrorList;
}
}
}