-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from SCCapstone/142
- Loading branch information
Showing
4 changed files
with
254 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"log" | ||
"math/rand" | ||
"net/http" | ||
"net/smtp" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
@@ -157,6 +158,14 @@ func InitializeRoutes() { | |
userRoutes.POST("/edit_device", middleware.EnsureLoggedIn(), editDevice) | ||
|
||
userRoutes.POST("/postCoordinates", middleware.EnsureLoggedIn(), changeDeviceCoordinates) | ||
|
||
userRoutes.GET("/forgot-password", middleware.EnsureNotLoggedIn(), showForgotPassword) | ||
|
||
userRoutes.POST("/forgot-password", middleware.EnsureNotLoggedIn(), performForgotPassword) | ||
|
||
userRoutes.GET("/reset-password", middleware.EnsureNotLoggedIn(), showResetPassword) | ||
|
||
userRoutes.POST("/reset-password", middleware.EnsureNotLoggedIn(), performResetPassword) | ||
} | ||
// Handle GET requests at /map, ensure user is logged in using middleware | ||
// Render the index page | ||
|
@@ -764,3 +773,94 @@ func getCurrentDevice() (deviceName string) { | |
return currentDevice | ||
} | ||
|
||
/* | ||
Renders forgot password page | ||
*/ | ||
func showForgotPassword(c *gin.Context) { | ||
Render(c, gin.H{ | ||
"title": "Forgot Password"}, "forgot-password.html") | ||
} | ||
|
||
/* | ||
Renders reset password page | ||
*/ | ||
func showResetPassword(c *gin.Context) { | ||
Render(c, gin.H{ | ||
"title": "Reset Password"}, "reset-password.html") | ||
} | ||
|
||
/* | ||
Checks if inputted email is in database | ||
If yes, returned to login page | ||
If no, renders error | ||
*/ | ||
func performForgotPassword(c *gin.Context) { | ||
username := c.PostForm("username") | ||
email := c.PostForm("email") | ||
if err := db.CheckUsername(username); err == nil { | ||
c.HTML(http.StatusBadRequest, "forgot-password.html", gin.H{ | ||
"title": "Forgot Password", | ||
"Email": email, | ||
"ErrorTitle": "Invalid Username", | ||
"ErrorMessage": "Username not connected to user."}) | ||
} else if err := db.CheckEmailValid(email); err != nil { | ||
c.HTML(http.StatusBadRequest, "forgot-password.html", gin.H{ | ||
"title": "Forgot Password", | ||
"Username": username, | ||
"ErrorTitle": "Invalid Email Address", | ||
"ErrorMessage": "Please type a valid email address."}) | ||
} else if err := db.CheckEmail(email); err != nil { | ||
from := "[email protected]" | ||
password := "gydhmmllmtsfjxal" | ||
to := []string{email} | ||
smtpHost := "smtp.gmail.com" | ||
smtpPort := "587" | ||
resetCode := db.GenerateResetCode(username) | ||
message := []byte("Subject: Reset Code\n\nHere is your reset password code: " + resetCode) | ||
auth := smtp.PlainAuth("", from, password, smtpHost) | ||
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message) | ||
if err != nil { | ||
c.HTML(http.StatusBadRequest, "forgot-password.html", gin.H{ | ||
"title": "Forgot Password", | ||
"Username": username, | ||
"Email": email, | ||
"ErrorTitle": "Failed to Send Email", | ||
"ErrorMessage": err.Error()}) | ||
} else { | ||
showResetPassword(c) | ||
} | ||
} else { | ||
c.HTML(http.StatusBadRequest, "forgot-password.html", gin.H{ | ||
"title": "Forgot Password", | ||
"ErrorTitle": "Invalid Email Address", | ||
"ErrorMessage": "Email not connected to a user."}) | ||
} | ||
} | ||
|
||
/* | ||
Checks if password is valid | ||
If yes, updates password | ||
If not, renders error | ||
*/ | ||
func performResetPassword(c *gin.Context) { | ||
reset_code := c.PostForm("reset-code") | ||
username := c.PostForm("username") | ||
password := c.PostForm("password") | ||
confirm_password := c.PostForm("confirm_password") | ||
|
||
if err := db.CheckResetCode(reset_code, username); err != nil { | ||
c.HTML(http.StatusBadRequest, "reset-password.html", gin.H{ | ||
"ErrorTitle": "Reset Password Failed", | ||
"ErrorMessage": err.Error()}) | ||
} else if password != confirm_password { | ||
c.HTML(http.StatusBadRequest, "reset-password.html", gin.H{ | ||
"ErrorTitle": "Reset Password Failed", | ||
"ErrorMessage": fmt.Sprintf("Passwords \"%s\" and \"%s\" do not match.", password, confirm_password)}) | ||
} else if err := db.ResetPassword(username, password); err != nil { | ||
c.HTML(http.StatusBadRequest, "reset-password.html", gin.H{ | ||
"ErrorTitle": "Reset Password Failed", | ||
"ErrorMessage": err.Error()}) | ||
} else { | ||
showLoginPage(c) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!--forgot-password.html--> | ||
|
||
<!--Embed the header.html template at this location--> | ||
{{ template "header.html" .}} | ||
|
||
<head> | ||
<link rel="stylesheet" href="/static/css/style.css"> | ||
</head> | ||
|
||
<article class="center-page"> | ||
<h1>Forgot Your Password?</h1> | ||
<p>If you have forgotten your password, please enter your account's username and email address below and click the "Reset Password" button. You will receive an email with a reset password code and be redirected to the reset password page.</p> | ||
<form action="/u/forgot-password" method="POST"> | ||
<label for="username">Username:</label><br> | ||
<input type="text" id="username" name="username" value = "{{ .Username}}"><br> | ||
<label for="email">Email:</label><br> | ||
<input type="text" id="email" name="email" value = "{{ .Email}}"><br> | ||
{{ if .ErrorTitle}} | ||
<div class="error"> | ||
{{.ErrorTitle}}: {{.ErrorMessage}} | ||
</div> | ||
{{end}} | ||
<div class="absolute"> | ||
<input class="submit-btn" type="submit" value="Reset Password"> | ||
</div> | ||
</form> | ||
<form action="/u/login" method="GET"> | ||
<p style = "margin-bottom: 1px">Return to Login Page <input class="signup moveup" type="submit" value="Login"></p> | ||
</form> | ||
<form action="/u/register" method="GET"> | ||
<p style = "margin-top: 1px">Don't have an account? <input class="signup moveup" type="submit" value="Sign-up"></p> | ||
</form> | ||
</article> | ||
|
||
<!--Embed the footer.html template at this location--> | ||
{{ template "footer.html" .}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!--reset-password.html--> | ||
|
||
<!--Embed the header.html template at this location--> | ||
{{ template "header.html" .}} | ||
|
||
<head> | ||
<link rel="stylesheet" href="/static/css/style.css"> | ||
</head> | ||
|
||
<article class="center-page"> | ||
<h1>Reset Password</h1> | ||
<p style = "margin-top: 5px">Please type in the reset password code, your username, and your new password below. <br><br> Remember that passwords must </p> | ||
<ul style = "color: white"> | ||
<li> have at least 10 characters </li> | ||
<li> have at least 1 digit</li> | ||
<li> have at least 1 symbol</li> | ||
<li> have at least 1 uppercase character</li> | ||
<li> have at least 1 lowercase character</li> | ||
</ul> | ||
<form class="form" action="/u/reset-password" method="POST"> | ||
<label for="reset-code">Reset Code</label><br> | ||
<input type="text" name="reset-code"id="reset-code"><br> | ||
<label for="username">Username</label><br> | ||
<input type="text" name="username"id="username"><br> | ||
<label for="password">Password</label><br> | ||
<input type="password" name="password"id="password"><br> | ||
<label for="confirm_password">Confirm Password</label><br> | ||
<input type="password" name="confirm_password"id="confirm_password"><br> | ||
{{ if .ErrorTitle}} | ||
<div class="error"> | ||
{{.ErrorTitle}}: {{.ErrorMessage}} | ||
</div> | ||
{{end}} | ||
<div class="absolute"> | ||
<input class="submit-btn" type="submit" value="Submit"> | ||
</div> | ||
<br><br> | ||
</form> | ||
</article> | ||
|
||
<!--Embed the footer.html template at this location--> | ||
{{ template "footer.html" .}} |