Beego: Howto create Simple Login

Created on 24 Feb 2014  路  4Comments  路  Source: astaxie/beego

Howto create Simple Login for app ?

Most helpful comment

I think another way is also effect.

  1. defined a BaseController:
type BaseController struct{
       beego.Controller
}

func (b *BaseController) Prepare(){
         sess_username, _ := self.GetSession("username").(string)
    if sess_username == "" {
        self.TplNames = "login.html"
        self.Render()
    } else { 
        self.Ctx.Redirect(302, "/")
    }
}
  1. all your logic Controller inherit BaseController:
type MainController struct{
         BaseController
}

All 4 comments

You can use the plugin to do the simple login:

var FilterUser = func(ctx *context.Context) {
    _, ok := ctx.Input.Session("uid").(int)
    if !ok {
        ctx.Redirect(302, "/login")
    }
}

beego.AddFilter("*","AfterStatic",FilterUser)

Like -> https://github.com/insionng/toropress/blob/master/handlers/LoginHandler.go

Or like http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins

@login_required in function like (Flask Python Framework)

.....There are only two changes to this function. First, we have added the login_required decorator. This will ensure that this page is only seen by logged in users. .......

Migrate from Flask Framework Python to Golang Beego Framework

I think another way is also effect.

  1. defined a BaseController:
type BaseController struct{
       beego.Controller
}

func (b *BaseController) Prepare(){
         sess_username, _ := self.GetSession("username").(string)
    if sess_username == "" {
        self.TplNames = "login.html"
        self.Render()
    } else { 
        self.Ctx.Redirect(302, "/")
    }
}
  1. all your logic Controller inherit BaseController:
type MainController struct{
         BaseController
}

:+1: Thanks!

Simple and fast, long life for Beego

Was this page helpful?
0 / 5 - 0 ratings