-
Notifications
You must be signed in to change notification settings - Fork 3
D evforce #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: Develop
Are you sure you want to change the base?
D evforce #20
Changes from all commits
ef0afb7
af225d6
9124e9a
89107e7
bb7e5c3
054df2d
ebf8621
33e6d79
4b5b126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,21 +12,21 @@ namespace Know_Your_Nation_Speedy.Controllers | |
| [ApiController] | ||
| public class UsersController : ControllerBase | ||
| { | ||
| private readonly MyDbContext _db; | ||
|
|
||
| EmailService emailService = new EmailService(); | ||
| private MyDbContext _db; | ||
| readonly IConfiguration _config; | ||
| public UsersController(MyDbContext context, IConfiguration config) | ||
| { | ||
| _db = context; | ||
| _config = config; | ||
| } | ||
|
|
||
| // GET api/values | ||
| [HttpGet] | ||
| public async Task<ActionResult<IEnumerable<User>>> Get() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did this even compile? |
||
| { | ||
| return await _db.UserEntries.ToListAsync(); | ||
| } | ||
|
|
||
| [HttpGet("{id}")] | ||
| public async Task<IActionResult> GetEntry([FromRoute] int id) | ||
| { | ||
|
|
@@ -38,7 +38,6 @@ public async Task<IActionResult> GetEntry([FromRoute] int id) | |
| await _db.SaveChangesAsync(); | ||
| return Ok(entry); | ||
| } | ||
|
|
||
| [HttpPost("login")] | ||
| public ActionResult<User> Login([FromBody] User User) | ||
| { | ||
|
|
@@ -56,22 +55,19 @@ public ActionResult<User> Login([FromBody] User User) | |
| return BadRequest(); | ||
| } | ||
| } | ||
|
|
||
| [HttpPost] | ||
| public async Task Post([FromBody] User User) | ||
| { | ||
| await _db.UserEntries.AddAsync(User); | ||
| await _db.SaveChangesAsync(); | ||
| } | ||
|
|
||
| [HttpPut("{id}")] | ||
| public async Task Put(int id, [FromBody] User User) | ||
| { | ||
| var entry = await _db.UserEntries.FindAsync(id); | ||
| entry = User; | ||
| await _db.SaveChangesAsync(); | ||
| } | ||
|
|
||
| [HttpDelete("{id}")] | ||
| public async Task<IActionResult> DeleteEntry([FromRoute]int id) | ||
| { | ||
|
|
@@ -84,5 +80,27 @@ public async Task<IActionResult> DeleteEntry([FromRoute]int id) | |
| await _db.SaveChangesAsync(); | ||
| return Ok(entry); | ||
| } | ||
| [HttpPut()] | ||
| [Route("ForgotPassword/{mail}")] | ||
| public async Task getCodes(string mail) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function name is not consistent with the standards of the rest of the project and C# as a whole
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, why call it getCodes when in fact it sends an email? |
||
| { | ||
| string code = emailService.generateCode(); | ||
| var entry = await _db.UserEntries.FindAsync(mail); | ||
|
|
||
| if (entry != null) | ||
| { | ||
| emailService.SendMail(mail, "testing", code); | ||
| } | ||
| } | ||
| // PUT api/values/5 | ||
| [HttpPut()] | ||
| [Route("ResetPassword/{password} + {mail}")] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the |
||
| public async Task ResetPassword(string mail,string password) | ||
| { | ||
| var entry = await _db.UserEntries.SingleOrDefaultAsync(m => m.Email == mail); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need a null check and error handling here |
||
| entry.Password = password; | ||
| _db.UserEntries.Update(entry); | ||
| await _db.SaveChangesAsync(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.Net; | ||
| using System.Net.Mail; | ||
| using System.Linq; | ||
| using Know_Your_Nation_Speedy.Models; | ||
|
|
||
|
|
||
| namespace Know_Your_Nation_Speedy.Models{ | ||
| public class EmailService | ||
| { | ||
| string smtpAddress = "smtp.gmail.com"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of these fields should be in the config |
||
| int portNumber = 587; | ||
| bool enableSSL = true; | ||
| string emailFromAddress = "**********"; //Sender Email Address | ||
| string password = "***********"; //Sender Password | ||
|
|
||
| public bool SendMail(string To, string Subject) | ||
| { | ||
| try | ||
| { | ||
| using (MailMessage mail = new MailMessage()) | ||
| { | ||
| MailAssignment(mail, emailFromAddress, To, Subject, "<a href = 'http://ereader.retrotest.co.za/resetPassword'>Reset Password</h1>"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't use the generated code here? |
||
| SmtpSend(mail); | ||
| } | ||
| } | ||
| catch(Exception e) | ||
| { | ||
| Console.WriteLine(e.Message); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public void MailAssignment(MailMessage mailMessage, string From, string To, string Subject, string Body) | ||
| { | ||
| mailMessage.From = new MailAddress(From); | ||
| mailMessage.To.Add(To); | ||
| mailMessage.Subject = Subject; | ||
| mailMessage.IsBodyHtml = true; | ||
| mailMessage.Body = Body; | ||
| } | ||
|
|
||
| public void SmtpSend(MailMessage mail) | ||
| { | ||
| SmtpClient smtp = new SmtpClient(smtpAddress, portNumber); | ||
| smtp.Credentials = new NetworkCredential(emailFromAddress, password); | ||
| smtp.EnableSsl = enableSSL; | ||
| smtp.Send(mail); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WS