avocado 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e32ef0766b1e70a3d4946f2d512acc59e5fe7d08e7166a751dbb3e5eacc35b41
4
- data.tar.gz: a822265796d94d9e5d4174894f6a7dea7bf7d845d0ac78eb65686a4ecab7f3cd
3
+ metadata.gz: '04228885896e7d922d727e6497a22a933f3910c96afad40b69bd21d209ab4a3c'
4
+ data.tar.gz: 8a71cb118b5d97513f506d44ee5b2e01a0af5747d9e8151c9d866a589565d660
5
5
  SHA512:
6
- metadata.gz: 4f3c42f3873e26eec8f11047b5fd5bbf885ccc4dd350aea52f872b3d869dd19917ff24c71a0989fbf1d7a547388a0ddd578ea0cb05eaacb12b8d6962cfb99af7
7
- data.tar.gz: f3b23cc9b7df23daabe61e9143aea880bd4183b47126c5bea515df271896c42498513add9776eff8c73302f95c3f6097f5acbc9ff360f9c77508817bf1cb56c9
6
+ metadata.gz: 6ff80323ec0979cbfecaa008f7cca93a73c7e2e82df4ac5590caae93de5b3b903c9fc724ab8508c50d70299d118df94718d6c22d00b0f990798a0a3544091a7f
7
+ data.tar.gz: d39393b42cfc22511acecaceacce2dc345a65a1500fe3071c7b69f11876f479eb48d9d03beee052a0518b485da2ded599b6fe919454df7bb547a4e2d3ac67664
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2023-07-17
4
+
5
+ - Add an `Avocado::Mailer` which generates each of the signed ids
6
+ - Rename `password_recovery` to `password_reset`
7
+
8
+ ## [0.2.0] - 2023-07-15
9
+
10
+ - Validate presence, uniqueness, and format on `email` attribute
11
+ - Normalize email value during save
12
+ - Validate password format and length
13
+ - Include a token generator for password recovery
14
+ - Rename `Avocado::UserConcern` to `Avocado::User`
15
+
3
16
  ## [0.1.0] - 2023-07-14
4
17
 
5
18
  - Initial release
data/README.md CHANGED
@@ -15,18 +15,31 @@ Without bundler, install the gem by executing:
15
15
  ## Usage
16
16
 
17
17
  If you have a `User` model in your application and are nervous about using Rails
18
- features directly, preferring to consume the features via a packaged gem, you
19
- can use the Rails `has_secure_password` feature in a more convoluted way, by
20
- adding the `Avocado::UserConcern` to your `User` model:
18
+ features directly, preferring to consume the features via a packaged gem, add
19
+ the `Avocado::User` to your `User` model:
21
20
 
22
21
  ```ruby
23
22
  class User < ApplicationRecord
24
- include Avocado::UserConcern
23
+ include Avocado::User
25
24
  end
26
25
  ```
27
26
 
28
- Note that behind the scenes this is truly just calling `has_secure_password` for
29
- you and doing nothing else. It's sort of funny to do this.
27
+ This will do a few things behind the scenes:
28
+
29
+ - Use the built-in `has_secure_password` to generate relevant password methods
30
+ - Add some basic validations for the `email` and `password` fields on `User`
31
+ - Normalize email values when records are saved
32
+ - Provide signed token generators for `password_reset`, `email_verification`,
33
+ and `email_affirmation`
34
+
35
+ It's sort of funny to do this because you genuinely just could have put this
36
+ stuff right in your app, and yet here we are making gems instead!
37
+
38
+ There is an `Avocado::Mailer` which can be included in a mailer class and
39
+ provides some basic mailers.
40
+
41
+ The `spec/internal` app within this repo has some example usage of both model
42
+ and mailer.
30
43
 
31
44
  ## Development
