簡體   English   中英

獲取登錄的用戶信息以供顯示

[英]Fetching Logged in User Info for display

我正在使用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
}

現在,我的問題是,如何將 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)

您可以很容易地使用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 )以添加中間件(開始( Use )或完成( Done )),只需使用iris.Application實例, app.Use/Done .使用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 上有很多,希望你一切順利!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM