From 28ff9006df0267ff1cd865af79ce30a6f9777954 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 14 Sep 2021 16:22:32 +0200 Subject: [PATCH] added loginResult type --- internal/graphqlTypes.go | 22 ++++++++++++++++++++++ internal/mutations.go | 19 ++++++++++++++++--- internal/schema.go | 2 +- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/internal/graphqlTypes.go b/internal/graphqlTypes.go index a42458e..e479b65 100644 --- a/internal/graphqlTypes.go +++ b/internal/graphqlTypes.go @@ -10,8 +10,14 @@ import ( "github.com/graphql-go/graphql/language/ast" ) +type LoginResult struct { + Token string `json:"token"` + Successful bool `json:"successful"` +} + var graphqlDirType *graphql.Object var graphqlFileType *graphql.Object +var graphqlLoginResultType *graphql.Object // graphqlTypes create all graphql types and stores the in the global variables func graphqlTypes() { @@ -192,6 +198,22 @@ func graphqlTypes() { }, 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 diff --git a/internal/mutations.go b/internal/mutations.go index 6d5d0ec..7d483b3 100644 --- a/internal/mutations.go +++ b/internal/mutations.go @@ -193,7 +193,15 @@ func deleteDirectory(ctx context.Context, path string) error { } //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{ StandardClaims: jwt.StandardClaims{ Subject: username, @@ -204,8 +212,13 @@ func login(ctx context.Context, username, password string) (string, error) { tokenString, err := token.SignedString([]byte("TODO")) if err != nil { - return "", err + return LoginResult{ + Successful: false, + }, err } - return tokenString, nil + return LoginResult{ + Token: tokenString, + Successful: true, + }, nil } diff --git a/internal/schema.go b/internal/schema.go index 6aa6e71..9622188 100644 --- a/internal/schema.go +++ b/internal/schema.go @@ -184,7 +184,7 @@ func graphqlSchema() (graphql.Schema, error) { }, }, "login": &graphql.Field{ - Type: graphql.NewNonNull(graphql.String), + Type: graphql.NewNonNull(graphqlLoginResultType), Args: graphql.FieldConfigArgument{ "username": &graphql.ArgumentConfig{ Type: graphql.NewNonNull(graphql.String),