feat(api): add PATCH /users/@me/fields, finish POST /auth/discord/callback

This commit is contained in:
Sam 2022-05-17 22:35:26 +02:00
parent 020ac15a00
commit 52a03b4aa6
9 changed files with 261 additions and 17 deletions

View file

@ -27,7 +27,7 @@ type User struct {
}
// usernames must match this regex
var usernameRegex = regexp.MustCompile(`[\w-.]{2,40}`)
var usernameRegex = regexp.MustCompile(`^[\w-.]{2,40}$`)
const (
ErrUserNotFound = errors.Sentinel("user not found")
@ -136,3 +136,13 @@ func (db *DB) Username(ctx context.Context, name string) (u User, err error) {
return u, nil
}
// UsernameTaken checks if the given username is already taken.
func (db *DB) UsernameTaken(ctx context.Context, username string) (valid, taken bool, err error) {
if !usernameRegex.MatchString(username) {
return false, false, nil
}
err = db.QueryRow(ctx, "select exists (select id from users where username = $1)", username).Scan(&taken)
return true, taken, err
}