当前位置: 首页 > 面试题库 >

获取登录的用户信息以显示

傅越
2023-03-14
问题内容

我正在使用https://github.com/kataras/iris
Go网络框架。我有:

  1. 用户注册
  2. 用户已验证并登录
  3. 创建会话并username使用用户键(表和结构)进行设置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
}

现在,我的问题是,如何将“已记录的用户”值共享给模板中的所有路由。我当前的选项是:

// 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)

问题答案:

您可以使用轻松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")
})

就这样,很简单,对吧?

但我有一些 笔记供您参考 ,如果您有更多时间请阅读。

当您位于根目录“
/”上时,不必.Party为了添加中间件(begin(Use)或finish(Done))而为它()创建一个参与者,只需使用iris.Application实例app.Use/Done

不要这样写:

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

    allRoutes.Get("/", myHandler)
}

改为:

app.Use(logThisMiddleware, authCheck)
app.Get("/", myHandler)

更容易 阅读和理解


注意到,;功能的最后使用的是您的编辑器和gocode工具,它们会删除它们,当您使用Go编程语言编写程序时,您不应该这样做,而是全部删除;

最后,请阅读文档和示例,我们在https://github.com/kataras/iris/tree/master/_examples中提供了许多示例,希望您一切顺利!



 类似资料:
  • cmf_get_current_user() 功能 获取当前登录的前台用户的信息,未登录时,返回false 参数 无 返回 array:用户信息;false表示未登录;

  • cmf_get_current_user() 功能 获取当前登录的前台用户的信息,未登录时,返回false 参数 无 返回 array:用户信息;false表示未登录;

  • 接口说明 获取当前登录用户的信息 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 GET /usercenter/api/userinfo/v1.0.0/getLoginUserInfo 是否需要登录 是 请求字段说明 参数 类型 请求类型 是否必须 说明 token string header 是 当前登录用户的TOKEN 响应字段说明 参

  • 接口说明 获取当前登录用户的信息 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 如开启https功能,请求地址的协议应改为https,如:https://www.example.com/wish3dearth/api/access/v1.0.0/getLicenseInfo API地址 GET /usercenter/api/userinfo/v1.0

  • sp_get_current_user() 功能: 获取当前登录用户信息,包括users表里详细信息; 参数: 无 返回: 数组,用户包括users表里详细信息

  • 目前,我正在使用以下内容将用户登录到我的应用程序中。然而,我想使用一个角函数来实际执行登录。为此,我想创建一个Rest网络服务来进行身份验证,但是我在SO上看到的所有示例都使用我认为被贬低的用户。我还希望该服务返回有关用户的信息。 我要问的是如何将MyUserDetailsService更改为用作登录的restful服务,或者如何创建一个可用于登录的服务,该服务将在登录后返回用户对象。 这是我的a