mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-10 20:56:50 +00:00
78 lines
2 KiB
Go
78 lines
2 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/markbates/goth/gothic"
|
|
|
|
"github.com/rjNemo/rentease/internal/auth"
|
|
)
|
|
|
|
var indexTemplate = `{{range $key,$value:=.Providers}}
|
|
<p><a href="/auth?provider={{$value}}">Log in with {{index $.ProvidersMap $value}}</a></p>
|
|
{{end}}`
|
|
|
|
var userTemplate = `
|
|
<p><a href="/logout?provider={{.Provider}}">logout</a></p>
|
|
<p>Name: {{.Name}} [{{.LastName}}, {{.FirstName}}]</p>
|
|
<p>Email: {{.Email}}</p>
|
|
<p>NickName: {{.NickName}}</p>
|
|
<p>Location: {{.Location}}</p>
|
|
<p>AvatarURL: {{.AvatarURL}} <img src="{{.AvatarURL}}"></p>
|
|
<p>Description: {{.Description}}</p>
|
|
<p>UserID: {{.UserID}}</p>
|
|
<p>AccessToken: {{.AccessToken}}</p>
|
|
<p>ExpiresAt: {{.ExpiresAt}}</p>
|
|
<p>RefreshToken: {{.RefreshToken}}</p>
|
|
`
|
|
|
|
func handleProviderCallback() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
res := c.Response()
|
|
req := c.Request()
|
|
user, err := gothic.CompleteUserAuth(res, req)
|
|
if err != nil {
|
|
fmt.Fprintln(res, err)
|
|
return nil
|
|
}
|
|
t, _ := template.New("foo").Parse(userTemplate)
|
|
return t.Execute(res, user)
|
|
}
|
|
}
|
|
|
|
func handleProviderLogout() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
res := c.Response()
|
|
req := c.Request()
|
|
err := gothic.Logout(res, req)
|
|
res.Header().Set("Location", "/")
|
|
res.WriteHeader(http.StatusTemporaryRedirect)
|
|
return err
|
|
}
|
|
}
|
|
|
|
func handleProvider() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
res := c.Response()
|
|
req := c.Request()
|
|
// try to get the user without re-authenticating
|
|
if gothUser, err := gothic.CompleteUserAuth(res, req); err == nil {
|
|
t, _ := template.New("foo").Parse(userTemplate)
|
|
return t.Execute(res, gothUser)
|
|
} else {
|
|
gothic.BeginAuthHandler(res, req)
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func handleLoginPage(as *auth.Service) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
t, _ := template.New("foo").Parse(indexTemplate)
|
|
return t.Execute(c.Response(), as.GetProviderIndex())
|
|
//return renderTempl(c, http.StatusOK, view.Login(as.GetProviderIndex()))
|
|
}
|
|
}
|