diff --git a/api/user.go b/api/user.go index a5757e2f..a4db8b3e 100644 --- a/api/user.go +++ b/api/user.go @@ -75,6 +75,23 @@ func handleUpdateMyUserInfo(w http.ResponseWriter, r *http.Request) { }) } +func handleRefreshUserOpenId(w http.ResponseWriter, r *http.Request) { + userId, _ := GetUserIdInSession(r) + + openId, err := store.UpdateUserOpenId(userId) + + if err != nil { + e.ErrorHandler(w, "DATABASE_ERROR", err.Error()) + return + } + + json.NewEncoder(w).Encode(Response{ + Succeed: true, + Message: "", + Data: openId, + }) +} + type CheckUsername struct { Username string } @@ -138,6 +155,7 @@ func RegisterUserRoutes(r *mux.Router) { userRouter.HandleFunc("/me", handleGetMyUserInfo).Methods("GET") userRouter.HandleFunc("/me", handleUpdateMyUserInfo).Methods("PATCH") + userRouter.HandleFunc("/open_id/new", handleRefreshUserOpenId).Methods("POST") userRouter.HandleFunc("/checkusername", handleCheckUsername).Methods("POST") userRouter.HandleFunc("/validpassword", handleValidPassword).Methods("POST") } diff --git a/resources/initial_db.sql b/resources/initial_db.sql index 3ed854d5..b62eb404 100644 --- a/resources/initial_db.sql +++ b/resources/initial_db.sql @@ -7,6 +7,7 @@ CREATE TABLE `users` ( `id` TEXT NOT NULL PRIMARY KEY, `username` TEXT NOT NULL, `password` TEXT NOT NULL, + `open_id` TEXT NOT NULL DEFAULT '', `github_name` TEXT NOT NULL DEFAULT '', `created_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), `updated_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), diff --git a/resources/memos.db b/resources/memos.db index f3240385..dcd93ca8 100644 Binary files a/resources/memos.db and b/resources/memos.db differ diff --git a/store/user.go b/store/user.go index 8b876bbc..eaa4f8e4 100644 --- a/store/user.go +++ b/store/user.go @@ -11,6 +11,7 @@ type User struct { Id string `json:"id"` Username string `json:"username"` Password string `json:"password"` + OpenId string `json:"openId"` GithubName string `json:"githubName"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` @@ -22,13 +23,14 @@ func CreateNewUser(username string, password string, githubName string) (User, e Id: utils.GenUUID(), Username: username, Password: password, + OpenId: utils.GenUUID(), GithubName: githubName, CreatedAt: nowDateTimeStr, UpdatedAt: nowDateTimeStr, } - 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) + query := `INSERT INTO users (id, username, password, open_id, github_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)` + _, err := DB.Exec(query, newUser.Id, newUser.Username, newUser.Password, newUser.OpenId, newUser.GithubName, newUser.CreatedAt, newUser.UpdatedAt) return newUser, err } @@ -64,24 +66,33 @@ func UpdateUser(id string, userPatch *UserPatch) (User, error) { return user, err } +func UpdateUserOpenId(userId string) (string, error) { + openId := utils.GenUUID() + + query := `UPDATE users SET open_id=? WHERE id=?` + _, err := DB.Exec(query, openId, userId) + + return openId, err +} + func GetUserById(id string) (User, error) { - query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE id=?` + query := `SELECT id, username, password, open_id, 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.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, id).Scan(&user.Id, &user.Username, &user.Password, &user.OpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err } func GetUserByUsernameAndPassword(username string, password string) (User, error) { - query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE username=? AND password=?` + query := `SELECT id, username, password, open_id, 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.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, username, password).Scan(&user.Id, &user.Username, &user.Password, &user.OpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err } func GetUserByGithubName(githubName string) (User, error) { - query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE github_name=?` + query := `SELECT id, username, password, open_id, 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.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, githubName).Scan(&user.Id, &user.Username, &user.Password, &user.OpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err }