Skip to content

Commit

Permalink
Added role to user
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebQ42 committed Jan 5, 2024
1 parent 059197b commit 5a616ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ Request Body:
{
// Must not be empty or just spaces and cannot end or begin with spaces.
// Must be less then 64 characters.
// Must be unique.
// Spaces will be trimmed.
"username": "username",
// Passwords must be between 5-32 characters.
"password": "password",
// Email must be unique.
"email": "email"
}
```
Expand All @@ -112,7 +114,7 @@ Response:
"token": "jwt token",
// Only populated if there's some problem with creating the user.
// username - Username is already taken or invalid.
// email - email is (probably) invalid.
// email - email is already taken or (probably) invalid.
// password - Password is invalid.
"problem": "username"
}
Expand Down
11 changes: 11 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ func (s *Stupid) createUser(r *Request) {
writeCreateUserProblemResp("username", r.Resp)
return
}
emailNotAvailable, err := s.users.Contains(map[string]any{"email": email})
if err != nil {
fmt.Printf("error while finding if email is already taken: %s", err)
r.Resp.WriteHeader(http.StatusInternalServerError)
return
}
if emailNotAvailable {
writeCreateUserProblemResp("email", r.Resp)
return
}
// TODO: check email properly
if !strings.Contains(email, "@") && !strings.Contains(email, ".") {
writeCreateUserProblemResp("email", r.Resp)
Expand All @@ -133,6 +143,7 @@ func (s *Stupid) createUser(r *Request) {
ID: uuid.NewString(),
Username: name,
Email: email,
Role: "user",
}
newUser.Salt, err = generateSalt()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type user struct {
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
Salt string `json:"salt" bson:"salt"`
Role string `json:"role" bson:"role"`
LastTimeout int64 `json:"lastTimeout" bson:"lastTimeout"`
Failed int `json:"failed" bson:"failed"`
}

0 comments on commit 5a616ea

Please sign in to comment.