firebase_auth_latest 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/firebase_auth/controller_helpers.rb +26 -0
- data/lib/firebase_auth/version.rb +1 -1
- data/lib/firebase_auth.rb +14 -3
- data/lib/generators/firebase_auth/model_generator.rb +15 -15
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14123f6452c05842304751f21130b99951ec72a3f8ec69f2ffa4922984225a49
|
4
|
+
data.tar.gz: fb812e7d17533973a639d3d8fde49cd10790df094790ae73e0baf56f4c8c2d47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b543b69f633b16bf1fff1bc8dff504ee165573b33d37b648b40506d0d3b0a7647f3b4b6a1a478d432b197aef3b02aa508e3639b4d16ecb35b87c65b18f99bd14
|
7
|
+
data.tar.gz: 3873e1fcaf2bae429191b744013afa4396d4c5b1df697a866a3e5435c1aa82af93bedf5d77997583093bb8f290924c6ec5399c14b00ec423427e0f42adc9033e
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module FirebaseAuth
|
2
|
+
module ControllerHelpers
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def authenticate_with_firebase(model_name)
|
9
|
+
define_method("authenticate_#{model_name.downcase}") do
|
10
|
+
id_token = request.headers['Authorization']&.split(' ')&.last
|
11
|
+
if id_token
|
12
|
+
project_id = Rails.application.credentials.dig(:firebase, :project_id)
|
13
|
+
decoded_token = FirebaseAuthService::FirebaseAuthService.new(project_id).verify_id_token(id_token)
|
14
|
+
if decoded_token
|
15
|
+
instance_variable_set("@current_#{model_name.downcase}", model_name.constantize.find_by(email: decoded_token['email']))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
unless instance_variable_get("@current_#{model_name.downcase}")
|
19
|
+
render json: { error: 'Unauthorized' }, status: :unauthorized
|
20
|
+
end
|
21
|
+
end
|
22
|
+
before_action "authenticate_#{model_name.downcase}".to_sym
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/firebase_auth.rb
CHANGED
@@ -1,7 +1,18 @@
|
|
1
1
|
require "firebase_auth/version"
|
2
2
|
require "firebase_auth/firebase_auth"
|
3
|
-
require "firebase_auth/
|
3
|
+
require "firebase_auth/controller_helpers"
|
4
|
+
require "generators/firebase_auth/model_generator"
|
4
5
|
|
5
|
-
module
|
6
|
+
module FirebaseAuthService
|
6
7
|
class Error < StandardError; end
|
7
|
-
|
8
|
+
|
9
|
+
module Rails
|
10
|
+
class Railtie < ::Rails::Railtie
|
11
|
+
initializer 'firebase_auth_service.action_controller' do
|
12
|
+
ActiveSupport.on_load(:action_controller) do
|
13
|
+
include FirebaseAuthService::ControllerHelpers
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'rails/generators'
|
2
|
+
require 'rails/generators/named_base'
|
2
3
|
|
3
|
-
module
|
4
|
-
module
|
4
|
+
module Generators
|
5
|
+
module FirebaseAuth
|
5
6
|
class ModelGenerator < Rails::Generators::NamedBase
|
6
7
|
source_root File.expand_path('templates', __dir__)
|
7
8
|
|
@@ -10,27 +11,26 @@ module FirebaseAuth
|
|
10
11
|
end
|
11
12
|
|
12
13
|
def create_migration_file
|
13
|
-
generate 'migration', "Create#{
|
14
|
+
generate 'migration', "Create#{table_name.capitalize} email:string user_id:string"
|
14
15
|
end
|
15
16
|
|
16
17
|
def inject_authenticate_method
|
17
|
-
inject_into_class
|
18
|
+
inject_into_class "app/controllers/application_controller.rb", ApplicationController do
|
18
19
|
<<-RUBY
|
20
|
+
before_action :authenticate_#{file_name}
|
19
21
|
|
20
|
-
|
22
|
+
private
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
@current_#{file_name} = #{class_name}.find_by(email: decoded_token['email'])
|
24
|
+
def authenticate_#{file_name}
|
25
|
+
id_token = request.headers['Authorization']&.split(' ')&.last
|
26
|
+
if id_token
|
27
|
+
decoded_token = FirebaseAuthService::FirebaseAuthService.new(Rails.application.credentials.dig(:firebase, :project_id)).verify_id_token(id_token)
|
28
|
+
if decoded_token
|
29
|
+
@current_#{file_name} = #{class_name}.find_by(email: decoded_token['email'])
|
30
|
+
end
|
30
31
|
end
|
32
|
+
render json: { error: 'Unauthorized' }, status: :unauthorized unless @current_#{file_name}
|
31
33
|
end
|
32
|
-
render json: { error: 'Unauthorized' }, status: :unauthorized unless @current_#{file_name}
|
33
|
-
end
|
34
34
|
RUBY
|
35
35
|
end
|
36
36
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: firebase_auth_latest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Taha Ali Irfan
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- LICENSE.txt
|
77
77
|
- README.md
|
78
78
|
- lib/firebase_auth.rb
|
79
|
+
- lib/firebase_auth/controller_helpers.rb
|
79
80
|
- lib/firebase_auth/firebase_auth.rb
|
80
81
|
- lib/firebase_auth/railtie.rb
|
81
82
|
- lib/firebase_auth/user.rb
|