morpho 0.3.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/app/api/morpho/entities/authentication_token.rb +9 -0
  3. data/app/api/morpho/entities/credentials.rb +8 -0
  4. data/app/api/morpho/entities/external.rb +10 -0
  5. data/app/api/morpho/entities/message.rb +7 -0
  6. data/app/api/morpho/entities/refresh_token.rb +7 -0
  7. data/app/api/morpho/entities/user_email.rb +7 -0
  8. data/app/api/morpho/entities/user_sign_up.rb +3 -3
  9. data/app/api/morpho/helpers/http_responses.rb +54 -18
  10. data/app/api/morpho/helpers/jwt_utils.rb +16 -25
  11. data/app/api/morpho/resources/activations.rb +22 -11
  12. data/app/api/morpho/resources/externals.rb +15 -6
  13. data/app/api/morpho/resources/hello.rb +36 -0
  14. data/app/api/morpho/resources/passwords.rb +23 -8
  15. data/app/api/morpho/resources/tokens.rb +45 -10
  16. data/app/api/morpho/resources/unlocks.rb +22 -11
  17. data/app/api/morpho/resources/users.rb +19 -4
  18. data/app/concepts/morpho/user/contract/activate.rb +8 -0
  19. data/app/concepts/morpho/user/contract/external_sign_in.rb +14 -0
  20. data/app/concepts/morpho/user/contract/refresh_token.rb +8 -0
  21. data/app/concepts/morpho/user/contract/reset_password.rb +8 -0
  22. data/app/concepts/morpho/user/contract/sign_in.rb +10 -0
  23. data/app/concepts/morpho/user/contract/sign_up.rb +14 -0
  24. data/app/concepts/morpho/user/contract/unlock.rb +8 -0
  25. data/app/concepts/morpho/user/operation/activate.rb +45 -0
  26. data/app/concepts/morpho/user/operation/external_sign_in.rb +82 -0
  27. data/app/concepts/morpho/user/operation/refresh_token.rb +35 -0
  28. data/app/concepts/morpho/user/operation/reset_password.rb +49 -0
  29. data/app/concepts/morpho/user/operation/sign_in.rb +71 -0
  30. data/app/concepts/morpho/user/operation/sign_up.rb +36 -0
  31. data/app/concepts/morpho/user/operation/unlock.rb +45 -0
  32. data/app/models/morpho/user.rb +5 -3
  33. data/app/services/morpho/jwt/authentication_token.rb +11 -0
  34. data/app/services/morpho/jwt/decode.rb +11 -0
  35. data/app/services/morpho/jwt/encode.rb +11 -0
  36. data/app/services/morpho/jwt/payload.rb +11 -0
  37. data/app/services/morpho/jwt/token.rb +13 -0
  38. data/app/views/layouts/morpho/application.html.erb +1 -1
  39. data/config/initializers/grape_swagger.rb +1 -0
  40. data/config/locales/morpho.en.yml +2 -1
  41. data/config/locales/morpho.es.yml +2 -1
  42. data/lib/generators/morpho/install/install_generator.rb +1 -0
  43. data/lib/generators/morpho/install/templates/app/api/morpho/api.rb +1 -0
  44. data/lib/generators/morpho/install/templates/config/initializers/morpho.rb +4 -2
  45. data/lib/morpho/cipher.rb +2 -2
  46. data/lib/morpho/configurations/mailer.rb +2 -2
  47. data/lib/morpho/grape/data_parser.rb +35 -0
  48. data/lib/morpho/grape/data_wrapper.rb +25 -0
  49. data/lib/morpho/loader.rb +4 -1
  50. data/lib/morpho/version.rb +1 -1
  51. data/lib/morpho.rb +2 -0
  52. metadata +97 -38
  53. data/app/api/morpho/entities/sign_in/authentication_token.rb +0 -11
  54. data/app/api/morpho/entities/sign_in/credentials.rb +0 -10
  55. data/app/api/morpho/entities/sign_in/external.rb +0 -12
  56. data/app/api/morpho/entities/sign_in/refresh_token.rb +0 -9
  57. data/app/api/morpho/entities/sign_in/success.rb +0 -9
  58. data/app/api/morpho/helpers/user_activation.rb +0 -11
  59. data/app/api/morpho/helpers/user_external_login.rb +0 -53
  60. data/app/api/morpho/helpers/user_login.rb +0 -42
  61. data/app/api/morpho/helpers/user_password_reset.rb +0 -11
  62. data/app/api/morpho/helpers/user_refresh_authentication_token.rb +0 -24
  63. data/app/api/morpho/helpers/user_registration.rb +0 -17
  64. data/app/api/morpho/helpers/user_unlock.rb +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 897ee2fc194496e055864aec5475926004ab7a5665b00c0a4979f4fb686b4c48
