Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
update github oauth callback api
steven527745ad15b6
diff --git a/.gitignore b/.gitignoreindex 78660904e..abc64caf4 100644--- a/.gitignore+++ b/.gitignore@@ -13,6 +13,9 @@*.logtmp+# Config+config+# Air (hot reload) generated.airdiff --git a/api/auth.go b/api/auth.goindex 7d69d15f7..b93215bf4 100644--- a/api/auth.go+++ b/api/auth.go@@ -1,8 +1,14 @@package apiimport (+ "bytes"+ "database/sql""encoding/json"+ "fmt"+ "io/ioutil""memos/api/e"+ "memos/common"+ "memos/config""memos/store""net/http"@@ -88,10 +94,131 @@ func handleUserSignOut(w http.ResponseWriter, r *http.Request) {})}+func handleGithubAuthCallback(w http.ResponseWriter, r *http.Request) {+ code := r.URL.Query().Get("code")++ requestBody := map[string]string{+ "client_id": config.GITHUB_CLIENTID,+ "client_secret": config.GITHUB_SECRET,+ "code": code,+ }++ requestJSON, _ := json.Marshal(requestBody)++ // POST request to get access_token+ req, err := http.NewRequest(+ "POST",+ "https://github.com/login/oauth/access_token",+ bytes.NewBuffer(requestJSON),+ )++ if err != nil {+ e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Error in request github api")+ return+ }++ req.Header.Set("Content-Type", "application/json")+ req.Header.Set("Accept", "application/json")++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Error in request github api")+ return+ }++ // Response body converted to stringified JSON+ respBody, _ := ioutil.ReadAll(resp.Body)++ // Represents the response received from Github+ type GithubAccessTokenResponse struct {+ AccessToken string `json:"access_token"`+ TokenType string `json:"token_type"`+ Scope string `json:"scope"`+ }++ ghResp := GithubAccessTokenResponse{}+ json.Unmarshal(respBody, &ghResp)++ githubAccessToken := ghResp.AccessToken++ // Get request to a set URL+ req, err = http.NewRequest(+ "GET",+ "https://api.github.com/user",+ nil,+ )+ if err != nil {+ e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Error in request github api")+ return+ }++ authorizationHeaderValue := fmt.Sprintf("token %s", githubAccessToken)+ req.Header.Set("Authorization", authorizationHeaderValue)++ resp, err = http.DefaultClient.Do(req)++ if err != nil {+ e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Error in request github api")+ return+ }++ respBody, _ = ioutil.ReadAll(resp.Body)++ githubData := string(respBody)++ type GithubUser struct {+ Login string `json:"login"`+ Name string `json:"name"`+ }++ githubUser := GithubUser{}+ json.Unmarshal([]byte(githubData), &githubUser)++ session, _ := SessionStore.Get(r, "session")+ userId := fmt.Sprintf("%v", session.Values["user_id"])++ if userId != "" {+ githubNameUsable, err := store.CheckGithubNameUsable(githubUser.Login)++ if err != nil {+ e.ErrorHandler(w, "DATABASE_ERROR", "Error in CheckGithubNameUsable")+ return+ }++ if !githubNameUsable {+ e.ErrorHandler(w, "DATABASE_ERROR", "Error in CheckGithubNameUsable")+ return+ }++ userPatch := store.UserPatch{+ GithubName: &githubUser.Login,+ }++ store.UpdateUser(userId, &userPatch)+ }++ user, err := store.GetUserByGithubName(githubUser.Login)++ if err == sql.ErrNoRows {+ username := githubUser.Name+ usernameUsable, _ := store.CheckUsernameUsable(username)+ if !usernameUsable {+ username = username + common.GenUUID()+ }+ user, _ = store.CreateNewUser(username, username, githubUser.Login, "")+ }++ session.Values["user_id"] = user.Id+ session.Save(r, w)++ http.Redirect(w, r, "/", http.StatusTemporaryRedirect)+}+func RegisterAuthRoutes(r *mux.Router) {authRouter := r.PathPrefix("/api/auth").Subrouter()authRouter.HandleFunc("/signup", handleUserSignUp).Methods("POST")authRouter.HandleFunc("/signin", handleUserSignIn).Methods("POST")authRouter.HandleFunc("/signout", handleUserSignOut).Methods("POST")+ authRouter.HandleFunc("/github", handleGithubAuthCallback).Methods("GET")}diff --git a/main.go b/main.goindex 50bc2e90d..a8f2b8b4f 100644--- a/main.go+++ b/main.go@@ -13,8 +13,8 @@ func main() {r := mux.NewRouter().StrictSlash(true)- api.RegisterUserRoutes(r)api.RegisterAuthRoutes(r)+ api.RegisterUserRoutes(r)api.RegisterMemoRoutes(r)api.RegisterQueryRoutes(r)diff --git a/resources/memos.db b/resources/memos.dbindex e420b8d2c..5f0e267e6 100644Binary files a/resources/memos.db and b/resources/memos.db differdiff --git a/web/src/App.tsx b/web/src/App.tsxindex e448b1415..0f75e7b65 100644--- a/web/src/App.tsx+++ b/web/src/App.tsx@@ -3,7 +3,6 @@ import appContext from "./stores/appContext";import { appRouterSwitch } from "./routers";import { globalStateService } from "./services";import "./less/app.less";-import 'prismjs/themes/prism.css';function App() {const {diff --git a/web/src/components/MyAccountSection.tsx b/web/src/components/MyAccountSection.tsxindex 85ba67189..fdd7c5d77 100644--- a/web/src/components/MyAccountSection.tsx+++ b/web/src/components/MyAccountSection.tsx@@ -155,7 +155,7 @@ const MyAccountSection: React.FC<Props> = () => {<Only when={window.location.origin.includes("justsven.top")}><div className="section-container connect-section-container"><p className="title-text">关联账号</p>- <label className="form-label input-form-label">+ <label className="form-label input-form-label hidden"><span className="normal-text">微信 OpenID:</span>{user.wxOpenId ? (<>