From 954e140dfdf78f05f48ac4848dd544f37aabf802 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sun, 15 Mar 2015 00:01:48 +0700 Subject: [PATCH] Keep user tokens, fix user info loading --- server/auth.go | 4 ++-- server/request.go | 6 +++++- task/user.go | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/server/auth.go b/server/auth.go index bc88ca7..0a3d8b9 100644 --- a/server/auth.go +++ b/server/auth.go @@ -27,8 +27,8 @@ func authCallbackHandler(w http.ResponseWriter, r *http.Request) { code := r.FormValue("code") log.Printf("Got code %q\n", code) - if _, login, err := task.Authenticate(code); err == nil { - req.authorize(login) + if token, login, err := task.Authenticate(code); err == nil { + req.authorize(token, login) } else { panic(err) } diff --git a/server/request.go b/server/request.go index c483d58..9562101 100644 --- a/server/request.go +++ b/server/request.go @@ -16,6 +16,7 @@ type ( r *http.Request w http.ResponseWriter sid string + token string login string } statRequest struct { @@ -32,16 +33,19 @@ type ( func parseRequest(w http.ResponseWriter, r *http.Request) (*request, *statRequest) { sid := sessionID(w, r) login, _ := redis.String(redisPool.Get().Do("HGET", "sessions", sid)) + token, _ := redis.String(redisPool.Get().Do("HGET", "tokens", sid)) req := &request{ r: r, w: w, sid: sid, + token: token, login: login, } return req, parseStatRequest(r) } -func (r *request) authorize(login string) { +func (r *request) authorize(token, login string) { + redisPool.Get().Do("HSET", "tokens", r.sid, token) redisPool.Get().Do("HSET", "sessions", r.sid, login) } diff --git a/task/user.go b/task/user.go index 0feccf6..c86ef1e 100644 --- a/task/user.go +++ b/task/user.go @@ -18,11 +18,21 @@ func FetchUserInfo(token, login string) (u *db.User, err error) { } saveResponseMeta(token, resp) + name := "" + if n := user.Name; n != nil { + name = *user.Name + } + + avatarURL := "" + if url := user.AvatarURL; url != nil { + avatarURL = *user.AvatarURL + } + u = &db.User{ Login: *user.Login, - Name: *user.Name, + Name: name, ID: uint64(*user.ID), - AvatarURL: *user.AvatarURL, + AvatarURL: avatarURL, } return