4
- data.tar.gz: 0e5a7348610783970b1243db39ed40405a47eab4a339f4d88aba4fdaa70e1a0d
3
+ metadata.gz: de2ce5df93635871bce8162346a183b6eb8b695e9c86dd8c1979ec50cb3f66c2
4
+ data.tar.gz: 6f18be1aa24bbdc3b9ca630592b4d8b38fde03b69691b5aeb762758b8f5fa267
5
5
  SHA512:
6
- metadata.gz: c4debc02baf4abf21d34c2ed9c83f9e8ce12e364e46dccf6a8529a74963ec7765af47e49265585af2cbdd7d7b47dd65162ffe06ec0acfa9ecb918f6da2c82fe8
7
- data.tar.gz: 571c6a523befd73e153e09ec8d91addb35f8d95654c64e797744702c0066b95bb71a152994c792f9ce60a0adfc1ee5d6bffcca8dcc69af1afb3b8e5fbffa85bb
6
+ metadata.gz: 2160dffd093818bd31c309acb7aaf0a360567d58a120e76bd2f134099a3d5997ba422c2d36984c62b353c9975d5c47a03acad9e93dfea0466b8b089397ff34dd
7
+ data.tar.gz: 02e0980867d32dd46af25e967d4971f0f7d58eaeaf569baca44c6affa557eac0567d0cc3fb2414098334298f12646ba52cdc95d0e807338bea64c2492b001b48
@@ -0,0 +1,9 @@
1
+ module Morpho
2
+ module Entities
3
+ class AuthenticationToken < ::Morpho::Entities::Base
4
+ expose :authentication_token, documentation: { type: 'string', desc: 'User authentication token', required: true }
5
+ expose :expires_at, documentation: { type: 'string', desc: 'Authentication token expiration date in millis', required: true }
6
+ expose :refresh_token, documentation: { type: 'string', desc: 'User refresh token', required: true }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Morpho
2
+ module Entities
3
+ class Credentials < ::Morpho::Entities::Base
4
+ expose :email, documentation: { type: 'string', desc: 'User email address' }
5
+ expose :password, documentation: { type: 'string', desc: 'User password' }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ module Morpho
2
+ module Entities
3
+ class External < ::Morpho::Entities::Base
4
+ expose :email, documentation: { type: 'string', desc: 'User email address' }
5
+ expose :name, documentation: { type: 'string', desc: 'User name' }
6
+ expose :provider, documentation: { type: 'string', desc: 'User authentication provider' }
7
+ expose :uid, documentation: { type: 'string', desc: 'User provider uid' }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Morpho
2
+ module Entities
3
+ class Message < ::Morpho::Entities::Base
4
+ expose :content, documentation: { type: 'string', desc: 'Message content' }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Morpho
2
+ module Entities
3
+ class RefreshToken < ::Morpho::Entities::Base
4
+ expose :refresh_token, documentation: { type: 'string', desc: 'User refresh token', required: true }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Morpho
2
+ module Entities
3
+ class UserEmail < ::Morpho::Entities::Base
4
+ expose :email, documentation: { type: 'string', desc: 'User email address', required: true }
5
+ end
6
+ end
7
+ end
@@ -1,9 +1,9 @@
1
1
  module Morpho
2
2
  module Entities
3
3
  class UserSignUp < ::Morpho::Entities::Base
4
- expose :email, documentation: { type: 'string', desc: 'User email address' }
5
- expose :password, documentation: { type: 'string', desc: 'User password' }
6
- expose :password_confirmation, documentation: { type: 'string', desc: 'User password confirmation' }
4
+ expose :email, documentation: { type: 'string', desc: 'User email address', required: true }
5
+ expose :password, documentation: { type: 'string', desc: 'User password', required: true }
6
+ expose :password_confirmation, documentation: { type: 'string', desc: 'User password confirmation', required: true }
7
7
  end
