2023-04-06 10:36:11 +05:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
2023-04-09 10:41:21 +05:00
|
|
|
"errors"
|
2023-04-06 10:36:11 +05:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/ordinary-dev/phoenix/backend"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ShowRegistrationForm(c *gin.Context) {
|
|
|
|
c.HTML(http.StatusOK, "auth.html.tmpl", gin.H{
|
|
|
|
"title": "Create an account",
|
|
|
|
"description": "To prevent other people from seeing your links, create an account.",
|
|
|
|
"button": "Create",
|
|
|
|
"formAction": "/users",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowLoginForm(c *gin.Context) {
|
|
|
|
c.HTML(http.StatusOK, "auth.html.tmpl", gin.H{
|
|
|
|
"title": "Sign in",
|
|
|
|
"description": "Authorization is required to view this page.",
|
|
|
|
"button": "Sign in",
|
|
|
|
"formAction": "/signin",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requires the user to log in before viewing the page.
|
|
|
|
// In case of an error, it shows the login page or the error page.
|
2023-04-09 10:41:21 +05:00
|
|
|
// Returns error if the user is not authorized.
|
|
|
|
// If `nil` is returned instead of an error, it is safe to display protected content.
|
|
|
|
func RequireAuth(c *gin.Context, db *gorm.DB) error {
|
2023-04-06 10:36:11 +05:00
|
|
|
number_of_accounts := backend.CountAdmins(db)
|
|
|
|
|
|
|
|
// First run
|
|
|
|
if number_of_accounts == 0 {
|
|
|
|
ShowRegistrationForm(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenValue, err := c.Cookie("phoenix-token")
|
|
|
|
|
|
|
|
// Anonymous visitor
|
|
|
|
if err != nil {
|
|
|
|
ShowLoginForm(c)
|
2023-04-09 10:41:21 +05:00
|
|
|
return errors.New("User is not authorized")
|
2023-04-06 10:36:11 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = backend.ValidateToken(db, tokenValue)
|
|
|
|
if err != nil {
|
|
|
|
ShowError(c, err)
|
2023-04-09 10:41:21 +05:00
|
|
|
return errors.New("Access token is invalid")
|
2023-04-06 10:36:11 +05:00
|
|
|
}
|
2023-04-09 10:41:21 +05:00
|
|
|
|
|
|
|
return nil
|
2023-04-06 10:36:11 +05:00
|
|
|
}
|