added loginResult type
This commit is contained in:
parent
426531e634
commit
28ff9006df
@ -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
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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),
|
||||||
|
Loading…
Reference in New Issue
Block a user