本文整理汇总了Golang中google/golang.org/appengine/user.LoginURL函数的典型用法代码示例。如果您正苦于以下问题:Golang LoginURL函数的具体用法?Golang LoginURL怎么用?Golang LoginURL使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LoginURL函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: APIKeyAddHandler
func APIKeyAddHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
loginUrl, _ := user.LoginURL(c, r.URL.RequestURI())
http.Redirect(w, r, loginUrl, http.StatusFound)
return
} else {
if !u.Admin {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("You're not an admin. Go away."))
} else {
key := randomString(26)
owner := r.FormValue("owner")
if owner == "" {
w.Write([]byte("You forgot a parameter."))
} else {
apiKey := APIKey{
APIKey: key,
OwnerEmail: owner,
}
dkey := datastore.NewIncompleteKey(c, "APIKey", nil)
_, err := datastore.Put(c, dkey, &apiKey)
if err != nil {
w.Write([]byte(fmt.Sprintf("error! %s", err.Error())))
} else {
w.Write([]byte(key))
}
}
}
}
}
示例2: loginWithGoogle
func loginWithGoogle() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := appengine.NewContext(c.Request)
u := user.Current(ctx)
if u == nil {
url, _ := user.LoginURL(ctx, c.Request.URL.String())
c.HTML(302, "login.tmpl", gin.H{
"url": url,
})
c.Abort()
return
}
email := strings.Split(u.Email, "@")
if email[1] == "elo7.com" && len(email) == 2 {
developer := models.Developer{Email: u.Email}
developer.Create(&db)
log.Infof(ctx, developer.Email)
} else {
url, _ := user.LogoutURL(ctx, "/")
c.Redirect(http.StatusTemporaryRedirect, url)
}
c.Next()
}
}
示例3: Api1UserProfileHandler
// If the user is not logged in, then return the login url. Otherwise return a json
// structure containing the user's name and email address, and which team they are on.
func Api1UserProfileHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
data := UserProfileData{}
ctx := appengine.NewContext(r)
u := user.Current(ctx)
if u == nil {
url, _ := user.LoginURL(ctx, "/")
data.LoginUrl = url
datajson, err := json.Marshal(data)
if err != nil {
http.Error(w, "Internal Service Error", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s", datajson)
return
}
url, _ := user.LogoutURL(ctx, "/")
data.LogoutUrl = url
data.Email = u.Email
data.IsAdmin = u.Admin
data.IsLoggedIn = true
datajson, err := json.Marshal(data)
if err != nil {
http.Error(w, "Internal Service Error", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s", datajson)
}
示例4: index
func index(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
// user.Current gives data about what the requester is
// logged in as, or nil if they are not logged in.
u := user.Current(ctx)
var model indexModel
// If they are not nil, they are logged in.
if u != nil {
// So let the template know, and get the logout url.
model.Login = true
logoutURL, err := user.LogoutURL(ctx, "/")
if err != nil {
log.Errorf(ctx, err.Error())
http.Error(res, "Server Error", http.StatusInternalServerError)
return
}
model.LogoutURL = logoutURL
} else {
// Otherwise, get the login url.
loginURL, err := user.LoginURL(ctx, "/")
if err != nil {
log.Errorf(ctx, err.Error())
http.Error(res, "Server Error", http.StatusInternalServerError)
return
}
model.LoginURL = loginURL
}
tpl.ExecuteTemplate(res, "index", model)
}
示例5: rootHandler
func rootHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
u := user.Current(ctx)
if u == nil {
url, _ := user.LoginURL(ctx, "/")
fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
return
}
fmt.Fprint(w, `<html><h1>Hi! Welcome to Tada</h1>`)
fmt.Fprint(w, "<!-- About to call writeItems -->")
fmt.Fprint(w, `<ol>`)
writeItems(w, r, u)
fmt.Fprint(w, `</ol>`)
fmt.Fprint(w, "<!-- Called writeItems -->")
url, _ := user.LogoutURL(ctx, "/")
fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, u, url)
fmt.Fprint(w, `</html>`)
makeNewItemForm(w)
}
示例6: editHandler
func editHandler(w http.ResponseWriter, r *http.Request) {
//For now identify by the ThreadId since each submission will have a unique one
strVal := r.FormValue("thread")
//TODO update with YAML require login to not need to check the user status
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
page := template.Must(template.ParseFiles(
"public/templates/_base_edit.html",
"public/templates/edit.html",
))
data := struct{ ThreadId string }{strVal}
if err := page.Execute(w, data); err != nil {
serveError(c, w, err)
fmt.Fprintf(w, "\n%v\n%v", err.Error(), data)
return
}
}
示例7: appstatsHandler
func appstatsHandler(w http.ResponseWriter, r *http.Request) {
ctx := storeContext(appengine.NewContext(r))
if appengine.IsDevAppServer() {
// noop
} else if u := user.Current(ctx); u == nil {
if loginURL, err := user.LoginURL(ctx, r.URL.String()); err == nil {
http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
} else {
serveError(w, err)
}
return
} else if !u.Admin {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
if detailsURL == r.URL.Path {
details(ctx, w, r)
} else if fileURL == r.URL.Path {
file(ctx, w, r)
} else if strings.HasPrefix(r.URL.Path, staticURL) {
name := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
content, ok := static[name]
if !ok {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
http.ServeContent(w, r, name, initTime, content)
} else {
index(ctx, w, r)
}
}
示例8: GetUser
// Retrieve User object and return the user's email
// If the user is logged in, return a logout URL so
// the user can logout
func GetUser(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
localUser := new(User)
if u == nil {
url, _ := user.LoginURL(c, "/")
localUser.Url = url
data, err := json.MarshalIndent(localUser, "", "\t")
if err != nil {
panic(err)
}
w.Write(data)
return
}
url, _ := user.LogoutURL(c, "/")
localUser.Url = url
localUser.Email = u.Email
data, err := json.MarshalIndent(localUser, "", "\t")
if err != nil {
panic(err)
}
w.Write(data)
}
示例9: handle
func handle(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(res, req)
return
}
ctx := appengine.NewContext(req)
u := user.Current(ctx)
if u != nil {
key := datastore.NewKey(ctx, "profile", u.Email, 0, nil)
var p profile
err := datastore.Get(ctx, key, &p)
if err == datastore.ErrNoSuchEntity {
http.Redirect(res, req, "/CreateProfile", http.StatusSeeOther)
return
} else if err != nil {
http.Error(res, "Server error!", http.StatusInternalServerError)
log.Errorf(ctx, "Datastore get Error: %s\n", err.Error())
return
}
}
// Get recent tweets
query := datastore.NewQuery("Tweets")
tweets := []tweet{}
_, err := query.GetAll(ctx, &tweets)
if err != nil {
http.Error(res, "Server error!", http.StatusInternalServerError)
log.Errorf(ctx, "Query Error: %s\n", err.Error())
return
}
// Create template
loginURL, err := user.LoginURL(ctx, "/")
if err != nil {
http.Error(res, "Server error!", http.StatusInternalServerError)
log.Errorf(ctx, "Login URL Error: %s\n", err.Error())
return
}
data := mainpageData{
Tweets: tweets,
Logged: u != nil,
LoginURL: loginURL,
}
if data.Logged {
data.Email = u.Email
}
err = tpl.ExecuteTemplate(res, "index.gohtml", data)
if err != nil {
http.Error(res, "Server error!", http.StatusInternalServerError)
log.Errorf(ctx, "Template Execute Error: %s\n", err.Error())
return
}
}
示例10: handleGAEAuth
// handleGAEAuth sends a redirect to GAE authentication page.
func handleGAEAuth(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
errorf(c, "user.LoginURL(%q): %v", r.URL.String(), err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, url, http.StatusFound)
}
示例11: GetUserInfoHandler
// GetUserInfoHandler returns either the location where the user can log into
// the app, or metadata about the currently authenticated user.
func GetUserInfoHandler(w util.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
dest := mux.Vars(r)["dest"]
if dest == "" {
dest = "/"
}
if u == nil {
url, err := user.LoginURL(c, dest)
w.WriteJSON(map[string]interface{}{"loginURL": url}, err)
return
}
// Check if the user exists in the database
q := datastore.NewQuery("Users").Limit(1)
q.Filter("GoogleID = ", u.ID)
var results []User
keys, err := q.GetAll(c, &results)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(results) == 0 {
newUser := User{
GoogleID: u.ID,
CreatedTime: time.Now(),
Email: u.Email,
}
key := datastore.NewIncompleteKey(c, "Users", nil)
newKey, err := datastore.Put(c, key, &newUser)
if newKey != nil {
newUser.ID = newKey.IntID()
}
url, _ := user.LogoutURL(c, dest)
newUser.LogoutURL = url
w.WriteJSON(newUser, err)
return
}
url, _ := user.LogoutURL(c, dest)
fullUser := results[0]
fullUser.ID = keys[0].IntID()
fullUser.LogoutURL = url
w.WriteJSON(fullUser, nil)
}
示例12: welcome
func welcome(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "text/html; charset=utf-8")
ctx := appengine.NewContext(r)
u := user.Current(ctx)
if u == nil {
url, _ := user.LoginURL(ctx, "/")
fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
return
}
url, _ := user.LogoutURL(ctx, "/")
fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, u, url)
}
示例13: handle
func handle(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(res, req)
return
}
ctx := appengine.NewContext(req)
u := user.Current(ctx)
if u != nil {
_, err := getProfileByEmail(ctx, u.Email)
if err == datastore.ErrNoSuchEntity {
http.Redirect(res, req, "/CreateProfile", http.StatusSeeOther)
return
} else if err != nil {
http.Error(res, "Server error!", http.StatusInternalServerError)
log.Errorf(ctx, "Datastore get Error: %s\n", err.Error())
return
}
}
//Get recent tweets
tweets, err := getTweets(ctx, "")
if err != nil {
http.Error(res, "server error!", http.StatusInternalServerError)
log.Errorf(ctx, "Query Error: &s\n", err.Error())
return
}
// Create template
loginURL, err := user.LoginURL(ctx, "/")
if err != nil {
http.Error(res, "server error!", http.StatusInternalServerError)
log.Errorf(ctx, "Login URL Error: &s\n", err.Error())
return
}
data := mainpageData{
Tweets: tweets,
Logged: u != nil,
LoginURL: loginURL,
}
if data.Logged {
data.Email = u.Email
}
err = tpl.ExecuteTemplate(res, "index.gohtml", nil)
if err != nil {
http.Error(res, "Serever error!", http.StatusInternalServerError)
log.Errorf(ctx, "Template Parse Error: %s\n", err.Error())
return
}
}
示例14: login
func login(c context.Context, w http.ResponseWriter, r *http.Request) {
// TODO: check default page and redirect to page list without it
var postLogin string
if dest, ok := r.URL.Query()["dest"]; ok {
postLogin = dest[0]
} else {
postLogin = MNG_ROOT_URL
}
loginURL, _ := user.LoginURL(c, postLogin)
http.Redirect(w, r, loginURL, http.StatusFound)
}
示例15: handler
func handler(res http.ResponseWriter, req *http.Request) {
c := appengine.NewContext(req)
u := user.Current(c)
if u == nil {
url, err := user.LoginURL(c, req.URL.String())
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.Header().Set("Location", url)
res.WriteHeader(http.StatusFound)
return
}
fmt.Fprintf(res, "Hello, %v!", u)
}