-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAuthController.cs
145 lines (118 loc) · 4.53 KB
/
AuthController.cs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using netcore_postgres_oauth_boiler.Models;
namespace netcore_postgres_oauth_boiler.Controllers
{
public class AuthController : Controller
{
private readonly DatabaseContext _context;
private readonly ILogger<AuthController> _logger;
public AuthController(ILogger<AuthController> logger, DatabaseContext context)
{
_logger = logger;
_context = context;
}
public IActionResult Login()
{
return View();
}
public IActionResult Register()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login([FromForm] string email, [FromForm] string password)
{
Console.WriteLine($"{email} is logging in.");
// Loading session
if (!HttpContext.Session.IsAvailable)
await HttpContext.Session.LoadAsync();
// Disallowing already logged-in users
if (HttpContext.Session.GetString("user") != null)
{
ViewData["error"] = "You are already logged in!";
return View("Login");
}
// Fetching the user
var user = await _context.Users.Where(c => Regex.IsMatch(c.email, email)).FirstOrDefaultAsync();
// Checking if user exists and verifying password
if (user == null || !BCrypt.Net.BCrypt.Verify(password, user.password))
{
ViewData["error"] = "Incorrect email or password!";
return View("Login");
}
// Attaching user to session
HttpContext.Session.SetString("user", user.id);
// Setting info alert to be shown
ViewData["info"] = "You have logged in!";
// Rendering index
return View("~/Views/Home/Index.cshtml");
}
[HttpPost]
public async Task<IActionResult> Register([FromForm] string email, [FromForm] string password)
{
// Loading session
if (!HttpContext.Session.IsAvailable)
await HttpContext.Session.LoadAsync();
// Verifying user is not logged in
if (HttpContext.Session.GetString("user") != null)
{
ViewData["error"] = "You are already logged in!";
return View("Register");
}
// Verifying data
if (email == null || password == null)
{
ViewData["error"] = "Missing username or password!";
return View("Register");
}
// Checking for duplicates
var count = await _context.Users.Where(c => Regex.IsMatch(c.email, email)).CountAsync();
if (count != 0)
{
ViewData["error"] = "This email is already taken!";
return View("Register");
}
// Saving the user
User u = new User(email, password);
_context.Users.Add(u);
await _context.SaveChangesAsync();
// Assigning user id to session
HttpContext.Session.SetString("user", u.id);
// Setting info alert
ViewData["info"] = "You have successfully registered!";
return View("~/Views/Home/Index.cshtml");
}
[HttpGet]
public async Task<IActionResult> SessionTest()
{
if (!HttpContext.Session.IsAvailable)
await HttpContext.Session.LoadAsync();
var c = HttpContext.Session.GetString("user");
return Ok("You are: " + c ?? "not logged in.");
}
[HttpGet]
public async Task<IActionResult> Logout()
{
// Removing session
if (!HttpContext.Session.IsAvailable)
await HttpContext.Session.LoadAsync();
HttpContext.Session.Clear();
ViewData["info"] = "You have logged out!";
return View("~/Views/Home/Index.cshtml");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}