feat: improve error handling
continuous-integration/drone/push Build is passing Details

main
RaviAnand Mohabir 1 year ago
parent a7d54e85a4
commit ca8805a6bc

@ -2,6 +2,7 @@ package auth
import (
"context"
"fmt"
"net/http"
"gitea.ravianand.me/Dan6erbond/listy/core"
@ -23,6 +24,7 @@ func Redirect(app *core.App) func(w http.ResponseWriter, r *http.Request) {
session.Values["state"] = state
if err := session.Save(r, w); err != nil {
fmt.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@ -46,16 +48,30 @@ func Callback(app *core.App) func(w http.ResponseWriter, r *http.Request) {
// use the same state string here that you used to generate the URL
token, err := app.SpotifyAuth.Token(r.Context(), state.(string), r)
if err != nil {
fmt.Println(err)
http.Error(w, "Couldn't get token", http.StatusNotFound)
return
}
spotifyClient := spotify.New(app.SpotifyAuth.Client(r.Context(), token))
user, _ := spotifyClient.CurrentUser(ctx)
user, err := spotifyClient.CurrentUser(ctx)
if err != nil {
fmt.Println(err)
http.Error(w, "Couldn't get current user", http.StatusNotFound)
return
}
users.SaveUserToken(ctx, app, user.ID, token)
_, err = users.SaveUserToken(ctx, app, user.ID, token)
if err != nil {
fmt.Println(err)
http.Error(w, "Couldn't save token", http.StatusNotFound)
return
}
app.Render.JSON(w, 200, user)
}

Loading…
Cancel
Save