feat: add open_id key to user (#10)

This commit is contained in:
STEVEN 2021-12-14 20:40:24 +08:00 committed by GitHub
parent aa40793a68
commit 5dc3d4a39e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 8 deletions

View file

@ -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")
}

View file

@ -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')),

Binary file not shown.

View file

@ -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
}