本文整理匯總了Golang中models.User.CreateOrUpdateUser方法的典型用法代碼示例。如果您正苦於以下問題:Golang User.CreateOrUpdateUser方法的具體用法?Golang User.CreateOrUpdateUser怎麽用?Golang User.CreateOrUpdateUser使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類models.User
的用法示例。
在下文中一共展示了User.CreateOrUpdateUser方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetCallback
func (oc *OauthController) GetCallback(w http.ResponseWriter, r *http.Request) {
// Generate config
conf := oc.generateConfig()
// Get the code from the request to callback handler
code := r.URL.Query().Get("code")
// Exchange code for access token
token, err := conf.Exchange(oauth2.NoContext, code)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Get user details
client := conf.Client(oauth2.NoContext, token)
userDetails, err := oc.retrieveUserDetails(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create or update user/token, persist to database
userModel := new(models.User)
_, isAlreadyRegistered := userModel.CreateOrUpdateUser(userDetails["data"].(map[string]interface{}), token)
// Redirect user to appropriate landing page
redirectPath := "/login"
if !isAlreadyRegistered {
redirectPath = "/register"
}
// Generate user cookie
if cookie, err := oc.createSecureCookie(userModel); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
http.SetCookie(w, cookie)
}
// Do not forget the path prefix
http.Redirect(w, r, pathPrefix+redirectPath, http.StatusMovedPermanently)
}