From b20741cca848ae8709d3931c33c7c9747f15057f Mon Sep 17 00:00:00 2001 From: steven Date: Sun, 12 Dec 2021 14:00:25 +0800 Subject: [PATCH] update users table with unique tag --- api/auth.go | 26 ++++++++++++++++++++--- api/user.go | 36 ++++++++++++++++++++++++++++++++ resources/initial_db.sql | 3 ++- resources/memos.db | Bin 45056 -> 45056 bytes store/user.go | 17 +++++++++++++++ web/src/helpers/api.ts | 18 +++++++++------- web/src/pages/Signin.tsx | 7 +++---- web/src/services/userService.ts | 16 ++++++++++---- 8 files changed, 103 insertions(+), 20 deletions(-) diff --git a/api/auth.go b/api/auth.go index b93215bf..e2d22c46 100644 --- a/api/auth.go +++ b/api/auth.go @@ -29,6 +29,16 @@ func handleUserSignUp(w http.ResponseWriter, r *http.Request) { return } + usernameUsable, _ := store.CheckUsernameUsable(userSignup.Username) + if !usernameUsable { + json.NewEncoder(w).Encode(Response{ + Succeed: false, + Message: "Username is existed", + Data: nil, + }) + return + } + user, err := store.CreateNewUser(userSignup.Username, userSignup.Password, "", "") if err != nil { @@ -65,7 +75,16 @@ func handleUserSignIn(w http.ResponseWriter, r *http.Request) { user, err := store.GetUserByUsernameAndPassword(userSignin.Username, userSignin.Password) if err != nil { - e.ErrorHandler(w, "DATABASE_ERROR", err.Error()) + if err == sql.ErrNoRows { + json.NewEncoder(w).Encode(Response{ + Succeed: false, + Message: "Username and password not allowed", + Data: nil, + }) + } else { + e.ErrorHandler(w, "DATABASE_ERROR", err.Error()) + } + return } @@ -202,8 +221,9 @@ func handleGithubAuthCallback(w http.ResponseWriter, r *http.Request) { if err == sql.ErrNoRows { username := githubUser.Name usernameUsable, _ := store.CheckUsernameUsable(username) - if !usernameUsable { - username = username + common.GenUUID() + for !usernameUsable { + username = githubUser.Name + common.GenUUID() + usernameUsable, _ = store.CheckUsernameUsable(username) } user, _ = store.CreateNewUser(username, username, githubUser.Login, "") } diff --git a/api/user.go b/api/user.go index 61da75a9..f1032aea 100644 --- a/api/user.go +++ b/api/user.go @@ -37,6 +37,42 @@ func handleUpdateMyUserInfo(w http.ResponseWriter, r *http.Request) { return } + if *userPatch.Username != "" { + usernameUsable, _ := store.CheckUsernameUsable(*userPatch.Username) + if !usernameUsable { + json.NewEncoder(w).Encode(Response{ + Succeed: false, + Message: "Username is existed", + Data: nil, + }) + return + } + } + + if *userPatch.GithubName != "" { + githubNameUsable, _ := store.CheckGithubNameUsable(*userPatch.GithubName) + if !githubNameUsable { + json.NewEncoder(w).Encode(Response{ + Succeed: false, + Message: "GitHub name is existed", + Data: nil, + }) + return + } + } + + if *userPatch.WxOpenId != "" { + wxOpenIdUsable, _ := store.CheckWxOpenIdUsable(*userPatch.GithubName) + if !wxOpenIdUsable { + json.NewEncoder(w).Encode(Response{ + Succeed: false, + Message: "Wx open id is existed", + Data: nil, + }) + return + } + } + user, err := store.UpdateUser(userId, &userPatch) if err != nil { diff --git a/resources/initial_db.sql b/resources/initial_db.sql index ce85a4a5..5c3262c9 100644 --- a/resources/initial_db.sql +++ b/resources/initial_db.sql @@ -29,7 +29,8 @@ CREATE TABLE `users` ( `github_name` TEXT DEFAULT '', `wx_open_id` TEXT DEFAULT '', `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE(`username`, `github_name`, `wx_open_id`) ); INSERT INTO `users` diff --git a/resources/memos.db b/resources/memos.db index 5c78111ea8347dfa425033617d352eed1770a28f..9af4c3669259d650cfaba5d0bb769c368a65b532 100644 GIT binary patch delta 330 zcmZp8z|`=7X@a!iD+UGzZXjj{VrC$&n5bi{_=-Uoq?TOih)m`e;&_z-ap(@ z{8xEexOVWKVhjGg*;`0kq^Wrm85;VCqHIpX)oq>(Jf`Lz;e;&_z-ap(G z{8zblaP8nd$$N6Mpuh_D%^Dn^m?j_Kj$>&u(PW=o!L1XBAZ1Pgyyotw~kx^sv zKVD--)y+nHxr~fzlNa!tF=_#YWHx{1|Ew=%Vr6J-WoT+-U}UIkXaqzG22i0*4$tJx ra7zQ72?ESO%n8IG=W_uu2N1Ju7HoLRznRhDKaiO4Uw*T|1V#Y>ZTUJu diff --git a/store/user.go b/store/user.go index 67f77827..bbdee291 100644 --- a/store/user.go +++ b/store/user.go @@ -133,6 +133,23 @@ func CheckGithubNameUsable(githubName string) (bool, error) { } } +func CheckWxOpenIdUsable(wxOpenId string) (bool, error) { + query := `SELECT * FROM users WHERE wx_open_id=?` + query = fmt.Sprintf("SELECT COUNT(*) FROM (%s)", query) + + var count uint + err := DB.QueryRow(query, wxOpenId).Scan(&count) + if err != nil && err != sql.ErrNoRows { + return false, FormatDBError(err) + } + + if count > 0 { + return false, nil + } else { + return true, nil + } +} + func CheckPasswordValid(id string, password string) (bool, error) { query := `SELECT * FROM users WHERE id=? AND password=?` query = fmt.Sprintf("SELECT COUNT(*) FROM (%s)", query) diff --git a/web/src/helpers/api.ts b/web/src/helpers/api.ts index c5da790a..88048ffd 100644 --- a/web/src/helpers/api.ts +++ b/web/src/helpers/api.ts @@ -6,7 +6,7 @@ type ResponseType = { data: T; }; -async function request(method: string, url: string, data?: BasicType): Promise> { +async function request(method: string, url: string, data?: any): Promise> { const requestConfig: RequestInit = { method, }; @@ -55,13 +55,15 @@ namespace api { return request("POST", "/api/user/validpassword", { password }); } - export function updateUserinfo(username?: string, password?: string, githubName?: string, wxOpenId?: string) { - return request("PATCH", "/api/user/me", { - username, - password, - githubName, - wxOpenId, - }); + interface UserInfoPatch { + username?: string; + password?: string; + githubName?: string; + wxOpenId?: string; + } + + export function updateUserinfo(userinfo: UserInfoPatch) { + return request("PATCH", "/api/user/me", userinfo); } export function getMyMemos() { diff --git a/web/src/pages/Signin.tsx b/web/src/pages/Signin.tsx index 4c93d126..432eedc3 100644 --- a/web/src/pages/Signin.tsx +++ b/web/src/pages/Signin.tsx @@ -4,7 +4,6 @@ import { validate, ValidatorConfig } from "../helpers/validator"; import useLoading from "../hooks/useLoading"; import { locationService, userService } from "../services"; import Only from "../components/common/OnlyWhen"; -import showAboutSiteDialog from "../components/AboutSiteDialog"; import toastHelper from "../components/Toast"; import "../less/signin.less"; @@ -50,8 +49,8 @@ const Signin: React.FC = () => { setPassword(text); }; - const handleAboutBtnClick = () => { - showAboutSiteDialog(); + const handleSignUpBtnClick = async () => { + toastHelper.info("注册已关闭"); }; const handleSignInBtnClick = async () => { @@ -186,7 +185,7 @@ const Signin: React.FC = () => { 体验一下 / - / diff --git a/web/src/services/userService.ts b/web/src/services/userService.ts index 9c55abf7..1755bc87 100644 --- a/web/src/services/userService.ts +++ b/web/src/services/userService.ts @@ -35,11 +35,15 @@ class UserService { } public async updateUsername(username: string): Promise { - await api.updateUserinfo(username); + await api.updateUserinfo({ + username, + }); } public async removeGithubName(): Promise { - await api.updateUserinfo(undefined, undefined, ""); + await api.updateUserinfo({ + githubName: "", + }); } public async checkPasswordValid(password: string): Promise { @@ -48,11 +52,15 @@ class UserService { } public async updatePassword(password: string): Promise { - await api.updateUserinfo(undefined, password); + await api.updateUserinfo({ + password, + }); } public async updateWxOpenId(wxOpenId: string): Promise { - await api.updateUserinfo(undefined, undefined, undefined, wxOpenId); + await api.updateUserinfo({ + wxOpenId, + }); } }