revise_auth-jets 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/app/config/routes.rb +7 -0
- data/app/controllers/api/base_controller.rb +42 -0
- data/app/controllers/api/v1/mes_controller.rb +11 -0
- data/app/controllers/revise_auth/registrations_controller.rb +1 -0
- data/app/controllers/revise_auth/sessions_controller.rb +1 -0
- data/lib/generators/revise_auth/model_generator.rb +13 -4
- data/lib/generators/revise_auth/templates/README +1 -2
- data/lib/generators/revise_auth/views_generator.rb +3 -1
- data/lib/revise_auth/api_model.rb +46 -0
- data/lib/revise_auth/version.rb +1 -1
- data/lib/revise_auth-jets.rb +1 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 825c70cb5515ef92564d9ba805da0c6629b8822c3b88b3760f4cee3b44e18c6c
|
4
|
+
data.tar.gz: f3befb88b7f16a042ed269bcc3e2bf12a50c58a512a0215f992587bbb95d010c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3edc1eff821369e7225b82e883d43e24373ae528d29682d01fdd9a14ad3c9549cbed623ec535d3448ab499986ff581a86a15811aeaf11965f9629ce7fbd55750
|
7
|
+
data.tar.gz: 70fedcacdf4b1d572810bf1120c8f915f10014ff0c89423a237e23299d542f42610a6d3db4ac20bfb1daac2d17fbd2fab37d33c3dfe4ae3ac69336811d2d17b1
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ bundle add "revise_auth-jets"
|
|
12
12
|
|
13
13
|
And then execute the following to generate a `User` model (optionally adding other fields such as `first_name` and `last_name`):
|
14
14
|
```bash
|
15
|
-
$ jets g revise_auth:model User
|
15
|
+
$ jets g revise_auth:model User
|
16
16
|
$ jets db:migrate
|
17
17
|
$ jets g revise_auth:views
|
18
18
|
```
|
data/app/config/routes.rb
CHANGED
@@ -23,6 +23,13 @@ Jets.application.routes.draw do
|
|
23
23
|
delete "logout", to: "sessions#delete"
|
24
24
|
end
|
25
25
|
|
26
|
+
# API routes
|
27
|
+
namespace :api do
|
28
|
+
namespace :v1 do
|
29
|
+
resource :me, only: :show
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
26
33
|
# The jets/public#show controller can serve static utf8 content out of the public folder.
|
27
34
|
# Note, as part of the deploy process Jets uploads files in the public folder to s3
|
28
35
|
# and serves them out of s3 directly. S3 is well suited to serve static assets.
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Api::BaseController < ApplicationController
|
2
|
+
include ReviseAuth::Authentication
|
3
|
+
skip_before_action :verify_authenticity_token
|
4
|
+
prepend_before_action :authenticate_api_token!
|
5
|
+
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
|
6
|
+
rescue_from ActionController::ParameterMissing, with: :handle_parameter_missing
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def record_not_found
|
11
|
+
render json: {error: "Record Not Found"}, status: :not_found
|
12
|
+
end
|
13
|
+
|
14
|
+
def handle_parameter_missing(exception)
|
15
|
+
render json: {error: exception.message}, status: :bad_request
|
16
|
+
end
|
17
|
+
|
18
|
+
def authenticate_api_token!
|
19
|
+
if user_from_token
|
20
|
+
login(user_from_token)
|
21
|
+
else
|
22
|
+
head :unauthorized
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def token_from_header
|
27
|
+
request.headers.fetch("authorization", "").split(" ").last
|
28
|
+
end
|
29
|
+
|
30
|
+
def api_token
|
31
|
+
@_api_token ||= ApiToken.find_by(token: token_from_header)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Only for use within authenticate_api_token! above
|
35
|
+
# Use current_user/Current.user or current_account/Current.account within app controllers
|
36
|
+
def user_from_token
|
37
|
+
if api_token.present?
|
38
|
+
api_token.touch(:last_used_at)
|
39
|
+
api_token.user
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -9,6 +9,7 @@ class ReviseAuth::RegistrationsController < ReviseAuthController
|
|
9
9
|
@user = User.new(sign_up_params)
|
10
10
|
if @user.save
|
11
11
|
login(@user)
|
12
|
+
current_user.api_tokens.first_or_create(name: ApiToken::APP_NAME)
|
12
13
|
redirect_to root_path
|
13
14
|
else
|
14
15
|
render :new, status: :unprocessable_entity
|
@@ -5,6 +5,7 @@ class ReviseAuth::SessionsController < ReviseAuthController
|
|
5
5
|
def create
|
6
6
|
if (user = User.authenticate_by(email: params[:email], password: params[:password]))
|
7
7
|
login(user)
|
8
|
+
current_user.api_tokens.first_or_create(name: ApiToken::APP_NAME)
|
8
9
|
redirect_to root_path
|
9
10
|
else
|
10
11
|
#flash[:alert] = I18n.t("revise_auth.invalid_email_or_password")
|
@@ -11,20 +11,26 @@ module ReviseAuth
|
|
11
11
|
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
12
12
|
|
13
13
|
def initialize(args, *options)
|
14
|
-
@original_attributes = args[1..] || []
|
15
14
|
super
|
16
15
|
end
|
17
16
|
|
18
17
|
def generate_model
|
19
|
-
model_attributess = model_attributes.join(', ').gsub('
|
18
|
+
model_attributess = model_attributes.join(', ').gsub(',', '')
|
19
|
+
puts "Adding #{name}"
|
20
20
|
puts "jets g model #{name} #{model_attributess}"
|
21
21
|
system "jets g model #{name} #{model_attributess}"
|
22
|
+
puts "Adding ApiToken"
|
23
|
+
system "jets g model ApiTokens #{name.downcase}:references token:string:uniq name:string metadata:jsonb transient:boolean last_used_at:datetime expires_at:datetime"
|
22
24
|
#generate :model, name, *model_attributes
|
23
25
|
end
|
24
26
|
|
25
27
|
def add_revise_auth_model
|
28
|
+
prepend_to_file "app/models/api_token.rb", "require 'revise_auth-jets'\n"
|
29
|
+
inject_into_class "app/models/api_token.rb", "ApiToken", " include ReviseAuth::ApiModel\n"
|
30
|
+
|
26
31
|
prepend_to_file model_path, "require 'revise_auth-jets'\n"
|
27
32
|
inject_into_class model_path, class_name, " include ReviseAuth::Model\n"
|
33
|
+
inject_into_class model_path, class_name, " has_many :api_tokens, dependent: :destroy\n"
|
28
34
|
end
|
29
35
|
|
30
36
|
def add_uniq_to_email_index
|
@@ -51,13 +57,16 @@ module ReviseAuth
|
|
51
57
|
|
52
58
|
def model_attributes
|
53
59
|
[
|
54
|
-
"email:string:
|
60
|
+
"email:string:uniq",
|
55
61
|
"password_digest:string",
|
62
|
+
"first_name:string",
|
63
|
+
"last_name:string",
|
64
|
+
"admin:boolean",
|
56
65
|
"confirmation_token:string",
|
57
66
|
"confirmed_at:datetime",
|
58
67
|
"confirmation_sent_at:datetime",
|
59
68
|
"unconfirmed_email:string"
|
60
|
-
]
|
69
|
+
]
|
61
70
|
end
|
62
71
|
end
|
63
72
|
end
|
@@ -1,8 +1,7 @@
|
|
1
1
|
🚚 Your Revise auth database model has been generated!
|
2
2
|
|
3
3
|
Next step:
|
4
|
-
Add
|
4
|
+
Add t.jsonb :metadata, default: {} and t.boolean :transient, default: false into your ApiToken migration
|
5
5
|
Run "jets db:migrate"
|
6
|
-
Add ActiveRecord::Base.signed_id_verifier_secret = "custom_verfifier_secret" in your initializers/ Set this as an env var
|
7
6
|
Add your stmp settings in your development.rb
|
8
7
|
Run "jets g revise_auth:views"
|
@@ -12,7 +12,7 @@ module ReviseAuth
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def copy_styles
|
15
|
-
template "app/stylesheet/theme.scss", "app/
|
15
|
+
template "app/stylesheet/theme.scss", "app/javascript/packs/theme.scss"
|
16
16
|
end
|
17
17
|
|
18
18
|
def copy_controllers
|
@@ -24,6 +24,7 @@ module ReviseAuth
|
|
24
24
|
end
|
25
25
|
else
|
26
26
|
directory "app/controllers/revise_auth"
|
27
|
+
directory "app/controllers/api"
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
@@ -35,6 +36,7 @@ module ReviseAuth
|
|
35
36
|
else
|
36
37
|
directory "app/views/revise_auth"
|
37
38
|
directory "app/views/main"
|
39
|
+
directory "app/views/shared"
|
38
40
|
end
|
39
41
|
end
|
40
42
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ReviseAuth
|
2
|
+
module ApiModel
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
DEFAULT_NAME = "api_token"
|
7
|
+
APP_NAME = "my_app"
|
8
|
+
|
9
|
+
belongs_to :user
|
10
|
+
|
11
|
+
scope :sorted, -> { order("last_used_at DESC NULLS LAST, created_at DESC") }
|
12
|
+
|
13
|
+
has_secure_token :token
|
14
|
+
|
15
|
+
validates :name, presence: true
|
16
|
+
|
17
|
+
def can?(permission)
|
18
|
+
Array.wrap(data("permissions")).include?(permission)
|
19
|
+
end
|
20
|
+
|
21
|
+
def cant?(permission)
|
22
|
+
!can?(permission)
|
23
|
+
end
|
24
|
+
|
25
|
+
def data(key, default: nil)
|
26
|
+
(metadata || {}).fetch(key, default)
|
27
|
+
end
|
28
|
+
|
29
|
+
def expired?
|
30
|
+
expires_at? && Time.current >= expires_at
|
31
|
+
end
|
32
|
+
|
33
|
+
def touch_last_used_at
|
34
|
+
return if transient?
|
35
|
+
update(last_used_at: Time.current)
|
36
|
+
end
|
37
|
+
|
38
|
+
def generate_token
|
39
|
+
loop do
|
40
|
+
self.token = SecureRandom.hex(16)
|
41
|
+
break unless ApiToken.where(token: token).exists?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/revise_auth/version.rb
CHANGED
data/lib/revise_auth-jets.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: revise_auth-jets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremiah Parrack
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-09-
|
11
|
+
date: 2023-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bcrypt
|
@@ -35,6 +35,8 @@ files:
|
|
35
35
|
- README.md
|
36
36
|
- Rakefile
|
37
37
|
- app/config/routes.rb
|
38
|
+
- app/controllers/api/base_controller.rb
|
39
|
+
- app/controllers/api/v1/mes_controller.rb
|
38
40
|
- app/controllers/main_controller.rb
|
39
41
|
- app/controllers/revise_auth/email_controller.rb
|
40
42
|
- app/controllers/revise_auth/password_controller.rb
|
@@ -62,6 +64,7 @@ files:
|
|
62
64
|
- lib/generators/revise_auth/templates/README
|
63
65
|
- lib/generators/revise_auth/views_generator.rb
|
64
66
|
- lib/revise_auth-jets.rb
|
67
|
+
- lib/revise_auth/api_model.rb
|
65
68
|
- lib/revise_auth/authentication.rb
|
66
69
|
- lib/revise_auth/backports.rb
|
67
70
|
- lib/revise_auth/current.rb
|