aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/graphql/resolver/auth_helpers.go
blob: 433e9e90c23cf4597652baf46c7ed2e22adb705d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package resolver

import (
	"context"
	"errors"
	"fmt"

	"github.com/labstack/echo/v4"
	"golang.org/x/crypto/bcrypt"
	appcontext "undef.ninja/x/feedaka/context"
)

// getUserIDFromContext retrieves the authenticated user ID from context
// This is a wrapper around the GetUserID function from the context package
func getUserIDFromContext(ctx context.Context) (int64, error) {
	userID, ok := appcontext.GetUserID(ctx)
	if !ok {
		return 0, fmt.Errorf("authentication required")
	}
	return userID, nil
}

// Helper function to get Echo context from GraphQL context
func getEchoContext(ctx context.Context) (echo.Context, error) {
	echoCtx, ok := ctx.Value("echo").(echo.Context)
	if !ok {
		return nil, errors.New("echo context not found")
	}
	return echoCtx, nil
}

func verifyPassword(hashedPassword, password string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
	return err == nil
}