diff --git a/api/auth.go b/api/auth.go index da446dde..d939d260 100644 --- a/api/auth.go +++ b/api/auth.go @@ -39,7 +39,7 @@ func handleUserSignUp(w http.ResponseWriter, r *http.Request) { return } - user, err := store.CreateNewUser(userSignup.Username, userSignup.Password, "", "") + user, err := store.CreateNewUser(userSignup.Username, userSignup.Password, "") if err != nil { e.ErrorHandler(w, "DATABASE_ERROR", err.Error()) @@ -225,7 +225,7 @@ func handleGithubAuthCallback(w http.ResponseWriter, r *http.Request) { username = githubUser.Name + utils.GenUUID() usernameUsable, _ = store.CheckUsernameUsable(username) } - user, _ = store.CreateNewUser(username, username, githubUser.Login, "") + user, _ = store.CreateNewUser(username, username, githubUser.Login) } session.Values["user_id"] = user.Id @@ -237,6 +237,8 @@ func handleGithubAuthCallback(w http.ResponseWriter, r *http.Request) { func RegisterAuthRoutes(r *mux.Router) { authRouter := r.PathPrefix("/api/auth").Subrouter() + authRouter.Use(JSONResponseMiddleWare) + authRouter.HandleFunc("/signup", handleUserSignUp).Methods("POST") authRouter.HandleFunc("/signin", handleUserSignIn).Methods("POST") authRouter.HandleFunc("/signout", handleUserSignOut).Methods("POST") diff --git a/api/memo.go b/api/memo.go index 09be5d49..a3793f66 100644 --- a/api/memo.go +++ b/api/memo.go @@ -105,6 +105,7 @@ func handleDeleteMemo(w http.ResponseWriter, r *http.Request) { func RegisterMemoRoutes(r *mux.Router) { memoRouter := r.PathPrefix("/api/memo").Subrouter() + memoRouter.Use(JSONResponseMiddleWare) memoRouter.Use(AuthCheckerMiddleWare) memoRouter.HandleFunc("/all", handleGetMyMemos).Methods("GET") diff --git a/api/middlewares.go b/api/middlewares.go index 270b6119..f36d1497 100644 --- a/api/middlewares.go +++ b/api/middlewares.go @@ -18,6 +18,14 @@ func AuthCheckerMiddleWare(next http.Handler) http.Handler { }) } +func JSONResponseMiddleWare(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + next.ServeHTTP(w, r) + }) +} + func CorsMiddleWare(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") diff --git a/api/query.go b/api/query.go index 546dd6ef..5b2cfb77 100644 --- a/api/query.go +++ b/api/query.go @@ -103,6 +103,7 @@ func handleDeleteQuery(w http.ResponseWriter, r *http.Request) { func RegisterQueryRoutes(r *mux.Router) { queryRouter := r.PathPrefix("/api/query").Subrouter() + queryRouter.Use(JSONResponseMiddleWare) queryRouter.Use(AuthCheckerMiddleWare) queryRouter.HandleFunc("/all", handleGetMyQueries).Methods("GET") diff --git a/api/user.go b/api/user.go index 1018ebd2..a5757e2f 100644 --- a/api/user.go +++ b/api/user.go @@ -61,18 +61,6 @@ func handleUpdateMyUserInfo(w http.ResponseWriter, r *http.Request) { } } - if userPatch.WxOpenId != nil { - 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 { @@ -145,6 +133,7 @@ func handleValidPassword(w http.ResponseWriter, r *http.Request) { func RegisterUserRoutes(r *mux.Router) { userRouter := r.PathPrefix("/api/user").Subrouter() + userRouter.Use(JSONResponseMiddleWare) userRouter.Use(AuthCheckerMiddleWare) userRouter.HandleFunc("/me", handleGetMyUserInfo).Methods("GET") diff --git a/resources/initial_db.sql b/resources/initial_db.sql index 5c3262c9..ce3e168e 100644 --- a/resources/initial_db.sql +++ b/resources/initial_db.sql @@ -1,44 +1,46 @@ +DROP TABLE IF EXISTS `memos`; DROP TABLE IF EXISTS `queries`; +DROP TABLE IF EXISTS `users`; + +CREATE TABLE `users` ( + `id` TEXT NOT NULL PRIMARY KEY, + `username` TEXT NOT NULL, + `password` TEXT NOT NULL, + `github_name` TEXT NOT NULL DEFAULT '', + `created_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), + `updated_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), + UNIQUE(`username`, `github_name`) +); + CREATE TABLE `queries` ( `id` TEXT NOT NULL PRIMARY KEY, `user_id` TEXT NOT NULL, `title` TEXT NOT NULL, `querystring` TEXT NOT NULL, - `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - `pinned_at` TEXT DEFAULT '', + `created_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), + `updated_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), + `pinned_at` TEXT NOT NULL DEFAULT '', FOREIGN KEY(`user_id`) REFERENCES `users`(`id`) ); -DROP TABLE IF EXISTS `memos`; CREATE TABLE `memos` ( `id` TEXT NOT NULL PRIMARY KEY, `content` TEXT NOT NULL, `user_id` TEXT NOT NULL, - `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - `deleted_at` TEXT DEFAULT '', + `created_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), + `updated_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), + `deleted_at` TEXT NOT NULL DEFAULT '', FOREIGN KEY(`user_id`) REFERENCES `users`(`id`) ); -DROP TABLE IF EXISTS `users`; -CREATE TABLE `users` ( - `id` TEXT NOT NULL PRIMARY KEY, - `username` TEXT NOT NULL, - `password` TEXT NOT NULL, - `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, - UNIQUE(`username`, `github_name`, `wx_open_id`) -); - INSERT INTO `users` (`id`, `username`, `password`) VALUES - ('1', 'guest', '123456'); + ('1', 'guest', '123456'), + ('2', 'test', '123456'); INSERT INTO `memos` (`id`, `content`, `user_id`) VALUES - ('1', '👋 Welcome to memos', '1'); + ('1', '👋 Welcome to memos', '1'), + ('2', '👋 Welcome to memos', '2'); diff --git a/server/main.go b/server/main.go index 69e90356..ddcca0fa 100644 --- a/server/main.go +++ b/server/main.go @@ -18,12 +18,12 @@ func main() { api.RegisterMemoRoutes(r) api.RegisterQueryRoutes(r) - spa := api.SPAHandler{ + webServe := api.SPAHandler{ StaticPath: "./web/dist", IndexPath: "index.html", } - r.PathPrefix("/").Handler(spa) + r.PathPrefix("/").Handler(webServe) http.ListenAndServe(":8080", r) } diff --git a/store/user.go b/store/user.go index cf5b6aba..8b876bbc 100644 --- a/store/user.go +++ b/store/user.go @@ -11,26 +11,24 @@ type User struct { Id string `json:"id"` Username string `json:"username"` Password string `json:"password"` - WxOpenId string `json:"wxOpenId"` GithubName string `json:"githubName"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` } -func CreateNewUser(username string, password string, githubName string, wxOpenId string) (User, error) { +func CreateNewUser(username string, password string, githubName string) (User, error) { nowDateTimeStr := utils.GetNowDateTimeStr() newUser := User{ Id: utils.GenUUID(), Username: username, Password: password, - WxOpenId: wxOpenId, GithubName: githubName, CreatedAt: nowDateTimeStr, UpdatedAt: nowDateTimeStr, } - query := `INSERT INTO users (id, username, password, wx_open_id, github_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)` - _, err := DB.Exec(query, newUser.Id, newUser.Username, newUser.Password, newUser.WxOpenId, newUser.GithubName, newUser.CreatedAt, newUser.UpdatedAt) + query := `INSERT INTO users (id, username, password, github_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)` + _, err := DB.Exec(query, newUser.Id, newUser.Username, newUser.Password, newUser.GithubName, newUser.CreatedAt, newUser.UpdatedAt) return newUser, err } @@ -39,7 +37,6 @@ type UserPatch struct { Username *string Password *string GithubName *string - WxOpenId *string } func UpdateUser(id string, userPatch *UserPatch) (User, error) { @@ -58,10 +55,6 @@ func UpdateUser(id string, userPatch *UserPatch) (User, error) { user.GithubName = *v set, args = append(set, "github_name=?"), append(args, *v) } - if v := userPatch.WxOpenId; v != nil { - user.WxOpenId = *v - set, args = append(set, "wx_open_id=?"), append(args, *v) - } set, args = append(set, "updated_at=?"), append(args, utils.GetNowDateTimeStr()) args = append(args, id) @@ -72,30 +65,23 @@ func UpdateUser(id string, userPatch *UserPatch) (User, error) { } func GetUserById(id string) (User, error) { - query := `SELECT id, username, password, wx_open_id, github_name, created_at, updated_at FROM users WHERE id=?` + query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE id=?` user := User{} - err := DB.QueryRow(query, id).Scan(&user.Id, &user.Username, &user.Password, &user.WxOpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, id).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err } func GetUserByUsernameAndPassword(username string, password string) (User, error) { - query := `SELECT id, username, password, wx_open_id, github_name, created_at, updated_at FROM users WHERE username=? AND password=?` + query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE username=? AND password=?` user := User{} - err := DB.QueryRow(query, username, password).Scan(&user.Id, &user.Username, &user.Password, &user.WxOpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, username, password).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err } func GetUserByGithubName(githubName string) (User, error) { - query := `SELECT id, username, password, wx_open_id, github_name, created_at, updated_at FROM users WHERE github_name=?` + query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE github_name=?` user := User{} - err := DB.QueryRow(query, githubName).Scan(&user.Id, &user.Username, &user.Password, &user.WxOpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) - return user, err -} - -func GetUserByWxOpenId(wxOpenId string) (User, error) { - query := `SELECT id, username, password, wx_open_id, github_name, created_at, updated_at FROM users WHERE id=?` - user := User{} - err := DB.QueryRow(query, wxOpenId).Scan(&user.Id, &user.Username, &user.Password, &user.WxOpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, githubName).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err } @@ -133,23 +119,6 @@ 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/components/BindWxOpenIdDialog.tsx b/web/src/components/BindWxOpenIdDialog.tsx deleted file mode 100644 index 842e0528..00000000 --- a/web/src/components/BindWxOpenIdDialog.tsx +++ /dev/null @@ -1,79 +0,0 @@ -import { useEffect, useState } from "react"; -import { userService } from "../services"; -import { showDialog } from "./Dialog"; -import toastHelper from "./Toast"; -import "../less/change-password-dialog.less"; - -interface Props extends DialogProps {} - -const BindWxOpenIdDialog: React.FC = ({ destroy }: Props) => { - const [wxOpenId, setWxOpenId] = useState(""); - - useEffect(() => { - // do nth - }, []); - - const handleCloseBtnClick = () => { - destroy(); - }; - - const handleWxOpenIdChanged = (e: React.ChangeEvent) => { - const text = e.target.value as string; - setWxOpenId(text); - }; - - const handleSaveBtnClick = async () => { - if (wxOpenId === "") { - toastHelper.error("微信 id 不能为空"); - return; - } - - try { - await userService.updateWxOpenId(wxOpenId); - userService.doSignIn(); - toastHelper.info("绑定成功!"); - handleCloseBtnClick(); - } catch (error: any) { - toastHelper.error(error); - } - }; - - return ( - <> -
-

绑定微信 OpenID

- -
-
-

- 关注微信公众号“小谈闲事”,主动发送任意消息,即可获取 OpenID 。 -

- -
- - 取消 - - - 保存 - -
-
- - ); -}; - -function showBindWxOpenIdDialog() { - showDialog( - { - className: "bind-wxid-dialog", - }, - BindWxOpenIdDialog - ); -} - -export default showBindWxOpenIdDialog; diff --git a/web/src/components/MyAccountSection.tsx b/web/src/components/MyAccountSection.tsx index fdd7c5d7..e9ef13b1 100644 --- a/web/src/components/MyAccountSection.tsx +++ b/web/src/components/MyAccountSection.tsx @@ -6,7 +6,6 @@ import { validate, ValidatorConfig } from "../helpers/validator"; import Only from "./common/OnlyWhen"; import toastHelper from "./Toast"; import showChangePasswordDialog from "./ChangePasswordDialog"; -import showBindWxOpenIdDialog from "./BindWxOpenIdDialog"; import "../less/my-account-section.less"; const validateConfig: ValidatorConfig = { @@ -88,20 +87,6 @@ const MyAccountSection: React.FC = () => { } }; - const handleUnbindWxBtnClick = async () => { - if (showConfirmUnbindWxBtn) { - try { - await userService.updateWxOpenId(""); - await userService.doSignIn(); - } catch (error: any) { - toastHelper.error(error.message); - } - setShowConfirmUnbindWxBtn(false); - } else { - setShowConfirmUnbindWxBtn(true); - } - }; - const handlePreventDefault = (e: React.MouseEvent) => { e.preventDefault(); }; @@ -155,33 +140,6 @@ const MyAccountSection: React.FC = () => {

关联账号

-