8
8
  end
9
9
  end
@@ -3,40 +3,76 @@ module Morpho
3
3
  module HTTPResponses
4
4
  protected
5
5
 
6
- def render_bad_request
7
- error!({ message: I18n.t('morpho.api.messages.bad_request'), with: Morpho::Entities::Error }, 400)
6
+ def render_bad_request(errors = [])
7
+ error!({
8
+ message: I18n.t('morpho.api.messages.bad_request'),
9
+ errors: errors,
10
+ with: Morpho::Entities::Error
11
+ }, 400)
8
12
  end
9
13
 
10
- def render_unauthorized
11
- error!({ message: I18n.t('morpho.api.messages.unauthorized'), with: Morpho::Entities::Error }, 401)
14
+ def render_unauthorized(errors = [])
15
+ error!({
16
+ message: I18n.t('morpho.api.messages.unauthorized'),
17
+ errors: errors,
18
+ with: Morpho::Entities::Error
19
+ }, 401)
12
20
  end
13
21
 
14
- def render_unauthorized_detailed(errors)
15
- error!({ message: I18n.t('morpho.api.messages.unauthorized'), errors: errors, with: Morpho::Entities::Error }, 401)
22
+ def render_unauthorized_detailed(errors = [])
23
+ render_unauthorized(errors)
16
24
  end
17
25
 
18
- def render_payment_required
19
- error!({ message: I18n.t('morpho.api.messages.payment_required'), with: Morpho::Entities::Error }, 402)
26
+ def render_payment_required(errors = [])
27
+ error!({
28
+ message: I18n.t('morpho.api.messages.payment_required'),
29
+ errors: errors,
30
+ with: Morpho::Entities::Error
31
+ }, 402)
20
32
  end
21
33
 
22
- def render_forbidden
23
- error!({ message: I18n.t('morpho.api.messages.forbidden'), with: Morpho::Entities::Error }, 403)
34
+ def render_forbidden(errors = [])
35
+ error!({
36
+ message: I18n.t('morpho.api.messages.forbidden'),
37
+ errors: errors,
38
+ with: Morpho::Entities::Error
39
+ }, 403)
24
40
  end
25
41
 
26
- def render_not_found
27
- error!({ message: I18n.t('morpho.api.messages.not_found'), with: Morpho::Entities::Error }, 404)
42
+ def render_not_found(errors = [])
43
+ error!({
44
+ message: I18n.t('morpho.api.messages.not_found'),
45
+ errors: errors,
46
+ with: Morpho::Entities::Error
47
+ }, 404)
28
48
  end
29
49
 
30
- def render_method_not_allowed
31
- error!({ message: I18n.t('morpho.api.messages.method_not_allowed'), with: Morpho::Entities::Error }, 405)
50
+ def render_method_not_allowed(errors = [])
51
+ error!({
52
+ message: I18n.t('morpho.api.messages.method_not_allowed'),
53
+ errors: errors,
54
+ with: Morpho::Entities::Error
55
+ }, 405)
32
56
  end
33
57
 
34
- def render_unprocessable_entity
35
- error!({ message: I18n.t('morpho.api.messages.unprocessable_entity'), with: Morpho::Entities::Error }, 422)
58
+ def render_unprocessable_entity(errors = [])
59
+ error!({
60
+ message: I18n.t('morpho.api.messages.unprocessable_entity'),
61
+ errors: errors,
62
+ with: Morpho::Entities::Error
63
+ }, 422)
36
64
  end
37
65
 
38
- def render_unprocessable_entity_detailed(errors)
39
- error!({ message: I18n.t('morpho.api.messages.unprocessable_entity'), errors: errors, with: Morpho::Entities::Error }, 422)
66
+ def render_locked(errors = [])
67
+ error!({
68
+ message: I18n.t('morpho.api.messages.locked'),
69
+ errors: errors,
70
+ with: Morpho::Entities::Error
71
+ }, 423)
72
+ end
73
+
74
+ def render_unprocessable_entity_detailed(errors = [])
75
+ render_unprocessable_entity(errors)
40
76
  end
