From 7bd1c6e1153fb37b5abafc79496893c3df5e15bf Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Sat, 24 Dec 2022 08:15:39 -0500 Subject: [PATCH] Check username taken --- server/server.go | 1 - web/src/app/Api.js | 10 ++++++++++ web/src/components/Signup.js | 6 ++++-- web/src/components/SiteLayout.js | 4 ++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/server/server.go b/server/server.go index 2484ba5..1c4ee59 100644 --- a/server/server.go +++ b/server/server.go @@ -47,7 +47,6 @@ import ( purge accounts that were not logged into in X sync subscription display name store users - signup: check unique user Pages: - Home - Password reset diff --git a/web/src/app/Api.js b/web/src/app/Api.js index 381b198..b3b383c 100644 --- a/web/src/app/Api.js +++ b/web/src/app/Api.js @@ -160,6 +160,9 @@ class Api { method: "POST", body: body }); + if (response.status === 409) { + throw new UsernameTakenError(username) + } if (response.status !== 200) { throw new Error(`Unexpected server response ${response.status}`); } @@ -250,5 +253,12 @@ class Api { } } +export class UsernameTakenError extends Error { + constructor(username) { + super(); + this.username = username; + } +} + const api = new Api(); export default api; diff --git a/web/src/components/Signup.js b/web/src/components/Signup.js index eaf7ef0..5956ed3 100644 --- a/web/src/components/Signup.js +++ b/web/src/components/Signup.js @@ -2,7 +2,7 @@ import * as React from 'react'; import TextField from "@mui/material/TextField"; import Button from "@mui/material/Button"; import Box from "@mui/material/Box"; -import api from "../app/Api"; +import api, {UsernameTakenError} from "../app/Api"; import routes from "./routes"; import session from "../app/Session"; import Typography from "@mui/material/Typography"; @@ -34,7 +34,9 @@ const Signup = () => { } } catch (e) { console.log(`[Signup] Signup for user ${user.username} failed`, e); - if (e && e.message) { + if ((e instanceof UsernameTakenError)) { + setError(t("Username {{username}} is already taken", { username: e.username })); + } else if (e.message) { setError(e.message); } else { setError(t("Unknown error. Check logs for details.")) diff --git a/web/src/components/SiteLayout.js b/web/src/components/SiteLayout.js index d6e050c..fd3fa88 100644 --- a/web/src/components/SiteLayout.js +++ b/web/src/components/SiteLayout.js @@ -14,8 +14,8 @@ const SiteLayout = (props) => {
  • Features
  • Pricing
  • Docs
  • - {!session.exists() &&
  • Sign up
  • } - {!session.exists() &&
  • Login
  • } + {config.enableSignup && !session.exists() &&
  • Sign up
  • } + {config.enableLogin && !session.exists() &&
  • Login
  • }
  • Open app