Implement user login and registration; update database connection management to be per use not a singleton by adjusting service lifetimes in DI.

This commit is contained in:
2026-02-26 18:45:15 -08:00
parent 6172d1c373
commit 79ee297e61
6 changed files with 194 additions and 90 deletions
+92 -32
View File
@@ -3,46 +3,70 @@
<PageTitle>Home</PageTitle>
<div class="card-holder">
<a class="card">
HOME
</a>
<a class="card">
S&P Chart
</a>
<a class="card">
Connect
</a>
<a class="card">
About
</a>
</div>
<div class="main-area">
<button @onclick="pullandtrain">@button1Text</button>
<button @onclick="predict">@button2Text</button>
@foreach (stockPredictionPair cur in predictions){
<div>
<h1>Symbol: @cur.Symbol</h1><br />
@if (cur.Movement == -1){
<p>Sell</p>
} else if (cur.Movement == 1){
<p>Buy</p>
} else{
<p>Hold</p>
}
<!-- Login Frame -->
@if (Session == null){
<div class="gridFrame">
<div><h2>LOGIN</h2></div>
<div class="loginRow">
<div><span>username</span></div>
<input @bind-value="userName" type="text" />
</div>
<div class="loginRow">
<div><span>password</span></div>
<input @bind-value="passWord" type="password" />
</div>
<div class="loginRow">
<button @onclick="LoginSession">Login</button>
<button @onclick="registerSession">Register</button>
</div>
<div>
<span style="color: red;">@loginError</span>
</div>
</div>
<!-- User Frame -->
}else{
<div class="gridFrame">
<span>UserName: @Session.UserName</span><br />
</div>
}
<!-- AI Frame -->
<div class="gridFrame">
<div>
<button @onclick="pullandtrain">@button1Text</button>
<button @onclick="predict">@button2Text</button>
</div>
<div>
@foreach (stockPredictionPair cur in predictions){
<div>
<h1>Symbol: @cur.Symbol</h1><br />
@if (cur.Movement == -1){
<p>Sell</p>
} else if (cur.Movement == 1){
<p>Buy</p>
} else{
<p>Hold</p>
}
</div>
}
</div>
</div>
</div>
@code {
string button1Text = "Train AI";
string button2Text = "Predict AI";
// User Stuff
loginSession? Session = null;
// Login Stuff
string userName = "";
string passWord = "";
string loginError = "";
List<stockPredictionPair> predictions = new List<stockPredictionPair>(){
new stockPredictionPair(){ Symbol = "AAPL" },
@@ -51,6 +75,36 @@
new stockPredictionPair(){ Symbol = "FUN" }
};
async Task LoginSession(){
string dbPrefix = $"[{userName.ToLower()}]:"; // Set the DB prefix for the get and set
string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash
if (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid
Session = new loginSession(){
UserName = userName.ToLower()
};
}else{
loginError = "wrong password";
}
}
async Task registerSession(){
string dbPrefix = $"[{userName.ToLower()}]:";
string passwordhash = dbDriver.Get( dbPrefix + "password" );
if (string.IsNullOrEmpty(passwordhash)){
dbDriver.Set( dbPrefix + "password", BCrypt.HashPassword( passWord, BCrypt.GenerateSalt() ) );
Session = new loginSession(){
UserName = userName.ToLower()
};
}else{
loginError = "account is taken";
}
}
// AI Stuff
string button1Text = "Train AI";
string button2Text = "Predict AI";
async Task pullandtrain(){
button1Text = "Do not refresh the page. The data is refreshing.";
await Task.Delay(1);
@@ -76,9 +130,15 @@
await Task.Delay(1);
}
class stockPredictionPair{
public string Symbol = "";
public int Movement = 0;
// Data Types
class stockPredictionPair {
public string Symbol { get; set; } = "";
public int Movement { get; set; } = 0;
}
class loginSession {
public string UserName { get; set; } = "";
}
}