41
77
 
42
78
  def render_no_content
@@ -3,39 +3,30 @@ module Morpho
3
3
  module JWTUtils
4
4
  protected
5
5
 
6
- def current_user
7
- @current_user ||= Morpho::User.find_by(email: jwt_token[:email])
8
- rescue
9
- nil
6
+ def logged_in?
7
+ !!current_user
10
8
  end
11
9
 
12
- def http_token
13
- @http_token ||= if request.headers[Morpho.config.jwt.header].present?
14
- request.headers[Morpho.config.jwt.header].split(' ').last
15
- end
10
+ def require_login
11
+ render_unauthorized unless logged_in?
16
12
  end
17
13
 
18
- def jwt_token
19
- @jwt_token ||= HashWithIndifferentAccess.new(jwt_decode(http_token).first)
20
- end
21
-
22
- def jwt_encode(payload)
23
- Morpho::Cipher.jwt_encode(payload)
14
+ def current_user
15
+ @current_user ||= Morpho::User.find_by(email: token[:email])
16
+ rescue
17
+ nil
24
18
  end
25
19
 
26
- def jwt_decode(token)
27
- begin
28
- return Morpho::Cipher.jwt_decode(token)
29
- rescue
30
- nil
31
- end
20
+ def authentication_token
21
+ @authentication_token ||= Morpho::JWT::AuthenticationToken.new(request)
22
+ rescue
23
+ nil
32
24
  end
33
25
 
34
- def user_payload(user)
35
- expires_at = Time.now.to_i + Morpho.config.jwt.expiration_time
36
- issued_at = Time.now.to_i
37
-
38
- { authentication_token: jwt_encode({ exp: expires_at, iat: issued_at, email: user.email }), expires_at: expires_at, refresh_token: user.refresh_token }
26
+ def token
27
+ @token ||= Morpho::JWT::Token.new(authentication_token)
28
+ rescue
29
+ nil
39
30
  end
40
31
  end
41
32
  end
@@ -1,27 +1,38 @@
1
1
  module Morpho
2
2
  module Resources
3
3
  class Activations < ::Grape::API
4
- helpers Morpho::Helpers::HTTPResponses,
5
- Morpho::Helpers::UserActivation
4
+ helpers Morpho::Helpers::HTTPResponses
6
5
 
7
6
  namespace :activations do
8
7
  desc 'Request user activation token' do
9
- success Morpho::Entities::User
8
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::User)
9
+ failure [
10
+ [ 404, I18n.t('morpho.api.messages.not_found'), Morpho::Entities::Error ],
11
+ [ 405, I18n.t('morpho.api.messages.method_not_allowed'), Morpho::Entities::Error ],
12
+ [ 422, I18n.t('morpho.api.messages.unprocessable_entity'), Morpho::Entities::Error ]
13
+ ]
10
14
  end
11
15
  params do
12
- requires :user, type: Morpho::Entities::User
16
+ requires :data, type: Morpho::Entities::UserEmail
13
17
  end
14
18
  post do
15
- if current_user
16
- if !current_user.active?
17
- current_user.resend_activation_needed_email!
19
+ result = Morpho::User::Operation::Activate.call(params)
18
20
 
19
- present current_user, with: Morpho::Entities::User
20
- else
21
+ if result.success?
22
+ present result['model'], with: Morpho::Entities::User
23
+ else
24
+ case result['error']
25
+ when :not_valid
26
+ render_unprocessable_entity(result['contract'].errors)
27
+ when :not_found
28
+ render_not_found
29
+ when :not_allowed
21
30
  render_method_not_allowed
31
+ when :not_delivered
32
+ render_unprocessable_entity
33
+ else
34
+ render_unprocessable_entity
22
35
  end
23
- else
24
- render_not_found
25
36
  end
26
37
  end
27
38
  end
@@ -1,19 +1,28 @@
1
1
  module Morpho
2
2
  module Resources
3
3
  class Externals < ::Grape::API
4
- helpers Morpho::Helpers::HTTPResponses,
5
- Morpho::Helpers::JWTUtils,
6
- Morpho::Helpers::UserExternalLogin
4
+ helpers Morpho::Helpers::HTTPResponses
7
5
 
