亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

React? ???? Go ???? ?? ???? ??? ? ????
P粉360266095
P粉360266095 2024-04-06 13:09:26
0
1
1071

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

P粉360266095
P粉360266095

?? ??(1)
P粉237029457

IIUC, ????? http://localhost:5173 加載,并向 http://127.0.0.1:3080/user?? GET ??? ???. ??? ??? ?? ?? ?????.

????? ????? ?? ? XMLHttpRequest ?? Fetch ???? ?? ??(?: Cookie ? HTTP ??)? ??? ????. XMLHttpRequest ?? ?? ?? ???? ??? ? ?? ???? ???? ???.

??? ??? URL? ??? ???? ????? ????? ???? ??? ??? ?? ???? ???.

???

??? ??? ?? ??? ??? ??? ?????.

??? ??? CORS ??? ???? ??? ? ???? ?? ?? ??? ???? ??? ??? ????. ??? ?? ?? ????? DevTools ??? ?????. ???? ??? ?????? ???? ??/?? ???? ????? ???.


? ??? ????? ? ??? ?? ???? ?????.

  1. ?? ??: go run main.go

  2. ??? ????? ?????? http://127.0.0.1:3080/callback? ?????.

    Set-Cookie: session=abc;路徑=/;過(guò)期=2023 年 4 月 18 日星期二 18:34:49 GMT;最大年齡=86372;僅 Http; SameSite=Lax.

  3. ???? ??? http://127.0.0.1:5173/? ?????.

  4. ? ????? Get ??? ?????. DevTools ??? ?? ?? "abc"? ???? ???.

Notes:

??? ??? 127.0.0.1(?? ??)? ???? ??? ?? ?? ??????. ??? 127.0.0.1(沒(méi)有端口)。所以http://127.0.0.1:5173/ ?????? ??? ?? ? ????.

???

??? ???? ?? ?? ??? ? ? ????. ??? ????? ????? ??????.