check session for login with gorilla sessions

685 views
Skip to first unread message

amir-taghavi

unread,
Dec 4, 2014, 9:08:20 AM12/4/14
to golan...@googlegroups.com
hi i want write function which check session login in panels page .
in following code i write checklogin and called it on panel function but it not work , while i run localhost:3030/panel page is white

package main

import(
"fmt"
"net/http"
)

var store = sessions.NewCookieStore([]byte("security code"))
func login(w http.ResponseWriter,r *http.Request){
fmt.Fprintln(w,"login page")
}

func set(w http.ResponseWriter,r *http.Request){
users,_ := store.Get("admins")
users.Values["login"] = true
users.Save(r,w)
}

func panel(w http.ResponseWriter,r *http.Request){
checklogin(w,r)
fmt.Fprintln(w,"Welcome to panel")
}

func checklogin(w http.ResponseWriter,r *http.Request){
users,_ := store.Get("admins")
if users.Values["login"]==nil{
http.Redirect(w,r,"/login",http.StatusFound)
}
}

func main(){
http.HandleFunc("/login",login)
http.HandleFunc("/set",login)
http.HandleFunc("/panel",login)
err := http.ListenAndServe(":3030",nil)
if err!=nil{
fmt.Print(err)
}
}



Nigel Tao

unread,
Dec 4, 2014, 9:50:02 PM12/4/14
to amir-taghavi, golang-nuts
On Fri, Dec 5, 2014 at 1:08 AM, amir-taghavi <amirtag...@gmail.com> wrote:
> users,_ := store.Get("admins")

This doesn't even compile. Get takes two arguments.
http://godoc.org/github.com/gorilla/sessions#CookieStore.Get

amir-taghavi

unread,
Dec 5, 2014, 3:06:49 AM12/5/14
to golan...@googlegroups.com
i change code :
package main

import(
"fmt"
"net/http"
)

var store = sessions.NewCookieStore([]byte("security code"))
func login(w http.ResponseWriter,r *http.Request){
fmt.Fprintln(w,"login page")
}

func set(w http.ResponseWriter,r *http.Request){
users,_ := store.Get(r,"admins")
users.Values["login"] = true
users.Save(r,w)
}

func panel(w http.ResponseWriter,r *http.Request){
checklogin(w,r)
fmt.Fprintln(w,"Welcome to panel")
}

func checklogin(w http.ResponseWriter,r *http.Request){
_,exist := store.Get(r,"admins")
if exist==nil{
http.Redirect(w,r,"/login",http.StatusFound)
}
}

func main(){
http.HandleFunc("/login",login)
http.HandleFunc("/set",set)
http.HandleFunc("/panel",panel)
err := http.ListenAndServe(":3030",nil)
if err!=nil{
fmt.Print(err)
}
}



but it not work
what do i do?

chris dollin

unread,
Dec 5, 2014, 3:28:47 AM12/5/14
to amir-taghavi, golang-nuts
On 5 December 2014 at 08:06, amir-taghavi <amirtag...@gmail.com> wrote:
> func main(){
> http.HandleFunc("/login",login)
> http.HandleFunc("/set",set)
> http.HandleFunc("/panel",panel)
> err := http.ListenAndServe(":3030",nil)
> if err!=nil{
> fmt.Print(err)
> }
> }
>
> but it not work
> what do i do?

You tell us exactly what you did and exactly what happened.

"not work" isn't much of a clue.

Chris

--
Chris "allusive" Dollin

amir-taghavi

unread,
Dec 5, 2014, 5:32:09 AM12/5/14
to golan...@googlegroups.com
i called localhost:3030/panel and it redirect me to login page
now i called localhost:3030/set and call localhost:3030/panel again it redirect me to login page !!!

amir-taghavi

unread,
Dec 5, 2014, 2:44:43 PM12/5/14
to golan...@googlegroups.com
help me please 

atd...@gmail.com

unread,
Dec 5, 2014, 6:28:46 PM12/5/14
to golan...@googlegroups.com
check your handlefunc.. you used  the `login` handler function thrice.

On Friday, December 5, 2014 7:44:43 PM UTC, amir-taghavi wrote:
help me please 

atd...@gmail.com

unread,
Dec 5, 2014, 6:32:55 PM12/5/14
to golan...@googlegroups.com
Sorry.. didn't see you had changed your code.

Try to print(exist), it's probably nil.

atd...@gmail.com

unread,
Dec 5, 2014, 6:36:09 PM12/5/14
to golan...@googlegroups.com
And what is the return value you ignored in the `set` handler ?

amir-taghavi

unread,
Dec 5, 2014, 11:58:36 PM12/5/14
to golan...@googlegroups.com
if i ignored set handler i can not access to panel handler 
else if i called set handler i must access to panel handler but panel handler not allow me to see the panel and redirect to login 

atd...@gmail.com

unread,
Dec 6, 2014, 4:57:33 AM12/6/14
to golan...@googlegroups.com


func set(w http.ResponseWriter,r *http.Request){
users,_ := store.Get(r,"admins")
users.Values["login"] = true
users.Save(r,w)
}

"users,_ := "
you're probably ignoring an error value or boolean that test whether you successfuly retrieved user info from store.


func checklogin(w http.ResponseWriter,r *http.Request){
_,exist := store.Get(r,"admins")
if exist==nil{
http.Redirect(w,r,"/login",http.StatusFound)
}
}

 
if exist is nil, you'll get redirected. exist is probably nil.

You need to carefully review your programs. Don't just copy paste. Make sure you understand every line. 

Happy coding.

Erfan Akbarimanesh

unread,
Dec 7, 2014, 5:47:25 AM12/7/14
to golan...@googlegroups.com
do you understand code? 
in programming you must can understand code . for write program you should think to it . then write code
i think you are hasty

 first : practice basic programming on go

i'm sure you can but you should try

your code is wrong . 
_,exist := store.Get(r,"admins")
       if exist==nil{
         http.Redirect(w,r,"/login",http.StatusFound)
   }

please see the following code and think it over:
users,_ := store.Get(r,"admins")
if _,ok := users.Values["login"];!ok{
http.Redirect(w,r,"/login",http.StatusFound)
}

Good Luck



amir-taghavi

unread,
Dec 7, 2014, 6:25:12 AM12/7/14
to golan...@googlegroups.com
thank you erfan 
my problem solved

Reply all
Reply to author
Forward
0 new messages