8
6
  namespace :externals do
9
7
  desc 'Request user authentication from external provider' do
10
- success Morpho::Entities::User
8
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::AuthenticationToken)
11
9
  end
12
10
  params do
13
- requires :data, type: Morpho::Entities::SignIn::External
11
+ requires :data, type: Morpho::Entities::External
14
12
  end
15
13
  post do
16
- login(params[:data])
14
+ result = Morpho::User::Operation::ExternalSignIn.call(params, ip: request.ip)
15
+
16
+ if result.success?
17
+ present result['token'], with: Morpho::Entities::AuthenticationToken
18
+ else
19
+ case result['error']
20
+ when :not_valid
21
+ render_unprocessable_entity(result['contract'].errors)
22
+ else
23
+ render_unprocessable_entity
24
+ end
25
+ end
17
26
  end
18
27
  end
19
28
  end
@@ -0,0 +1,36 @@
1
+ module Morpho
2
+ module Resources
3
+ class Hello < ::Grape::API
4
+ helpers Morpho::Helpers::HTTPResponses, Morpho::Helpers::JWTUtils
5
+
6
+ namespace :hello do
7
+ desc 'Request hello message' do
8
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::Message)
9
+ end
10
+ get :public do
11
+ message = { content: 'Hello world!' }
12
+
13
+ present message, with: Morpho::Entities::Message
14
+ end
15
+
16
+ namespace do
17
+ before do
18
+ require_login
19
+ end
20
+
21
+ desc 'Request hello message' do
22
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::Message)
23
+ failure [
24
+ [ 401, I18n.t('morpho.api.messages.unauthorized'), Morpho::Entities::Error ]
25
+ ]
26
+ end
27
+ get :private do
28
+ message = { content: 'Hello world!' }
29
+
30
+ present message, with: Morpho::Entities::Message
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,23 +1,38 @@
1
1
  module Morpho
2
2
  module Resources
3
3
  class Passwords < ::Grape::API
4
- helpers Morpho::Helpers::HTTPResponses,
5
- Morpho::Helpers::UserPasswordReset
4
+ helpers Morpho::Helpers::HTTPResponses
6
5
 
7
6
  namespace :passwords do
8
7
  desc 'Request user reset password token' do
9
- success Morpho::Entities::User
8
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::User)
9
+ failure [
10
+ [ 404, I18n.t('morpho.api.messages.not_found'), Morpho::Entities::Error ],
11
+ [ 405, I18n.t('morpho.api.messages.method_not_allowed'), Morpho::Entities::Error ],
12
+ [ 422, I18n.t('morpho.api.messages.unprocessable_entity'), Morpho::Entities::Error ]
13
+ ]
10
14
  end
11
15
  params do
12
- requires :user, type: Morpho::Entities::User
16
+ requires :data, type: Morpho::Entities::UserEmail
13
17
  end
14
18
  post do
15
- if current_user
16
- current_user.deliver_reset_password_instructions!
19
+ result = Morpho::User::Operation::ResetPassword.call(params)
17
20
 
18
- present current_user, with: Morpho::Entities::User
21
+ if result.success?
22
+ present result['model'], with: Morpho::Entities::User
19
23
  else
20
- render_not_found
24
+ case result['error']
25
+ when :not_valid
26
+ render_unprocessable_entity(result['contract'].errors)
27
+ when :not_found
28
+ render_not_found
29
+ when :not_allowed
30
+ render_method_not_allowed
31
+ when :not_delivered
32
+ render_unprocessable_entity
33
+ else
34
+ render_unprocessable_entity
35
+ end
21
36
  end
22
37
  end
23
38
  end
@@ -2,35 +2,70 @@ module Morpho
2
2
  module Resources
3
3
  class Tokens < ::Grape::API
4
4
  helpers Morpho::Helpers::HTTPResponses,
5
- Morpho::Helpers::JWTUtils,
6
- Morpho::Helpers::UserLogin,
7
- Morpho::Helpers::UserRefreshAuthenticationToken
5
+ Morpho::Helpers::JWTUtils
8
6
 
9
7
  namespace :tokens do
10
8
  desc 'Request user authentication token' do
