2

I am using Beego framework for my purpose and i have two html view files one is login and other is landing login.html

<form action="/commonDashboard" name="loginForm" method="post" autocomplete="off" id="loginForm" >
             <input type="text" placeholder="Enter your user name" id="userName" name="userName" taborder="1">
                <input type="password" class="qwerty" placeholder="Enter your password" id="userPassword" name="userPassword" taborder="2">
                <input type="button" value="Login" onclick="validatelogin();" taborder="3">
                <input type="button" value="Clear" onclick="resetForm();" taborder="3">
    <span class="forgot"><a href="#">forgot password ?</a></span>
            <!-- Virtual Keyboard -->
                <span class="virtual"><a href="#" id="passwd" class="tooltip-tipsy" title="Click to open the virtual keyboard">Virtual Keyboard  
    </span>
            </form>
        

and landing page is

<header class="top">
 <div class="container-fluid">
     <aside class="logo"><img src="img.jpg"  class="img-responsive"></aside>
        
        <aside class="top-right">
         <div class="profile">
             <ul>
                 <li class="name"><img src="../static/img/profile-img.jpg" alt="Shri Ram"> Hi <a href="#">Shri</a></li>
                    <li class="logout"><a href="/login">Logout</a></li>
                    <li><a href="#">Contacts</a></li>
                    <li class="last"><a href="#">Help</a></li>
                </ul>
            </div>
            
            <div class="login-time">
             <ul>
                 <li><span>Current Login:</span> Mon 22 Sep 2014 03:28 PM</li>
                    <li class="last"><span>Last Login:</span> Sun 21 Sep 2014 02:28 PM</li>
                </ul>
            </div>
        </aside>
    </div>
</header>

and controller is as follows

package controllers

import (
 "fmt"
 "github.com/astaxie/beego"
)

type MainController struct {
 beego.Controller
}

func (c *MainController) Get() {
 c.TplNames = "login.html"
 fmt.Println("login Get Method")
}

type LoginController struct {
 beego.Controller
}

func (c *LoginController) Post() {
 c.TplNames = "commonDashboard.html"
 fmt.Println("this is dashboard")
 password := c.Input().Get("userPassword")
 username := c.GetSession("userName").(string)
 c.SetSession("userName", username)
 c.Data["user"] = username
 fmt.Println("password:" + " " + password)
}

type HeaderController struct {
 beego.Controller
}

func (c *HeaderController) Get() {
 fmt.Println("this is Header")
 username := c.Input().Get("userName")
 fmt.Println(username)
 c.TplNames = "commonHeader.html"
}

type FooterController struct {
 beego.Controller
}

func (c *FooterController) Get() {
 fmt.Println("this is footer")
 c.TplNames = "commonFooter.html"
}

type MenuController struct {
 beego.Controller
}

func (c *MenuController) Get() {
 fmt.Println("this is menu")
 c.TplNames = "commonMenu.html"
}

i want to display the currently logged in username in header of html file

Vijay Kumar
  • 597
  • 2
  • 8
  • 27

1 Answers1

1

Enable session support in app.conf with:

sessionon = true

Save the username into session if login is valid:

func (c *LoginController) Post() {
    var json JsonObject
    u := &User{}
    if err := c.ParseForm(u); err != nil {
        json = JsonObject{
            Success: false,
            Message: "Server error",
        }
    } else {
        if u.Username == "admin" && u.Password == "123456" {
            json = JsonObject{
                Success: true,
            }
            // successfully login, put the user into session, you may put just username in
            c.SetSession("User", u)
        } else {
            json = JsonObject{
                Success: false,
                Message: "Incorrect username or password",
            }
        }
    }
    c.Data["json"] = json
    c.ServeJson()
}

Then you can recover the username in every Controller:

func (c *IndexController) Get() {
    // get user from session, you may get just username
    u := c.GetSession("User")
    if u == nil {
        c.Redirect("/login", 302)
    }
    // set user from session into response data
    c.Data["User"] = u
    c.TplNames = "index.html"
}

Get it on the html with template format:

{{ .User.username }}

You can just use {{ .userName }}

Jiang
  • 590
  • 2
  • 10
  • thanks . I AM getting error: incompatible types in binary expression fmt.Println("username:" + " " + username) – Vijay Kumar May 27 '15 at 08:53
  • @VijayKumar `username := c.GetSession().(string)` Remember that golang is sensitive with type. While `GetSession` returns a type of `interface{}`, you should explicitly convert it to the type which you set into session. – Jiang May 27 '15 at 08:58
  • 1
    i am trying it like this username := c.GetSession(userName).(string) c.Data["user"] = username – Vijay Kumar May 27 '15 at 09:04
  • its giving this error now even though field name is same as i have mentioned error: reference to undefined name ‘userName’ username := c.GetSession(userName).(string) – Vijay Kumar May 27 '15 at 09:28
  • and when i try this username := c.GetSession("userName").(string) c.Data["user"] = username i got this error on browsers display --Handler crashed with error interface conversion: interface is nil, not string – Vijay Kumar May 27 '15 at 09:56
  • @VijayKumar Make sure that you've set the `userName` into session by `c.SetSession("userName", )` while doing login. – Jiang May 27 '15 at 09:59
  • shall i put all controller code for better undersanding. – Vijay Kumar May 27 '15 at 10:10
  • 1
    That would be better, login controller and the controller which you want to use `username`. – Jiang May 27 '15 at 10:16
  • still its the same. Can you please specify that in your answer. Field name is userName. – Vijay Kumar May 27 '15 at 10:18
  • can you see my updated question where i have updated my controllers. – Vijay Kumar May 27 '15 at 10:27
  • i want to display that username in landing page particularly in header – Vijay Kumar May 27 '15 at 10:30
  • but no where &User is defined – Vijay Kumar May 27 '15 at 10:50
  • well, you should change it into a simple string, `User` is a struct I define. – Jiang May 27 '15 at 12:31