gistea/router/oauth.go

42 lines
1.0 KiB
Go

package router
import (
"net/http"
"github.com/markbates/goth/gothic"
)
func (a *App) login(w http.ResponseWriter, r *http.Request) {
user, err := gothic.CompleteUserAuth(w, r)
if err != nil {
gothic.BeginAuthHandler(w, r)
return
}
if err := a.store.CompleteAuth(w, r, user.AccessToken); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, a.Domain, http.StatusFound)
}
func (a *App) logout(w http.ResponseWriter, r *http.Request) {
if err := a.store.Logout(w, r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, a.Domain, http.StatusFound)
}
func (a *App) loginCallback(w http.ResponseWriter, r *http.Request) {
user, err := gothic.CompleteUserAuth(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if err := a.store.CompleteAuth(w, r, user.AccessToken); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, a.Domain, http.StatusFound)
}