aboutsummaryrefslogtreecommitdiffstats
path: root/web.go
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2017-06-12 23:52:26 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2017-06-12 23:52:26 -0400
commita38f778312b392614ec0ccf14f9f2790671b63eb (patch)
tree202dd4df5d1418c285c348f2f1366d339f99a6f0 /web.go
parenta8684a6aade6bac962da35a96e14667cca3c44c5 (diff)
downloadlastfm-api-a38f778312b392614ec0ccf14f9f2790671b63eb.tar.gz
Abstract all DB access via DataStore, fix lfm_password bug
Diffstat (limited to 'web.go')
-rw-r--r--web.go85
1 files changed, 43 insertions, 42 deletions
diff --git a/web.go b/web.go
index 5e17b61..5dc2e70 100644
--- a/web.go
+++ b/web.go
@@ -1,7 +1,6 @@
package main
import (
- "context"
"encoding/hex"
"encoding/json"
"fmt"
@@ -12,10 +11,14 @@ import (
_ "github.com/lib/pq"
)
-type UserInfo struct {
- Sub string `json:"sub"`
- UserName string `json:"given_name"`
- Email string `json:"email"`
+type User struct {
+ Id int
+ Type string
+ OpId string `json:"sub"`
+ Name string `json:"given_name"`
+ Email string `json:"email"`
+ LfmName string
+ LfmPassword string
}
type UserSession struct {
@@ -67,32 +70,26 @@ func (app *App) callback(w http.ResponseWriter, r *http.Request) {
panic(fmt.Errorf("state"))
}
code := r.FormValue("code")
- tok, _ := app.Config.OAuth.Exchange(context.Background(), code)
- client := app.Config.OAuth.Client(context.Background(), tok)
+ tok, _ := app.Config.OAuth.Exchange(r.Context(), code)
+ client := app.Config.OAuth.Client(r.Context(), tok)
resp, _ := client.Get("https://www.googleapis.com/plus/v1/people/me/openIdConnect")
p, _ := ioutil.ReadAll(resp.Body)
- userinfo := new(UserInfo)
- json.Unmarshal(p, userinfo)
+ user := &User{Type: "google"}
+ json.Unmarshal(p, user)
- se := new(UserSession)
- se.Id = hex.EncodeToString(genKey(32))
- row := app.DB.QueryRow("SELECT user_id, name FROM users WHERE type='google' AND op_id=$1",
- userinfo.Sub)
- err := row.Scan(&se.UserId, &se.UserName)
- if err != nil {
- row := app.DB.QueryRow("INSERT into users (type, op_id, name, email)"+
- "values ('google', $1, $2, $3) RETURNING user_id",
- userinfo.Sub, userinfo.UserName, userinfo.Email)
- row.Scan(&se.UserId)
- se.UserName = userinfo.UserName
- }
- app.DB.Exec("INSERT into user_sessions values ($1, $2)", se.Id, se.UserId)
- app.SetCookie(w, "session", se, 86400*30)
- if err != nil {
+ s := &UserSession{Id: hex.EncodeToString(genKey(32))}
+ if err := app.GetUser(user); err != nil {
+ if err := app.InsertUser(user); err != nil {
+ panic(err)
+ }
http.Redirect(w, r, "/settings", http.StatusTemporaryRedirect)
- return
+ } else {
+ http.Redirect(w, r, "/", http.StatusFound)
}
- http.Redirect(w, r, "/", http.StatusFound)
+ s.UserId = user.Id
+ s.UserName = user.Name
+ app.InsertUserSession(s)
+ app.SetCookie(w, "session", s, 86400*30)
}
func (app *App) settings(w http.ResponseWriter, r *http.Request) {
@@ -104,25 +101,29 @@ func (app *App) settings(w http.ResponseWriter, r *http.Request) {
}
if r.Method == "POST" {
- _, err = app.DB.Exec("UPDATE users SET name=$1, email=$2, lfm_name=$3, lfm_password=$4 WHERE user_id=$5",
- r.FormValue("name"), r.FormValue("email"), r.FormValue("lfm_name"),
- md5hex(r.FormValue("lfm_password")), se.UserId)
- if err != nil {
+ u := &User{
+ Id: se.UserId,
+ Name: r.FormValue("name"),
+ Email: r.FormValue("email"),
+ LfmName: r.FormValue("lfm_name"),
+ LfmPassword: md5hex(r.FormValue("lfm_password")),
+ }
+ if err := app.SaveUser(u); err != nil {
log.Println(err)
}
- se.UserName = r.FormValue("name")
+ se.UserName = u.Name
app.SetCookie(w, "session", se, 86400*30)
}
- var userName, email, lfmName, lfmPassword string
- row := app.DB.QueryRow("SELECT name, email, lfm_name, lfm_password FROM users WHERE user_id=$1",
- se.UserId)
- row.Scan(&userName, &email, &lfmName, &lfmPassword)
- app.Template.ExecuteTemplate(w, "settings.tmpl", struct {
- Session *UserSession
- UserName string
- Email string
- LfmName string
- LfmPassword string
- }{se, userName, email, lfmName, lfmPassword})
+ user := &User{Id: se.UserId}
+ if err := app.GetUser(user); err != nil {
+ log.Println(err)
+ }
+ err = app.Template.ExecuteTemplate(w, "settings.tmpl", struct {
+ Session *UserSession
+ *User
+ }{Session: se, User: user})
+ if err != nil {
+ log.Println(err)
+ }
}