source: KernelRecordsMVC.Web/Controllers/AccountController.cs@ 08aefc6

main
Last change on this file since 08aefc6 was 08aefc6, checked in by mmilevski <markomilevski3@…>, 4 weeks ago

Initial commit

  • Property mode set to 100644
File size: 3.9 KB
Line 
1using Microsoft.AspNetCore.Mvc;
2using KernelRecordsMVC.Data;
3using KernelRecordsMVC.Models;
4using KernelRecordsMVC.Application.ViewModels;
5using Microsoft.AspNetCore.Http;
6
7namespace KernelRecordsMVC.Controllers;
8
9public class AccountController : Controller
10{
11 private readonly KernelRecordsContext _context;
12
13 public AccountController(KernelRecordsContext context)
14 {
15 _context = context;
16 }
17
18 // ==============================
19 // REGISTER - GET
20 // ==============================
21
22 [HttpGet]
23 public IActionResult Register()
24 {
25 return View();
26 }
27
28
29 // ==============================
30 // REGISTER - POST
31 // ==============================
32
33 [HttpPost]
34 [ValidateAntiForgeryToken]
35 public IActionResult Register(RegisterViewModel model)
36 {
37 if (!ModelState.IsValid)
38 return View(model);
39
40 if (_context.Users.Any(x => x.Username == model.Username))
41 {
42 ModelState.AddModelError(
43 "Username",
44 "Username is already taken.");
45
46 return View(model);
47 }
48
49 if (_context.Users.Any(x => x.Email == model.Email))
50 {
51 ModelState.AddModelError(
52 "Email",
53 "Email is already registered.");
54
55 return View(model);
56 }
57
58 var user = new User
59 {
60 Email = model.Email,
61 Username = model.Username,
62
63 // We'll replace this with proper password hashing
64 // in the security cleanup section.
65 Password = model.Password,
66
67 DateCreated = DateTime.Today,
68 ShippingAddress = model.ShippingAddress,
69 TelephoneNumber = model.TelephoneNumber
70 };
71
72 _context.Users.Add(user);
73
74 _context.SaveChanges();
75
76 // Every normal registered user is a Consumer.
77 var consumer = new Consumer
78 {
79 UserId = user.UserId,
80 PointsCollected = 0
81 };
82
83 _context.Consumers.Add(consumer);
84
85 _context.SaveChanges();
86
87 return RedirectToAction(nameof(Login));
88 }
89
90
91 // ==============================
92 // LOGIN - GET
93 // ==============================
94
95 [HttpGet]
96 public IActionResult Login()
97 {
98 return View();
99 }
100
101
102 // ==============================
103 // LOGIN - POST
104 // ==============================
105
106 [HttpPost]
107 [ValidateAntiForgeryToken]
108 public IActionResult Login(LoginViewModel model)
109 {
110 if (!ModelState.IsValid)
111 return View(model);
112
113 var user = _context.Users
114 .FirstOrDefault(x =>
115 x.Username == model.Username &&
116 x.Password == model.Password);
117
118 if (user == null)
119 {
120 ModelState.AddModelError(
121 "",
122 "Invalid username or password.");
123
124 return View(model);
125 }
126
127 HttpContext.Session.SetInt32(
128 "UserId",
129 checked((int)user.UserId));
130
131 HttpContext.Session.SetString(
132 "Username",
133 user.Username);
134
135 // Determine account type from the actual
136 // ADMINS / CONSUMERS tables.
137 var admin = _context.Admins
138 .FirstOrDefault(x => x.UserId == user.UserId);
139
140 if (admin != null)
141 {
142 HttpContext.Session.SetString(
143 "Role",
144 "Admin");
145
146 HttpContext.Session.SetString(
147 "AdminType",
148 admin.Type.ToString());
149 }
150 else
151 {
152 HttpContext.Session.SetString(
153 "Role",
154 "Consumer");
155 }
156
157 return RedirectToAction(
158 "Index",
159 "Home");
160 }
161
162
163 // ==============================
164 // LOGOUT
165 // ==============================
166
167 public IActionResult Logout()
168 {
169 HttpContext.Session.Clear();
170
171 return RedirectToAction(
172 "Index",
173 "Home");
174 }
175}
Note: See TracBrowser for help on using the repository browser.