React ?? ???? ???? ???? ???? ???? ?? ??? ??? github OAUTH? ??? ??????. ?? ????? ? ???? ?? ?? ?? ?? ???? ???? ???? ? ????. ??? ?? React? ???? ????? ??? ????? ?? ?? ? ? ????.
????? ?? ?????, ?? ???? ?? ?? ?? ?????? ????? scs?? ?? ? ???????? ???? ????? ??? ?????. ??? ??? ? ?? ??? ????? ???? ?? ? ?????. ???? API ???? ??? ? ??? ???, ???? ?? ?? ???? ?? ? ?? ?? ??? ????? ??? ??? ??? ???? ??? ??? ????? ?????. ?? ??? ??? ????.
func (h Handler) HandleAuth(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:5173") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") w.Header().Set("Access-Control-Allow-Methods", "GET") url := Oauth2Config.AuthCodeURL("state", oauth2.AccessTypeOffline) http.Redirect(w, r, url, http.StatusFound) } func (h Handler) HandleAuthCallback(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:5173") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") w.Header().Set("Access-Control-Allow-Methods", "GET") code := r.URL.Query().Get("code") token, err := Oauth2Config.Exchange(r.Context(), code) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // Use the access token to get the user's GitHub data client := github2.NewTokenClient(r.Context(), token.AccessToken) user, _, err := client.Users.Get(r.Context(), "") if err != nil { fmt.Printf("Error: %v\n", err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } session, err := store.Get(r, "session") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } session.Values["user"] = user.GetLogin() session.Values["access_token"] = token.AccessToken err = session.Save(r, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, "this is authcallback: %s", user.GetLogin()) } func (h Handler) HandleCurrentUser(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") w.Header().Set("Access-Control-Allow-Methods", "GET") session, err := store.Get(r, "session") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } user, ok := session.Values["user"].(string) if !ok { http.Error(w, "Invalid user in session", http.StatusInternalServerError) return } // Set the content type header to JSON w.Header().Set("Content-Type", "text/plain") // Write the JSON data to the response w.Write([]byte(user)) }
IIUC, ????? http://localhost:5173
加載,并向 http://127.0.0.1:3080/user
?? GET ??? ???. ??? ??? ?? ?? ?????.
????? ????? ?? ? XMLHttpRequest ?? Fetch ???? ?? ??(?: Cookie ? HTTP ??)? ??? ????. XMLHttpRequest ?? ?? ?? ???? ??? ? ?? ???? ???? ???.
??? ??? URL? ??? ???? ????? ????? ???? ??? ??? ?? ???? ???.
?????? ??? ?? ??? ??? ??? ?????.
??? ??? CORS ??? ???? ??? ? ???? ?? ?? ??? ???? ??? ??? ????. ??? ?? ?? ????? DevTools ??? ?????. ???? ??? ?????? ???? ??/?? ???? ????? ???.
? ??? ????? ? ??? ?? ???? ?????.
?? ??: go run main.go
??? ????? ?????? http://127.0.0.1:3080/callback
? ?????.
Set-Cookie: session=abc;路徑=/;過(guò)期=2023 年 4 月 18 日星期二 18:34:49 GMT;最大年齡=86372;僅 Http; SameSite=Lax
.
???? ??? http://127.0.0.1:5173/
? ?????.
? ????? Get ??? ?????. DevTools ??? ?? ?? "abc"? ???? ???.
Notes:
??? ??? 127.0.0.1
(?? ??)? ???? ??? ?? ?? ??????. ??? 127.0.0.1
(沒(méi)有端口)。所以http://127.0.0.1:5173/
?????? ??? ?? ? ????.
??? ???? ?? ?? ??? ? ? ????. ??? ????? ????? ??????.