11
- success Morpho::Entities::SignIn::Success
9
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::AuthenticationToken)
12
10
  failure [
13
- [ 401, I18n.t('morpho.api.messages.unauthorized'), Morpho::Entities::Error ]
11
+ [ 401, I18n.t('morpho.api.messages.unauthorized'), Morpho::Entities::Error ],
12
+ [ 403, I18n.t('morpho.api.messages.forbidden'), Morpho::Entities::Error ],
13
+ [ 404, I18n.t('morpho.api.messages.not_found'), Morpho::Entities::Error ],
14
+ [ 422, I18n.t('morpho.api.messages.unprocessable_entity'), Morpho::Entities::Error ],
15
+ [ 423, I18n.t('morpho.api.messages.locked'), Morpho::Entities::Error ]
14
16
  ]
15
17
  end
16
18
  params do
17
- requires :data, type: Morpho::Entities::SignIn::Credentials
19
+ requires :data, type: Morpho::Entities::Credentials
18
20
  end
19
21
  post do
20
- login(params[:data])
22
+ result = Morpho::User::Operation::SignIn.call(params, ip: request.ip)
23
+
24
+ if result.success?
25
+ present result['token'], with: Morpho::Entities::AuthenticationToken
26
+ else
27
+ case result['error']
28
+ when :not_valid
29
+ render_unprocessable_entity(result['contract'].errors)
30
+ when :not_found
31
+ render_not_found([I18n.t('morpho.api.messages.sign_in.unexistent')])
32
+ when :not_active
33
+ render_forbidden([I18n.t('morpho.api.messages.sign_in.unconfirmed')])
34
+ when :locked
35
+ render_locked([I18n.t('morpho.api.messages.sign_in.locked')])
36
+ when :wrong_password
37
+ render_unauthorized([I18n.t('morpho.api.messages.sign_in.bad_credentials')])
38
+ else
39
+ render_unprocessable_entity
40
+ end
41
+ end
21
42
  end
22
43
 
23
44
  desc 'Refresh user authentication token' do
24
- success Morpho::Entities::SignIn::Success
45
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::AuthenticationToken)
25
46
  failure [
47
+ [ 404, I18n.t('morpho.api.messages.not_found'), Morpho::Entities::Error ],
26
48
  [ 422, I18n.t('morpho.api.messages.unprocessable_entity'), Morpho::Entities::Error ]
27
49
  ]
28
50
  end
29
51
  params do
30
- requires :data, type: Morpho::Entities::SignIn::RefreshToken
52
+ requires :data, type: Morpho::Entities::RefreshToken
31
53
  end
32
54
  post :refresh do
33
- refresh_token(params[:data])
55
+ result = Morpho::User::Operation::RefreshToken.call(params)
56
+
57
+ if result.success?
58
+ present result['token'], with: Morpho::Entities::AuthenticationToken
59
+ else
60
+ case result['error']
61
+ when :not_valid
62
+ render_unprocessable_entity(result['contract'].errors)
63
+ when :not_found
64
+ render_not_found([I18n.t('morpho.api.messages.refresh_token.invalid')])
65
+ else
66
+ render_unprocessable_entity
67
+ end
68
+ end
34
69
  end
35
70
  end
36
71
  end
@@ -1,27 +1,38 @@
1
1
  module Morpho
2
2
  module Resources
3
3
  class Unlocks < ::Grape::API
4
- helpers Morpho::Helpers::HTTPResponses,
5
- Morpho::Helpers::UserUnlock
4
+ helpers Morpho::Helpers::HTTPResponses
6
5
 
7
6
  namespace :unlocks do
8
7
  desc 'Request user unlock token' do
9
- success Morpho::Entities::User
8
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::UserEmail)
9
+ failure [
10
+ [ 404, I18n.t('morpho.api.messages.not_found'), Morpho::Entities::Error ],
11
+ [ 405, I18n.t('morpho.api.messages.method_not_allowed'), Morpho::Entities::Error ],
12
+ [ 422, I18n.t('morpho.api.messages.unprocessable_entity'), Morpho::Entities::Error ]
13
+ ]
10
14
  end
11
15
  params do
12
- requires :user, type: Morpho::Entities::User
16
+ requires :data, type: Morpho::Entities::UserEmail
13
17
  end
14
18
  post do
