首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有可能在google app engine上使用sessionauth of martini和Datastore?

有没有可能在google app engine上使用sessionauth of martini和Datastore?
EN

Stack Overflow用户
提问于 2014-06-03 17:18:18
回答 1查看 493关注 0票数 1

我尝试在谷歌应用引擎上使用martinisessionauth示例,并希望将登录列表保存在数据存储中,但不知道如何处理appengine.Context。有谁有这样的经验吗?

谢谢。

更新:

代码语言:javascript
复制
// Auth example is an example application which requires a login
// to view a private link. The username is "testuser" and the password
// is "password". This will require GORP and an SQLite3 database.
package ahl

import (
    //"fmt"
    "github.com/go-martini/martini"
    "github.com/hnakamur/gaesessions"
    "github.com/martini-contrib/binding"
    "github.com/martini-contrib/render"
    "github.com/martini-contrib/sessionauth"
    "github.com/martini-contrib/sessions"
    "net/http"
)

//var namespace string = "ahl"

func init() {
    //store := sessions.NewCookieStore([]byte("secret123"))
    store := gaesessions.NewDatastoreStore("", gaesessions.DefaultNonPersistentSessionDuration)

    m := martini.Classic()
    m.Use(render.Renderer())

    // Default our store to use Session cookies, so we don't leave logged in
    // users roaming around
    //store.Options(sessions.Options{
    //  MaxAge: 0,
    //})
    m.Use(sessions.Sessions("my_session", store))
    m.Use(sessionauth.SessionUser(GenerateAnonymousUser))
    sessionauth.RedirectUrl = "/new-login"
    sessionauth.RedirectParam = "new-next"

    m.Get("/", func(r render.Render) {
        r.HTML(200, "index", nil)
    })

    m.Get("/new-login", func(r render.Render) {
        r.HTML(200, "login", nil)
    })

    m.Post("/new-login", binding.Bind(MyUserModel{}), func(session sessions.Session, postedUser MyUserModel, r render.Render, req *http.Request) {
        // You should verify credentials against a database or some other mechanism at this point.
        // Then you can authenticate this session.
        //user := MyUserModel{}
        user := MyUserModel{1, "testuser", "password", false}
        //err := dbmap.SelectOne(&user, "SELECT * FROM users WHERE username = $1 and password = $2", postedUser.Username, postedUser.Password)
        //if err != nil {
        //  r.Redirect(sessionauth.RedirectUrl)
        //  return
        //} else {
        err := sessionauth.AuthenticateSession(session, &user)
        if err != nil {
            r.JSON(500, err)
        }

        params := req.URL.Query()
        redirect := params.Get(sessionauth.RedirectParam)
        r.Redirect(redirect)
        return
        //}
    })

    m.Get("/private", sessionauth.LoginRequired, func(r render.Render, user sessionauth.User) {
        r.HTML(200, "private", user.(*MyUserModel))
    })

    m.Get("/logout", sessionauth.LoginRequired, func(session sessions.Session, user sessionauth.User, r render.Render) {
        sessionauth.Logout(session, user)
        r.Redirect("/")
    })

    http.Handle("/", m)
}
EN

回答 1

Stack Overflow用户

发布于 2014-06-03 20:14:52

是的,这应该是可能的。sessionauth包要求您向它传递一个cookie,并且有一个*sessions.Store包可以替换默认的cookie/文件存储:https://github.com/hnakamur/gaesessions

sessionauth包有一个完整的示例(https://github.com/martini-contrib/sessionauth/blob/master/example/auth_example.go) -只需将sessions.NewCookieStore替换为gaesessions.NewDatastoreStore即可。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24011381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档