added loginResult type

This commit is contained in:
Djeeberjr 2021-09-14 16:22:32 +02:00
parent 426531e634
commit 28ff9006df
3 changed files with 39 additions and 4 deletions

View File

@ -10,8 +10,14 @@ import (
"github.com/graphql-go/graphql/language/ast" "github.com/graphql-go/graphql/language/ast"
) )
type LoginResult struct {
Token string `json:"token"`
Successful bool `json:"successful"`
}
var graphqlDirType *graphql.Object var graphqlDirType *graphql.Object
var graphqlFileType *graphql.Object var graphqlFileType *graphql.Object
var graphqlLoginResultType *graphql.Object
// graphqlTypes create all graphql types and stores the in the global variables // graphqlTypes create all graphql types and stores the in the global variables
func graphqlTypes() { func graphqlTypes() {
@ -192,6 +198,22 @@ func graphqlTypes() {
}, nil }, nil
}, },
}) })
graphqlLoginResultType = graphql.NewObject(graphql.ObjectConfig{
Name: "LoginResut",
Description: "Result of a login",
Fields: graphql.Fields{
"token": &graphql.Field{
Type: graphql.String,
Description: "JWT token if login was successful",
},
"successful": &graphql.Field{
Type: graphql.NewNonNull(graphql.Boolean),
Description: "If the login was successful",
},
},
})
} }
// graphqlTypes helper func for using the dataloader to get a file // graphqlTypes helper func for using the dataloader to get a file

View File

@ -193,7 +193,15 @@ func deleteDirectory(ctx context.Context, path string) error {
} }
//login Checks for valid username password combination. Returns singed jwt string //login Checks for valid username password combination. Returns singed jwt string
func login(ctx context.Context, username, password string) (string, error) { func login(ctx context.Context, username, password string) (LoginResult, error) {
// TODO: replace with propper user management
if username != "admin" && password != "hunter2" {
return LoginResult{
Successful: false,
}, nil
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, JWTClaims{ token := jwt.NewWithClaims(jwt.SigningMethodHS256, JWTClaims{
StandardClaims: jwt.StandardClaims{ StandardClaims: jwt.StandardClaims{
Subject: username, Subject: username,
@ -204,8 +212,13 @@ func login(ctx context.Context, username, password string) (string, error) {
tokenString, err := token.SignedString([]byte("TODO")) tokenString, err := token.SignedString([]byte("TODO"))
if err != nil { if err != nil {
return "", err return LoginResult{
Successful: false,
}, err
} }
return tokenString, nil return LoginResult{
Token: tokenString,
Successful: true,
}, nil
} }

View File

@ -184,7 +184,7 @@ func graphqlSchema() (graphql.Schema, error) {
}, },
}, },
"login": &graphql.Field{ "login": &graphql.Field{
Type: graphql.NewNonNull(graphql.String), Type: graphql.NewNonNull(graphqlLoginResultType),
Args: graphql.FieldConfigArgument{ Args: graphql.FieldConfigArgument{
"username": &graphql.ArgumentConfig{ "username": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String), Type: graphql.NewNonNull(graphql.String),