15
- if current_user
16
- if current_user.login_locked?
17
- current_user.resend_unlock_token_email!
19
+ result = Morpho::User::Operation::Unlock.call(params)
18
20
 
19
- present current_user, with: Morpho::Entities::User
20
- else
21
+ if result.success?
22
+ present result['model'], with: Morpho::Entities::User
23
+ else
24
+ case result['error']
25
+ when :not_valid
26
+ render_unprocessable_entity(result['contract'].errors)
27
+ when :not_found
28
+ render_not_found
29
+ when :not_allowed
21
30
  render_method_not_allowed
31
+ when :not_delivered
32
+ render_unprocessable_entity
33
+ else
34
+ render_unprocessable_entity
22
35
  end
23
- else
24
- render_not_found
25
36
  end
26
37
  end
27
38
  end
@@ -1,18 +1,33 @@
1
1
  module Morpho
2
2
  module Resources
3
3
  class Users < ::Grape::API
4
- helpers Morpho::Helpers::HTTPResponses,
5
- Morpho::Helpers::UserRegistration
4
+ helpers Morpho::Helpers::HTTPResponses
6
5
 
7
6
  namespace :users do
8
7
  desc 'User registration' do
9
- success Morpho::Entities::User
8
+ success Morpho::Grape::DataWrapper.new(Morpho::Entities::User)
9
+ failure [
10
+ [ 422, I18n.t('morpho.api.messages.unprocessable_entity'), Morpho::Entities::Error ]
11
+ ]
10
12
  end
11
13
  params do
12
14
  requires :data, type: Morpho::Entities::UserSignUp
13
15
  end
14
16
  post do
15
- register(params[:data])
17
+ result = Morpho::User::Operation::SignUp.call(params)
18
+
19
+ if result.success?
20
+ present result['model'], with: Morpho::Entities::User
21
+ else
22
+ case result['error']
23
+ when :not_valid
24
+ render_unprocessable_entity(result['contract'].errors)
25
+ when :not_saved
26
+ render_unprocessable_entity(result['model'].errors)
27
+ else
28
+ render_unprocessable_entity
29
+ end
30
+ end
16
31
  end
17
32
  end
18
33
  end
@@ -0,0 +1,8 @@
1
+ module Morpho
2
+ class User::Contract::Activate < Reform::Form
3
+ include Reform::Form::ActiveRecord
4
+
5
+ property :email
6
+ validates :email, presence: true, email_format: true
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ require 'reform/form/validation/unique_validator'
2
+
3
+ module Morpho
4
+ class User::Contract::ExternalSignIn < Reform::Form
5
+ include Reform::Form::ActiveRecord
6
+
7
+ property :email
8
+ validates :email, presence: true, email_format: true
9
+ property :uid
10
+ validates :uid, presence: true
11
+ property :provider
12
+ validates :provider, presence: true
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module Morpho
2
+ class User::Contract::RefreshToken < Reform::Form
3
+ include Reform::Form::ActiveRecord
4
+
5
+ property :refresh_token
6
+ validates :refresh_token, presence: true
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Morpho
2
+ class User::Contract::ResetPassword < Reform::Form
3
+ include Reform::Form::ActiveRecord
4
+
5
+ property :email
6
+ validates :email, presence: true, email_format: true
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ module Morpho
2
+ class User::Contract::SignIn < Reform::Form
3
+ include Reform::Form::ActiveRecord
4
+
5
+ property :email
6
+ validates :email, presence: true, email_format: true
7
+ property :password
8
+ validates :password, presence: true
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ require 'reform/form/validation/unique_validator'
2
+
3
+ module Morpho
4
+ class User::Contract::SignUp < Reform::Form
5
+ include Reform::Form::ActiveRecord
6
+
7
+ property :email
8
+ validates :email, presence: true, unique: true, email_format: true
9
+ property :password
10
+ validates :password, presence: true, confirmation: true
11
+ property :password_confirmation, virtual: true
12
+ validates :password_confirmation, presence: true
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module Morpho
2
+ class User::Contract::Unlock < Reform::Form
3
+ include Reform::Form::ActiveRecord
4
+
5
+ property :email
6
+ validates :email, presence: true, email_format: true
7
+ end
8
+ end