save "create sync group" table in database
This commit is contained in:
+10
-34
@@ -4,15 +4,18 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"ruben/inventory2/internal/domains/authentication"
|
||||
"ruben/inventory2/internal/site/cookies"
|
||||
"ruben/inventory2/internal/site/response"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TODO: use a login/logout server? (prefix: '/auth'?)
|
||||
|
||||
// GET /login
|
||||
func (s *Server) loginPage(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
|
||||
u, err := s.newLoginURL(ctx, "/")
|
||||
u, err := newLoginURL(ctx, s.auth, "/")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -20,31 +23,15 @@ func (s *Server) loginPage(r *http.Request) (response.Response, error) {
|
||||
return response.TemporaryRedirect(u), nil
|
||||
}
|
||||
|
||||
func (s *Server) newLoginURL(ctx context.Context, targetURI string) (string, error) {
|
||||
state, err := s.auth.NewState(ctx, targetURI)
|
||||
func newLoginURL(ctx context.Context, auth *authentication.Authenticator, targetURI string) (string, error) {
|
||||
state, err := auth.NewState(ctx, targetURI)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate random state: %w", err)
|
||||
}
|
||||
|
||||
base64EncodedState := fmt.Sprintf("%x", state[:])
|
||||
|
||||
return s.auth.AuthCodeURL(base64EncodedState), nil
|
||||
}
|
||||
|
||||
// POST /login
|
||||
func (s *Server) login(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
email := r.FormValue("email")
|
||||
if email == "" {
|
||||
return nil, response.BadRequest().Msg("no email provided")
|
||||
}
|
||||
|
||||
acct, err := s.accts.GetAccountByEmail(ctx, email)
|
||||
if err != nil {
|
||||
return nil, response.Errorf("failed to create account: %w", err)
|
||||
}
|
||||
|
||||
return response.SeeOther(fmt.Sprintf("/accounts/%d", acct.AccountID)), nil
|
||||
return auth.AuthCodeURL(base64EncodedState), nil
|
||||
}
|
||||
|
||||
// GET /login/callback
|
||||
@@ -64,18 +51,7 @@ func (s *Server) loginCallback(r *http.Request) (response.Response, error) {
|
||||
// set access_token cookie and redirect to a reasonable place
|
||||
|
||||
return response.TemporaryRedirect(targetURI).
|
||||
Cookie(newAccessTokenCookie(accessToken, expiration)), nil
|
||||
}
|
||||
|
||||
func newAccessTokenCookie(tkn string, expiration time.Time) http.Cookie {
|
||||
return http.Cookie{
|
||||
Name: "access_token",
|
||||
Value: tkn,
|
||||
Path: "/",
|
||||
Expires: expiration,
|
||||
MaxAge: 0, // using Expiration instead
|
||||
Secure: true,
|
||||
}
|
||||
Cookie(cookies.AccessToken(accessToken, expiration)), nil
|
||||
}
|
||||
|
||||
// GET /logout
|
||||
@@ -92,5 +68,5 @@ func (s *Server) logoutPage(r *http.Request) (response.Response, error) {
|
||||
}
|
||||
|
||||
return response.TemporaryRedirect(s.auth.GetLogoutURL(host).String()).
|
||||
Cookie(getExpiredCookie("access_token")), nil
|
||||
Cookie(cookies.Expired("access_token")), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user