32
45
 
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Avocado
6
+ module Mailer
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ before_action :set_user
11
+ before_action :set_signed_id
12
+
13
+ default to: -> { @user.email }
14
+ end
15
+
16
+ def email_affirmation
17
+ mail
18
+ end
19
+
20
+ def email_verification
21
+ mail
22
+ end
23
+
24
+ def password_reset
25
+ mail
26
+ end
27
+
28
+ private
29
+
30
+ def set_user
31
+ @user = params[:user]
32
+ end
33
+
34
+ def set_signed_id
35
+ @signed_id = @user.generate_token_for(action_name.to_sym)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Avocado
6
+ module User
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ include UserEmail
11
+ include UserEmailAffirmation
12
+ include UserEmailVerification
13
+ include UserPassword
14
+ include UserPasswordReset
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Avocado
6
+ module UserEmail
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ validates :email, presence: true, uniqueness: true, format: {with: URI::MailTo::EMAIL_REGEXP}
11
+
12
+ normalizes :email, with: ->(email) { email.downcase.strip }
13
+ end
14
+ end
15
+ end
@@ -3,11 +3,13 @@
3
3
  require "active_support/concern"
4
4
 
5
5
  module Avocado
6
- module UserConcern
6
+ module UserEmailAffirmation
7
7
  extend ActiveSupport::Concern
8
8
 
9
+ TOKEN_EXPIRATION = 16.minutes
10
+
9
11
  included do
10
- has_secure_password
12
+ generates_token_for :email_affirmation, expires_in: TOKEN_EXPIRATION
11
13
  end
12
14
  end
13
15
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Avocado
6
+ module UserEmailVerification
7
+ extend ActiveSupport::Concern
8
+
9
+ TOKEN_EXPIRATION = 2_048.minutes
10
+
11
+ included do
12
+ generates_token_for :email_verification, expires_in: TOKEN_EXPIRATION do
13
+ email
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Avocado
6
+ module UserPassword
7
+ extend ActiveSupport::Concern
8
+
9
+ REQUIRED_FORMAT = /\A(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*\z/x
10
+ REQUIRED_LENGTH = 8
11
+
12
+ included do
13
+ has_secure_password
14
+
15
+ validates :password, format: {with: REQUIRED_FORMAT}, length: {minimum: REQUIRED_LENGTH}, allow_nil: true
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Avocado
6
+ module UserPasswordReset
7
+ extend ActiveSupport::Concern
8
+
9
+ TOKEN_EXPIRATION = 64.minutes
10
+
11
+ included do
12
+ generates_token_for :password_reset, expires_in: TOKEN_EXPIRATION do
13
+ password_digest_salt
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def password_digest_salt
20
+ password_from_digest.salt[-10..]
21
+ end
22
+
23
+ def password_from_digest
24
+ BCrypt::Password.new(password_digest)
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Avocado
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/avocado.rb CHANGED
@@ -1,8 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "avocado/version"
4
- require_relative "avocado/user_concern"
5
4
 
6
5
  module Avocado
7
6
  class Error < StandardError; end
7
+
8
+ autoload :Mailer, "avocado/mailer"
9
+ autoload :User, "avocado/user"
10
+ autoload :UserEmail, "avocado/user_email"
11
+ autoload :UserEmailAffirmation, "avocado/user_email_affirmation"
12
+ autoload :UserEmailVerification, "avocado/user_email_verification"
13
+ autoload :UserPassword, "avocado/user_password"
14
+ autoload :UserPasswordReset, "avocado/user_password_reset"
8
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avocado
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Jankowski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-14 00:00:00.000000000 Z
11
+ date: 2023-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt
@@ -82,7 +82,13 @@ files:
82
82
  - Rakefile
83
83
  - config.ru
84
84
  - lib/avocado.rb
85
- - lib/avocado/user_concern.rb
85
+ - lib/avocado/mailer.rb
86
+ - lib/avocado/user.rb
87
+ - lib/avocado/user_email.rb
88
+ - lib/avocado/user_email_affirmation.rb
89
+ - lib/avocado/user_email_verification.rb
90
+ - lib/avocado/user_password.rb
91
+ - lib/avocado/user_password_reset.rb
86
92
  - lib/avocado/version.rb
87
93
  - sig/avocado.rbs
88
94
  homepage: https://github.com/unicorngroomers/avocado