-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
36 lines (30 loc) · 1.04 KB
/
authentication.go
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
36
package middlewares
import (
"context"
"errors"
"github.com/Baja-KS/WebshopAPI-AuthenticationService/internal/database"
"github.com/Baja-KS/WebshopAPI-AuthenticationService/internal/service"
"gorm.io/gorm"
)
type AuthenticationMiddleware struct {
DB *gorm.DB
Next service.Service
}
func (a *AuthenticationMiddleware) Login(ctx context.Context, username string, password string) (database.UserOut, string, error) {
return a.Next.Login(ctx, username, password)
}
func (a *AuthenticationMiddleware) Register(ctx context.Context, user database.UserIn) (string, error) {
if !database.AuthenticateUser(ctx, a.DB) {
return "Unauthorized", errors.New("unauthorized")
}
return a.Next.Register(ctx, user)
}
func (a *AuthenticationMiddleware) GetAll(ctx context.Context) ([]database.UserOut, error) {
if !database.AuthenticateUser(ctx, a.DB) {
return []database.UserOut{}, errors.New("unauthorized")
}
return a.Next.GetAll(ctx)
}
func (a *AuthenticationMiddleware) AuthUser(ctx context.Context) (database.UserOut, error) {
return a.Next.AuthUser(ctx)
}