From a0171ad500428b6be26045fbd8d703ac3083dde5 Mon Sep 17 00:00:00 2001 From: Lakhan Samani Date: Wed, 28 Jul 2021 15:43:08 +0530 Subject: [PATCH] feat:allow signup without verification (#39) * fix: add disable basic auth check in resolvers * feat: allow signup without email verification Resolves #32 --- server/graph/generated/generated.go | 472 ++++++++++++++-------------- server/graph/model/models_gen.go | 14 +- server/graph/schema.graphqls | 10 +- server/graph/schema.resolvers.go | 8 +- server/resolvers/forgotPassword.go | 5 + server/resolvers/login.go | 11 +- server/resolvers/resetPassword.go | 4 + server/resolvers/signup.go | 89 ++++-- server/resolvers/token.go | 6 +- server/resolvers/verifyEmail.go | 6 +- 10 files changed, 344 insertions(+), 281 deletions(-) diff --git a/server/graph/generated/generated.go b/server/graph/generated/generated.go index 6fd8fc4..a04b88a 100644 --- a/server/graph/generated/generated.go +++ b/server/graph/generated/generated.go @@ -43,18 +43,18 @@ type DirectiveRoot struct { } type ComplexityRoot struct { - Error struct { - Message func(childComplexity int) int - Reason func(childComplexity int) int - } - - LoginResponse struct { + AuthResponse struct { AccessToken func(childComplexity int) int AccessTokenExpiresAt func(childComplexity int) int Message func(childComplexity int) int User func(childComplexity int) int } + Error struct { + Message func(childComplexity int) int + Reason func(childComplexity int) int + } + Meta struct { IsBasicAuthenticationEnabled func(childComplexity int) int IsEmailVerificationEnabled func(childComplexity int) int @@ -112,11 +112,11 @@ type ComplexityRoot struct { } type MutationResolver interface { - Signup(ctx context.Context, params model.SignUpInput) (*model.Response, error) - Login(ctx context.Context, params model.LoginInput) (*model.LoginResponse, error) + Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse, error) + Login(ctx context.Context, params model.LoginInput) (*model.AuthResponse, error) Logout(ctx context.Context) (*model.Response, error) UpdateProfile(ctx context.Context, params model.UpdateProfileInput) (*model.Response, error) - VerifyEmail(ctx context.Context, params model.VerifyEmailInput) (*model.LoginResponse, error) + VerifyEmail(ctx context.Context, params model.VerifyEmailInput) (*model.AuthResponse, error) ResendVerifyEmail(ctx context.Context, params model.ResendVerifyEmailInput) (*model.Response, error) ForgotPassword(ctx context.Context, params model.ForgotPasswordInput) (*model.Response, error) ResetPassword(ctx context.Context, params model.ResetPassowrdInput) (*model.Response, error) @@ -124,7 +124,7 @@ type MutationResolver interface { type QueryResolver interface { Meta(ctx context.Context) (*model.Meta, error) Users(ctx context.Context) ([]*model.User, error) - Token(ctx context.Context) (*model.LoginResponse, error) + Token(ctx context.Context) (*model.AuthResponse, error) Profile(ctx context.Context) (*model.User, error) VerificationRequests(ctx context.Context) ([]*model.VerificationRequest, error) } @@ -144,6 +144,34 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in _ = ec switch typeName + "." + field { + case "AuthResponse.accessToken": + if e.complexity.AuthResponse.AccessToken == nil { + break + } + + return e.complexity.AuthResponse.AccessToken(childComplexity), true + + case "AuthResponse.accessTokenExpiresAt": + if e.complexity.AuthResponse.AccessTokenExpiresAt == nil { + break + } + + return e.complexity.AuthResponse.AccessTokenExpiresAt(childComplexity), true + + case "AuthResponse.message": + if e.complexity.AuthResponse.Message == nil { + break + } + + return e.complexity.AuthResponse.Message(childComplexity), true + + case "AuthResponse.user": + if e.complexity.AuthResponse.User == nil { + break + } + + return e.complexity.AuthResponse.User(childComplexity), true + case "Error.message": if e.complexity.Error.Message == nil { break @@ -158,34 +186,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Error.Reason(childComplexity), true - case "LoginResponse.accessToken": - if e.complexity.LoginResponse.AccessToken == nil { - break - } - - return e.complexity.LoginResponse.AccessToken(childComplexity), true - - case "LoginResponse.accessTokenExpiresAt": - if e.complexity.LoginResponse.AccessTokenExpiresAt == nil { - break - } - - return e.complexity.LoginResponse.AccessTokenExpiresAt(childComplexity), true - - case "LoginResponse.message": - if e.complexity.LoginResponse.Message == nil { - break - } - - return e.complexity.LoginResponse.Message(childComplexity), true - - case "LoginResponse.user": - if e.complexity.LoginResponse.User == nil { - break - } - - return e.complexity.LoginResponse.User(childComplexity), true - case "Meta.isBasicAuthenticationEnabled": if e.complexity.Meta.IsBasicAuthenticationEnabled == nil { break @@ -586,7 +586,7 @@ type Error { reason: String! } -type LoginResponse { +type AuthResponse { message: String! accessToken: String accessTokenExpiresAt: Int64 @@ -640,11 +640,11 @@ input ResetPassowrdInput { } type Mutation { - signup(params: SignUpInput!): Response! - login(params: LoginInput!): LoginResponse! + signup(params: SignUpInput!): AuthResponse! + login(params: LoginInput!): AuthResponse! logout: Response! updateProfile(params: UpdateProfileInput!): Response! - verifyEmail(params: VerifyEmailInput!): LoginResponse! + verifyEmail(params: VerifyEmailInput!): AuthResponse! resendVerifyEmail(params: ResendVerifyEmailInput!): Response! forgotPassword(params: ForgotPasswordInput!): Response! resetPassword(params: ResetPassowrdInput!): Response! @@ -653,7 +653,7 @@ type Mutation { type Query { meta: Meta! users: [User!]! - token: LoginResponse + token: AuthResponse profile: User! verificationRequests: [VerificationRequest!]! } @@ -823,6 +823,137 @@ func (ec *executionContext) field___Type_fields_args(ctx context.Context, rawArg // region **************************** field.gotpl ***************************** +func (ec *executionContext) _AuthResponse_message(ctx context.Context, field graphql.CollectedField, obj *model.AuthResponse) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "AuthResponse", + Field: field, + Args: nil, + IsMethod: false, + IsResolver: false, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Message, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(string) + fc.Result = res + return ec.marshalNString2string(ctx, field.Selections, res) +} + +func (ec *executionContext) _AuthResponse_accessToken(ctx context.Context, field graphql.CollectedField, obj *model.AuthResponse) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "AuthResponse", + Field: field, + Args: nil, + IsMethod: false, + IsResolver: false, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.AccessToken, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*string) + fc.Result = res + return ec.marshalOString2ᚖstring(ctx, field.Selections, res) +} + +func (ec *executionContext) _AuthResponse_accessTokenExpiresAt(ctx context.Context, field graphql.CollectedField, obj *model.AuthResponse) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "AuthResponse", + Field: field, + Args: nil, + IsMethod: false, + IsResolver: false, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.AccessTokenExpiresAt, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*int64) + fc.Result = res + return ec.marshalOInt642ᚖint64(ctx, field.Selections, res) +} + +func (ec *executionContext) _AuthResponse_user(ctx context.Context, field graphql.CollectedField, obj *model.AuthResponse) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "AuthResponse", + Field: field, + Args: nil, + IsMethod: false, + IsResolver: false, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.User, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*model.User) + fc.Result = res + return ec.marshalOUser2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐUser(ctx, field.Selections, res) +} + func (ec *executionContext) _Error_message(ctx context.Context, field graphql.CollectedField, obj *model.Error) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -893,137 +1024,6 @@ func (ec *executionContext) _Error_reason(ctx context.Context, field graphql.Col return ec.marshalNString2string(ctx, field.Selections, res) } -func (ec *executionContext) _LoginResponse_message(ctx context.Context, field graphql.CollectedField, obj *model.LoginResponse) (ret graphql.Marshaler) { - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - }() - fc := &graphql.FieldContext{ - Object: "LoginResponse", - Field: field, - Args: nil, - IsMethod: false, - IsResolver: false, - } - - ctx = graphql.WithFieldContext(ctx, fc) - resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return obj.Message, nil - }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if resTmp == nil { - if !graphql.HasFieldError(ctx, fc) { - ec.Errorf(ctx, "must not be null") - } - return graphql.Null - } - res := resTmp.(string) - fc.Result = res - return ec.marshalNString2string(ctx, field.Selections, res) -} - -func (ec *executionContext) _LoginResponse_accessToken(ctx context.Context, field graphql.CollectedField, obj *model.LoginResponse) (ret graphql.Marshaler) { - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - }() - fc := &graphql.FieldContext{ - Object: "LoginResponse", - Field: field, - Args: nil, - IsMethod: false, - IsResolver: false, - } - - ctx = graphql.WithFieldContext(ctx, fc) - resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return obj.AccessToken, nil - }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if resTmp == nil { - return graphql.Null - } - res := resTmp.(*string) - fc.Result = res - return ec.marshalOString2ᚖstring(ctx, field.Selections, res) -} - -func (ec *executionContext) _LoginResponse_accessTokenExpiresAt(ctx context.Context, field graphql.CollectedField, obj *model.LoginResponse) (ret graphql.Marshaler) { - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - }() - fc := &graphql.FieldContext{ - Object: "LoginResponse", - Field: field, - Args: nil, - IsMethod: false, - IsResolver: false, - } - - ctx = graphql.WithFieldContext(ctx, fc) - resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return obj.AccessTokenExpiresAt, nil - }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if resTmp == nil { - return graphql.Null - } - res := resTmp.(*int64) - fc.Result = res - return ec.marshalOInt642ᚖint64(ctx, field.Selections, res) -} - -func (ec *executionContext) _LoginResponse_user(ctx context.Context, field graphql.CollectedField, obj *model.LoginResponse) (ret graphql.Marshaler) { - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - }() - fc := &graphql.FieldContext{ - Object: "LoginResponse", - Field: field, - Args: nil, - IsMethod: false, - IsResolver: false, - } - - ctx = graphql.WithFieldContext(ctx, fc) - resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return obj.User, nil - }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if resTmp == nil { - return graphql.Null - } - res := resTmp.(*model.User) - fc.Result = res - return ec.marshalOUser2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐUser(ctx, field.Selections, res) -} - func (ec *executionContext) _Meta_version(ctx context.Context, field graphql.CollectedField, obj *model.Meta) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -1306,9 +1306,9 @@ func (ec *executionContext) _Mutation_signup(ctx context.Context, field graphql. } return graphql.Null } - res := resTmp.(*model.Response) + res := resTmp.(*model.AuthResponse) fc.Result = res - return ec.marshalNResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐResponse(ctx, field.Selections, res) + return ec.marshalNAuthResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐAuthResponse(ctx, field.Selections, res) } func (ec *executionContext) _Mutation_login(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { @@ -1348,9 +1348,9 @@ func (ec *executionContext) _Mutation_login(ctx context.Context, field graphql.C } return graphql.Null } - res := resTmp.(*model.LoginResponse) + res := resTmp.(*model.AuthResponse) fc.Result = res - return ec.marshalNLoginResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐLoginResponse(ctx, field.Selections, res) + return ec.marshalNAuthResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐAuthResponse(ctx, field.Selections, res) } func (ec *executionContext) _Mutation_logout(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { @@ -1467,9 +1467,9 @@ func (ec *executionContext) _Mutation_verifyEmail(ctx context.Context, field gra } return graphql.Null } - res := resTmp.(*model.LoginResponse) + res := resTmp.(*model.AuthResponse) fc.Result = res - return ec.marshalNLoginResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐLoginResponse(ctx, field.Selections, res) + return ec.marshalNAuthResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐAuthResponse(ctx, field.Selections, res) } func (ec *executionContext) _Mutation_resendVerifyEmail(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { @@ -1695,9 +1695,9 @@ func (ec *executionContext) _Query_token(ctx context.Context, field graphql.Coll if resTmp == nil { return graphql.Null } - res := resTmp.(*model.LoginResponse) + res := resTmp.(*model.AuthResponse) fc.Result = res - return ec.marshalOLoginResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐLoginResponse(ctx, field.Selections, res) + return ec.marshalOAuthResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐAuthResponse(ctx, field.Selections, res) } func (ec *executionContext) _Query_profile(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { @@ -3747,6 +3747,39 @@ func (ec *executionContext) unmarshalInputVerifyEmailInput(ctx context.Context, // region **************************** object.gotpl **************************** +var authResponseImplementors = []string{"AuthResponse"} + +func (ec *executionContext) _AuthResponse(ctx context.Context, sel ast.SelectionSet, obj *model.AuthResponse) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, authResponseImplementors) + + out := graphql.NewFieldSet(fields) + var invalids uint32 + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("AuthResponse") + case "message": + out.Values[i] = ec._AuthResponse_message(ctx, field, obj) + if out.Values[i] == graphql.Null { + invalids++ + } + case "accessToken": + out.Values[i] = ec._AuthResponse_accessToken(ctx, field, obj) + case "accessTokenExpiresAt": + out.Values[i] = ec._AuthResponse_accessTokenExpiresAt(ctx, field, obj) + case "user": + out.Values[i] = ec._AuthResponse_user(ctx, field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch() + if invalids > 0 { + return graphql.Null + } + return out +} + var errorImplementors = []string{"Error"} func (ec *executionContext) _Error(ctx context.Context, sel ast.SelectionSet, obj *model.Error) graphql.Marshaler { @@ -3779,39 +3812,6 @@ func (ec *executionContext) _Error(ctx context.Context, sel ast.SelectionSet, ob return out } -var loginResponseImplementors = []string{"LoginResponse"} - -func (ec *executionContext) _LoginResponse(ctx context.Context, sel ast.SelectionSet, obj *model.LoginResponse) graphql.Marshaler { - fields := graphql.CollectFields(ec.OperationContext, sel, loginResponseImplementors) - - out := graphql.NewFieldSet(fields) - var invalids uint32 - for i, field := range fields { - switch field.Name { - case "__typename": - out.Values[i] = graphql.MarshalString("LoginResponse") - case "message": - out.Values[i] = ec._LoginResponse_message(ctx, field, obj) - if out.Values[i] == graphql.Null { - invalids++ - } - case "accessToken": - out.Values[i] = ec._LoginResponse_accessToken(ctx, field, obj) - case "accessTokenExpiresAt": - out.Values[i] = ec._LoginResponse_accessTokenExpiresAt(ctx, field, obj) - case "user": - out.Values[i] = ec._LoginResponse_user(ctx, field, obj) - default: - panic("unknown field " + strconv.Quote(field.Name)) - } - } - out.Dispatch() - if invalids > 0 { - return graphql.Null - } - return out -} - var metaImplementors = []string{"Meta"} func (ec *executionContext) _Meta(ctx context.Context, sel ast.SelectionSet, obj *model.Meta) graphql.Marshaler { @@ -4392,6 +4392,20 @@ func (ec *executionContext) ___Type(ctx context.Context, sel ast.SelectionSet, o // region ***************************** type.gotpl ***************************** +func (ec *executionContext) marshalNAuthResponse2githubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐAuthResponse(ctx context.Context, sel ast.SelectionSet, v model.AuthResponse) graphql.Marshaler { + return ec._AuthResponse(ctx, sel, &v) +} + +func (ec *executionContext) marshalNAuthResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐAuthResponse(ctx context.Context, sel ast.SelectionSet, v *model.AuthResponse) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + return ec._AuthResponse(ctx, sel, v) +} + func (ec *executionContext) unmarshalNBoolean2bool(ctx context.Context, v interface{}) (bool, error) { res, err := graphql.UnmarshalBoolean(v) return res, graphql.ErrorOnPath(ctx, err) @@ -4432,20 +4446,6 @@ func (ec *executionContext) unmarshalNLoginInput2githubᚗcomᚋauthorizerdevᚋ return res, graphql.ErrorOnPath(ctx, err) } -func (ec *executionContext) marshalNLoginResponse2githubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐLoginResponse(ctx context.Context, sel ast.SelectionSet, v model.LoginResponse) graphql.Marshaler { - return ec._LoginResponse(ctx, sel, &v) -} - -func (ec *executionContext) marshalNLoginResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐLoginResponse(ctx context.Context, sel ast.SelectionSet, v *model.LoginResponse) graphql.Marshaler { - if v == nil { - if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { - ec.Errorf(ctx, "must not be null") - } - return graphql.Null - } - return ec._LoginResponse(ctx, sel, v) -} - func (ec *executionContext) marshalNMeta2githubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐMeta(ctx context.Context, sel ast.SelectionSet, v model.Meta) graphql.Marshaler { return ec._Meta(ctx, sel, &v) } @@ -4841,6 +4841,13 @@ func (ec *executionContext) marshalN__TypeKind2string(ctx context.Context, sel a return res } +func (ec *executionContext) marshalOAuthResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐAuthResponse(ctx context.Context, sel ast.SelectionSet, v *model.AuthResponse) graphql.Marshaler { + if v == nil { + return graphql.Null + } + return ec._AuthResponse(ctx, sel, v) +} + func (ec *executionContext) unmarshalOBoolean2bool(ctx context.Context, v interface{}) (bool, error) { res, err := graphql.UnmarshalBoolean(v) return res, graphql.ErrorOnPath(ctx, err) @@ -4880,13 +4887,6 @@ func (ec *executionContext) marshalOInt642ᚖint64(ctx context.Context, sel ast. return graphql.MarshalInt64(*v) } -func (ec *executionContext) marshalOLoginResponse2ᚖgithubᚗcomᚋauthorizerdevᚋauthorizerᚋserverᚋgraphᚋmodelᚐLoginResponse(ctx context.Context, sel ast.SelectionSet, v *model.LoginResponse) graphql.Marshaler { - if v == nil { - return graphql.Null - } - return ec._LoginResponse(ctx, sel, v) -} - func (ec *executionContext) unmarshalOString2string(ctx context.Context, v interface{}) (string, error) { res, err := graphql.UnmarshalString(v) return res, graphql.ErrorOnPath(ctx, err) diff --git a/server/graph/model/models_gen.go b/server/graph/model/models_gen.go index 7121e2a..46964d9 100644 --- a/server/graph/model/models_gen.go +++ b/server/graph/model/models_gen.go @@ -2,6 +2,13 @@ package model +type AuthResponse struct { + Message string `json:"message"` + AccessToken *string `json:"accessToken"` + AccessTokenExpiresAt *int64 `json:"accessTokenExpiresAt"` + User *User `json:"user"` +} + type Error struct { Message string `json:"message"` Reason string `json:"reason"` @@ -16,13 +23,6 @@ type LoginInput struct { Password string `json:"password"` } -type LoginResponse struct { - Message string `json:"message"` - AccessToken *string `json:"accessToken"` - AccessTokenExpiresAt *int64 `json:"accessTokenExpiresAt"` - User *User `json:"user"` -} - type Meta struct { Version string `json:"version"` IsGoogleLoginEnabled bool `json:"isGoogleLoginEnabled"` diff --git a/server/graph/schema.graphqls b/server/graph/schema.graphqls index ba5d5aa..133bde3 100644 --- a/server/graph/schema.graphqls +++ b/server/graph/schema.graphqls @@ -40,7 +40,7 @@ type Error { reason: String! } -type LoginResponse { +type AuthResponse { message: String! accessToken: String accessTokenExpiresAt: Int64 @@ -94,11 +94,11 @@ input ResetPassowrdInput { } type Mutation { - signup(params: SignUpInput!): Response! - login(params: LoginInput!): LoginResponse! + signup(params: SignUpInput!): AuthResponse! + login(params: LoginInput!): AuthResponse! logout: Response! updateProfile(params: UpdateProfileInput!): Response! - verifyEmail(params: VerifyEmailInput!): LoginResponse! + verifyEmail(params: VerifyEmailInput!): AuthResponse! resendVerifyEmail(params: ResendVerifyEmailInput!): Response! forgotPassword(params: ForgotPasswordInput!): Response! resetPassword(params: ResetPassowrdInput!): Response! @@ -107,7 +107,7 @@ type Mutation { type Query { meta: Meta! users: [User!]! - token: LoginResponse + token: AuthResponse profile: User! verificationRequests: [VerificationRequest!]! } diff --git a/server/graph/schema.resolvers.go b/server/graph/schema.resolvers.go index 1e1b4f5..01f8f07 100644 --- a/server/graph/schema.resolvers.go +++ b/server/graph/schema.resolvers.go @@ -11,11 +11,11 @@ import ( "github.com/authorizerdev/authorizer/server/resolvers" ) -func (r *mutationResolver) Signup(ctx context.Context, params model.SignUpInput) (*model.Response, error) { +func (r *mutationResolver) Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse, error) { return resolvers.Signup(ctx, params) } -func (r *mutationResolver) Login(ctx context.Context, params model.LoginInput) (*model.LoginResponse, error) { +func (r *mutationResolver) Login(ctx context.Context, params model.LoginInput) (*model.AuthResponse, error) { return resolvers.Login(ctx, params) } @@ -27,7 +27,7 @@ func (r *mutationResolver) UpdateProfile(ctx context.Context, params model.Updat return resolvers.UpdateProfile(ctx, params) } -func (r *mutationResolver) VerifyEmail(ctx context.Context, params model.VerifyEmailInput) (*model.LoginResponse, error) { +func (r *mutationResolver) VerifyEmail(ctx context.Context, params model.VerifyEmailInput) (*model.AuthResponse, error) { return resolvers.VerifyEmail(ctx, params) } @@ -51,7 +51,7 @@ func (r *queryResolver) Users(ctx context.Context) ([]*model.User, error) { return resolvers.Users(ctx) } -func (r *queryResolver) Token(ctx context.Context) (*model.LoginResponse, error) { +func (r *queryResolver) Token(ctx context.Context) (*model.AuthResponse, error) { return resolvers.Token(ctx) } diff --git a/server/resolvers/forgotPassword.go b/server/resolvers/forgotPassword.go index a83debb..4d2de00 100644 --- a/server/resolvers/forgotPassword.go +++ b/server/resolvers/forgotPassword.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/db" "github.com/authorizerdev/authorizer/server/enum" "github.com/authorizerdev/authorizer/server/graph/model" @@ -15,6 +16,10 @@ import ( func ForgotPassword(ctx context.Context, params model.ForgotPasswordInput) (*model.Response, error) { var res *model.Response + if constants.DISABLE_BASIC_AUTHENTICATION == "true" { + return res, fmt.Errorf(`basic authentication is disabled for this instance`) + } + params.Email = strings.ToLower(params.Email) if !utils.IsValidEmail(params.Email) { diff --git a/server/resolvers/login.go b/server/resolvers/login.go index d86819a..6211885 100644 --- a/server/resolvers/login.go +++ b/server/resolvers/login.go @@ -6,6 +6,7 @@ import ( "log" "strings" + "github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/db" "github.com/authorizerdev/authorizer/server/enum" "github.com/authorizerdev/authorizer/server/graph/model" @@ -14,13 +15,17 @@ import ( "golang.org/x/crypto/bcrypt" ) -func Login(ctx context.Context, params model.LoginInput) (*model.LoginResponse, error) { +func Login(ctx context.Context, params model.LoginInput) (*model.AuthResponse, error) { gc, err := utils.GinContextFromContext(ctx) - var res *model.LoginResponse + var res *model.AuthResponse if err != nil { return res, err } + if constants.DISABLE_BASIC_AUTHENTICATION == "true" { + return res, fmt.Errorf(`basic authentication is disabled for this instance`) + } + params.Email = strings.ToLower(params.Email) user, err := db.Mgr.GetUserByEmail(params.Email) if err != nil { @@ -54,7 +59,7 @@ func Login(ctx context.Context, params model.LoginInput) (*model.LoginResponse, session.SetToken(userIdStr, refreshToken) - res = &model.LoginResponse{ + res = &model.AuthResponse{ Message: `Logged in successfully`, AccessToken: &accessToken, AccessTokenExpiresAt: &expiresAt, diff --git a/server/resolvers/resetPassword.go b/server/resolvers/resetPassword.go index d42242d..a55caa2 100644 --- a/server/resolvers/resetPassword.go +++ b/server/resolvers/resetPassword.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/db" "github.com/authorizerdev/authorizer/server/graph/model" "github.com/authorizerdev/authorizer/server/utils" @@ -11,6 +12,9 @@ import ( func ResetPassword(ctx context.Context, params model.ResetPassowrdInput) (*model.Response, error) { var res *model.Response + if constants.DISABLE_BASIC_AUTHENTICATION == "true" { + return res, fmt.Errorf(`basic authentication is disabled for this instance`) + } if params.Password != params.ConfirmPassword { return res, fmt.Errorf(`passwords don't match`) diff --git a/server/resolvers/signup.go b/server/resolvers/signup.go index 359d6a0..2e00ac7 100644 --- a/server/resolvers/signup.go +++ b/server/resolvers/signup.go @@ -7,14 +7,24 @@ import ( "strings" "time" + "github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/db" "github.com/authorizerdev/authorizer/server/enum" "github.com/authorizerdev/authorizer/server/graph/model" + "github.com/authorizerdev/authorizer/server/session" "github.com/authorizerdev/authorizer/server/utils" ) -func Signup(ctx context.Context, params model.SignUpInput) (*model.Response, error) { - var res *model.Response +func Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse, error) { + gc, err := utils.GinContextFromContext(ctx) + var res *model.AuthResponse + if err != nil { + return res, err + } + + if constants.DISABLE_BASIC_AUTHENTICATION == "true" { + return res, fmt.Errorf(`basic authentication is disabled for this instance`) + } if params.ConfirmPassword != params.Password { return res, fmt.Errorf(`passowrd and confirm password does not match`) } @@ -51,31 +61,70 @@ func Signup(ctx context.Context, params model.SignUpInput) (*model.Response, err } user.SignupMethod = enum.BasicAuth.String() + if constants.DISABLE_EMAIL_VERICATION == "true" { + user.EmailVerifiedAt = time.Now().Unix() + } _, err = db.Mgr.SaveUser(user) if err != nil { return res, err } - - // insert verification request - verificationType := enum.BasicAuthSignup.String() - token, err := utils.CreateVerificationToken(params.Email, verificationType) - if err != nil { - log.Println(`Error generating token`, err) + userIdStr := fmt.Sprintf("%d", user.ID) + userToReturn := &model.User{ + ID: userIdStr, + Email: user.Email, + Image: &user.Image, + FirstName: &user.FirstName, + LastName: &user.LastName, + SignupMethod: user.SignupMethod, + EmailVerifiedAt: &user.EmailVerifiedAt, + CreatedAt: &user.CreatedAt, + UpdatedAt: &user.UpdatedAt, } - db.Mgr.AddVerification(db.VerificationRequest{ - Token: token, - Identifier: verificationType, - ExpiresAt: time.Now().Add(time.Minute * 30).Unix(), - Email: params.Email, - }) - // exec it as go routin so that we can reduce the api latency - go func() { - utils.SendVerificationMail(params.Email, token) - }() + if constants.DISABLE_EMAIL_VERICATION != "true" { + // insert verification request + verificationType := enum.BasicAuthSignup.String() + token, err := utils.CreateVerificationToken(params.Email, verificationType) + if err != nil { + log.Println(`Error generating token`, err) + } + db.Mgr.AddVerification(db.VerificationRequest{ + Token: token, + Identifier: verificationType, + ExpiresAt: time.Now().Add(time.Minute * 30).Unix(), + Email: params.Email, + }) - res = &model.Response{ - Message: `Verification email has been sent. Please check your inbox`, + // exec it as go routin so that we can reduce the api latency + go func() { + utils.SendVerificationMail(params.Email, token) + }() + + res = &model.AuthResponse{ + Message: `Verification email has been sent. Please check your inbox`, + User: userToReturn, + } + } else { + + refreshToken, _, _ := utils.CreateAuthToken(utils.UserAuthInfo{ + ID: userIdStr, + Email: user.Email, + }, enum.RefreshToken) + + accessToken, expiresAt, _ := utils.CreateAuthToken(utils.UserAuthInfo{ + ID: userIdStr, + Email: user.Email, + }, enum.AccessToken) + + session.SetToken(userIdStr, refreshToken) + res = &model.AuthResponse{ + Message: `Signed up successfully.`, + AccessToken: &accessToken, + AccessTokenExpiresAt: &expiresAt, + User: userToReturn, + } + + utils.SetCookie(gc, accessToken) } return res, nil diff --git a/server/resolvers/token.go b/server/resolvers/token.go index 80679dd..ccf0e4e 100644 --- a/server/resolvers/token.go +++ b/server/resolvers/token.go @@ -13,10 +13,10 @@ import ( "github.com/authorizerdev/authorizer/server/utils" ) -func Token(ctx context.Context) (*model.LoginResponse, error) { +func Token(ctx context.Context) (*model.AuthResponse, error) { metaInfo := utils.GetMetaInfo() log.Println("=> meta", metaInfo) - var res *model.LoginResponse + var res *model.AuthResponse gc, err := utils.GinContextFromContext(ctx) if err != nil { @@ -55,7 +55,7 @@ func Token(ctx context.Context) (*model.LoginResponse, error) { }, enum.AccessToken) } utils.SetCookie(gc, token) - res = &model.LoginResponse{ + res = &model.AuthResponse{ Message: `Token verified`, AccessToken: &token, AccessTokenExpiresAt: &expiresAt, diff --git a/server/resolvers/verifyEmail.go b/server/resolvers/verifyEmail.go index 8457b97..e12d167 100644 --- a/server/resolvers/verifyEmail.go +++ b/server/resolvers/verifyEmail.go @@ -12,9 +12,9 @@ import ( "github.com/authorizerdev/authorizer/server/utils" ) -func VerifyEmail(ctx context.Context, params model.VerifyEmailInput) (*model.LoginResponse, error) { +func VerifyEmail(ctx context.Context, params model.VerifyEmailInput) (*model.AuthResponse, error) { gc, err := utils.GinContextFromContext(ctx) - var res *model.LoginResponse + var res *model.AuthResponse if err != nil { return res, err } @@ -53,7 +53,7 @@ func VerifyEmail(ctx context.Context, params model.VerifyEmailInput) (*model.Log session.SetToken(userIdStr, refreshToken) - res = &model.LoginResponse{ + res = &model.AuthResponse{ Message: `Email verified successfully.`, AccessToken: &accessToken, AccessTokenExpiresAt: &expiresAt,