refactor: rename email back to username

This commit is contained in:
Stavros
2025-01-26 18:29:30 +02:00
parent 708006decf
commit 989ea8f229
12 changed files with 58 additions and 58 deletions

View File

@@ -142,7 +142,7 @@ func (api *API) SetupRoutes() {
return
}
user := api.Auth.GetUser(login.Email)
user := api.Auth.GetUser(login.Username)
if user == nil {
c.JSON(401, gin.H{
@@ -161,7 +161,7 @@ func (api *API) SetupRoutes() {
}
session := sessions.Default(c)
session.Set("tinyauth_sid", fmt.Sprintf("email:%s", login.Email))
session.Set("tinyauth_sid", fmt.Sprintf("username:%s", login.Username))
session.Save()
c.JSON(200, gin.H{
@@ -199,7 +199,7 @@ func (api *API) SetupRoutes() {
c.JSON(200, gin.H{
"status": 200,
"message": "Unauthenticated",
"email": "",
"username": "",
"isLoggedIn": false,
"oauth": false,
"provider": "",
@@ -212,7 +212,7 @@ func (api *API) SetupRoutes() {
c.JSON(200, gin.H{
"status": 200,
"message": "Authenticated",
"email": userContext.Email,
"username": userContext.Username,
"isLoggedIn": userContext.IsLoggedIn,
"oauth": userContext.OAuth,
"provider": userContext.Provider,
@@ -306,7 +306,7 @@ func (api *API) SetupRoutes() {
if !api.Auth.EmailWhitelisted(email) {
log.Warn().Str("email", email).Msg("Email not whitelisted")
unauthorizedQuery, unauthorizedQueryErr := query.Values(types.UnauthorizedQuery{
Email: email,
Username: email,
})
if handleApiError(c, "Failed to build query", unauthorizedQueryErr) {
return

View File

@@ -18,9 +18,9 @@ type Auth struct {
OAuthWhitelist []string
}
func (auth *Auth) GetUser(email string) *types.User {
func (auth *Auth) GetUser(username string) *types.User {
for _, user := range auth.Users {
if user.Email == email {
if user.Username == username {
return &user
}
}
@@ -28,7 +28,7 @@ func (auth *Auth) GetUser(email string) *types.User {
}
func (auth *Auth) CheckPassword(user types.User, password string) bool {
hashedPasswordErr := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
hashedPasswordErr := bcrypt.CompareHashAndPassword([]byte(user.Username), []byte(password))
return hashedPasswordErr == nil
}

View File

@@ -28,7 +28,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) (types.UserContext, error) {
if sessionCookie == nil {
return types.UserContext{
Email: "",
Username: "",
IsLoggedIn: false,
OAuth: false,
Provider: "",
@@ -39,7 +39,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) (types.UserContext, error) {
if !dataOk {
return types.UserContext{
Email: "",
Username: "",
IsLoggedIn: false,
OAuth: false,
Provider: "",
@@ -50,7 +50,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) (types.UserContext, error) {
if len(split) != 2 {
return types.UserContext{
Email: "",
Username: "",
IsLoggedIn: false,
OAuth: false,
Provider: "",
@@ -60,18 +60,18 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) (types.UserContext, error) {
sessionType := split[0]
sessionValue := split[1]
if sessionType == "email" {
if sessionType == "username" {
user := hooks.Auth.GetUser(sessionValue)
if user == nil {
return types.UserContext{
Email: "",
Username: "",
IsLoggedIn: false,
OAuth: false,
Provider: "",
}, nil
}
return types.UserContext{
Email: sessionValue,
Username: sessionValue,
IsLoggedIn: true,
OAuth: false,
Provider: "",
@@ -82,7 +82,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) (types.UserContext, error) {
if provider == nil {
return types.UserContext{
Email: "",
Username: "",
IsLoggedIn: false,
OAuth: false,
Provider: "",
@@ -93,7 +93,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) (types.UserContext, error) {
session.Delete("tinyauth_sid")
session.Save()
return types.UserContext{
Email: "",
Username: "",
IsLoggedIn: false,
OAuth: false,
Provider: "",
@@ -101,7 +101,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) (types.UserContext, error) {
}
return types.UserContext{
Email: sessionValue,
Username: sessionValue,
IsLoggedIn: true,
OAuth: true,
Provider: sessionType,

View File

@@ -7,12 +7,12 @@ type LoginQuery struct {
}
type LoginRequest struct {
Email string `json:"email"`
Username string `json:"username"`
Password string `json:"password"`
}
type User struct {
Email string
Username string
Password string
}
@@ -42,7 +42,7 @@ type Config struct {
}
type UserContext struct {
Email string
Username string
IsLoggedIn bool
OAuth bool
Provider string
@@ -83,5 +83,5 @@ type OAuthProviders struct {
}
type UnauthorizedQuery struct {
Email string `url:"email"`
Username string `url:"username"`
}

View File

@@ -22,7 +22,7 @@ func ParseUsers(users string) (types.Users, error) {
return types.Users{}, errors.New("invalid user format")
}
usersParsed = append(usersParsed, types.User{
Email: userSplit[0],
Username: userSplit[0],
Password: userSplit[1],
})
}