Skip to content
Snippets Groups Projects
Commit 562d3614 authored by Daniel Lorych's avatar Daniel Lorych :cookie:
Browse files

Merge branch '41-class-login' into 'main'

Resolve "Class: Login"

Closes #41

See merge request d2-lorych/helios!6
parents 177ebdaf efdfa233
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Helios.Source;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Helios.Controllers
{
public class LoginController : Controller
{
private readonly LoginService _loginService;
public LoginController(LoginService service)
{
_loginService = service;
}
public IActionResult UserLogin(string emailAddress, string password)
{
Login login = _loginService.GetLogin(emailAddress, password);
if (login.IsValidUser)
{
return View();
}
else
{
return View();
}
}
}
}
\ No newline at end of file
......@@ -9,11 +9,15 @@
<ItemGroup>
<None Remove="Source\" />
<None Remove="Npgsql" />
<None Remove="Microsoft.VisualStudio.Web.CodeGeneration.Design" />
<None Remove="Swashbuckle.AspNetCore" />
</ItemGroup>
<ItemGroup>
<Folder Include="Source\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="6.0.4" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
</ItemGroup>
</Project>
var builder = WebApplication.CreateBuilder(args);
using Helios.Source;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddTransient<LoginService>(provider
=> new LoginService(builder.Configuration.GetConnectionString("default")));
var app = builder.Build();
// Configure the HTTP request pipeline.
......
using System;
namespace Helios.Source
{
public class Login
{
public string EmailAddress { get; set; }
public bool IsValidUser { get; set; }
public bool IsAdmin { get; set; }
public Login(string emailAddress)
{
EmailAddress = emailAddress;
}
}
}
using System;
using Npgsql;
namespace Helios.Source
{
public class LoginService
{
private readonly NpgsqlConnection _connection;
private string password = string.Empty;
public LoginService(string connectionString)
{
_connection = new NpgsqlConnection(connectionString);
}
public Login GetLogin(string email, string password)
{
this.password = password;
Login userLogin = new Login(email);
userLogin = GetUserDetails(userLogin);
return userLogin;
}
private Login GetUserDetails(Login userLogin)
{
NpgsqlCommand cmd = new NpgsqlCommand("select * from user_login where email = @emailParam and user_password = @passwordParam", _connection);
cmd.Parameters.AddWithValue("@emailParam", userLogin.EmailAddress);
cmd.Parameters.AddWithValue("@passwordParam", this.password);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
userLogin.IsValidUser = true;
userLogin.IsAdmin = bool.Parse(reader["is_admin"].ToString());
}
else
{
userLogin.IsValidUser = false;
userLogin.IsAdmin = false;
}
return userLogin;
}
}
}
create table Login(
create table User_Login(
id SERIAL PRIMARY KEY,
email TEXT,
user_password TEXT,
is_admin BOOLEAN
)
INSERT INTO Login
INSERT INTO User_Login
(email, user_password, is_admin)
VALUES
('user@helios.co.uk', 'userpassword', FALSE),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment