Link Search Menu Expand Document

重定向到认证页

  1. 示例如下

示例如下

可以在应用程序中提供一个登录按钮或者是用链接的方式使用户跳转到登录路由:

<a href="/login">Sign In</a>

登录句柄将生成一个链接,并将用户重定向到XAuth:

http.HandleFunc("/login", LoginHandler)

func LoginHandler(w http.ResponseWriter, r *http.Request) {
    nonce, _ = XAuthUtils.GenerateNonce()
    var redirectPath string

    q := r.URL.Query()
    q.Add("client_id", os.Getenv("CLIENT_ID"))
    q.Add("response_type", "code")
    q.Add("response_mode", "query")
    q.Add("scope", "openid profile email")
    q.Add("redirect_uri", "http://localhost:8080/authorization-code/callback")
    q.Add("state", state)
    q.Add("nonce", nonce)

    redirectPath = os.Getenv("ISSUER") + "/v1/authorize?" + q.Encode()

    http.Redirect(w, r, redirectPath, http.StatusMovedPermanently)
}

用户将被重定向到XAuth托管的登录页面并进行身份认证,认证成功后用户将会被通过浏览器重定向的回到应用程序,并且在这次重定向中包含了用户的信息。