获取登录用户信息以进行显示 - Golang模板

时间:2017-09-09 12:01:30

标签: go

我使用的是https://github.com/kataras/iris golang网络框架。我做了以下事情: -

  1. 用户注册
  2. 用户验证&登录
  3. 使用用户(表格和结构)username
  4. 创建并使用键username进行设置

    现在,这是我登录用户的代码: -

    // Loaded All DB and other required value above
    
    allRoutes := app.Party("/", logThisMiddleware, authCheck) {
        allRoutes.Get("/", func(ctx context.Context) {
            ctx.View("index.html");
        });
    }
    

    在authcheck中间件

    func authcheck(ctx context.Context) {
        // Loaded session.
        // Fetched Session key "isLoggedIn"
        // If isLoggedIn == "no" or "" (empty)
        // Redirected to login page
        // else
        ctx.Next()
    }
    

    我的会话功能

    func connectSess() *sessions.Sessions {
        // Creating Gorilla SecureCookie Session
        // returning session
    }
    

    现在,我的问题是,如何将Logged User值共享给模板中的所有路由。我当前的选择是: -

    // Loaded all DB and required value
    allRoutes := app.Party("/", logThisMiddleware, authCheck) {
    
        allRoutes.Get("/", func(ctx context.Context) {
            // Load Session again
            // Fetch username stored in session
            // Run Query against DB
            // Share the user struct value.
            // Example ctx.ViewData("user", user)
            ctx.View("index.html");
        });
    
        allRoutes.Get("dashboard", func(ctx context.Context) {
            // Load Session again
            // Fetch username stored in session
            // Run Query against DB
            // Share the user struct value.
            // Example ctx.ViewData("user", user)
            ctx.View("index.html");
        });
    }
    

    但是上面代码的问题是,我将不得不为每个路由编写会话,并为我运行的每个路由再次运行查询而不是共享。

    我觉得,必须有更好的方法,而不是为authCheck中间件和第二allRoutes.Get路径中的每个路由加载两次会话。

    我需要关于如何优化这一点的想法,并且只需编写一次代码而不是在每个路径下面重复,就可以将用户数据共享到模板

            // Load Session again
            // Fetch username stored in session
            // Run Query against DB
            // Share the user struct value.
            // Example ctx.ViewData("user", user)
    

1 个答案:

答案 0 :(得分:2)

您可以使用ctx.Values().Set/Get轻松地在路由的处理程序或中间件之间创建可共享的内容。

// load session manager once
sess := connectSess()

func authCheck(ctx context.Context) {
    session := sess.Start(ctx)
    // Load your user here.
    // [...]
    // Save the returning user to the local storage of this handlers chain, once. 
    ctx.Values().Set("user", user) // <-- IMPORTANT
}

app.Get("/", func(ctx context.Context) {
    // Get the user from our handlers chain's local storage.
    user := ctx.Values().Get("user") // <-- IMPORTANT

    // Bind the "{{.user}}" to the user instance.
    ctx.ViewData("user", user)
    // Render the template file.
    ctx.View("index.html")
})

app.Get("dashboard", func(ctx context.Context) {
    // The same, get the user from the local storage...
    user := ctx.Values().Get("user") // <-- IMPORTANT

    ctx.ViewData("user", user)
    ctx.View("index.html")
})

这就是,非常简单,对吧?

但是我有一些笔记,如果你有更多时间,请阅读它们。

当您使用root“/”时,您不必为其创建一个聚会(.Party)以添加中间件(begin(Use)或finish({{1 }})),只使用Done实例,iris.Application

不写这个:

app.Use/Done

改为:

allRoutes := app.Party("/", logThisMiddleware, authCheck) {

    allRoutes.Get("/", myHandler)
}

阅读和理解更容易。

注意到您在功能结束时使用了app.Use(logThisMiddleware, authCheck) app.Get("/", myHandler) ,编辑器和;工具会在您编写程序时将其删除使用Go编程语言不应该这样做,删除所有gocode

最后,请阅读文档和示例,我们在https://github.com/kataras/iris/tree/master/_examples有很多,希望您最好!

相关问题