diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2017-06-25 18:44:37 -0400 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2017-06-25 18:44:37 -0400 |
| commit | c9c5a01c07c428800249f2880e8467c196f694cf (patch) | |
| tree | 081c5c7c994482d043a512944edb62672d9712cf /web.go | |
| parent | 682bba726dca2e33317876c459f6334ddf5f060a (diff) | |
| download | lastfm-api-c9c5a01c07c428800249f2880e8467c196f694cf.tar.gz | |
Progress on AuthGetSession
Diffstat (limited to 'web.go')
| -rw-r--r-- | web.go | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -96,6 +96,11 @@ func (app *App) callback(w http.ResponseWriter, r *http.Request) { s.UserName = user.Name app.InsertUserSession(s) app.SetCookie(w, "session", s, 86400*30) + var lfmtoken string + app.GetCookie(r, "lfmtoken", &lfmtoken) + if lfmtoken != "" { + http.Redirect(w, r, "api/auth", http.StatusTemporaryRedirect) + } if newUser { http.Redirect(w, r, "/settings", http.StatusTemporaryRedirect) } else { @@ -103,6 +108,30 @@ func (app *App) callback(w http.ResponseWriter, r *http.Request) { } } +func (app *App) auth(w http.ResponseWriter, r *http.Request) { + se := new(UserSession) + err := app.GetCookie(r, "session", se) + if err != nil { + app.SetCookie(w, "lfmtoken", r.FormValue("token"), 120) + app.SetCookie(w, "lfmkey", r.FormValue("api_key"), 120) + http.Redirect(w, r, "/login", http.StatusFound) + return + } + key := r.FormValue("api_key") + if c, err := app.GetClient(key); err != nil { + fmt.Printf("%v\n", err) + } else { + if token, err := app.GetToken(r.FormValue("token")); err != nil { + return + } else { + token.UserId = se.UserId + app.PutToken(token) + app.Template.ExecuteTemplate(w, "auth.tmpl", c.Name) + } + } + return +} + func (app *App) settings(w http.ResponseWriter, r *http.Request) { se := new(UserSession) err := app.GetCookie(r, "session", se) |
