1+ class AuthGenerator < Rails ::Generators ::NamedBase
2+ source_root File . expand_path ( '../../../' , __dir__ )
3+
4+ def create_views
5+ if class_name == 'Install'
6+ # ---------- Gems --------------
7+
8+ gem 'faraday' , '~> 1.10'
9+ gem 'jwt' , '~> 2.5'
10+ gem 'warden' , '~> 1.2' , '>= 1.2.9'
11+
12+ rake 'bundle'
13+ # ---------- Controllers --------------
14+
15+ template 'lib/generators/auth/templates/controllers/home_controller.rb' ,
16+ File . join ( 'app/controllers' , class_path , 'home_controller.rb' )
17+
18+ template 'lib/generators/auth/templates/controllers/session_controller.rb' ,
19+ File . join ( 'app/controllers' , class_path , 'session_controller.rb' )
20+
21+ template 'lib/generators/auth/templates/controllers/unauthorized_controller.rb' ,
22+ File . join ( 'app/controllers' , class_path , 'unauthorized_controller.rb' )
23+
24+ inject_into_file 'app/controllers/application_controller.rb' , after : "class ApplicationController < ActionController::Base\n " do
25+ <<-'RUBY'
26+ helper_method :current_user, :user_signed_in?
27+
28+ def warden
29+ request.env['warden']
30+ end
31+
32+ def user_signed_in?(...)
33+ warden.authenticated?(...)
34+ end
35+
36+ def authenticate_user!(...)
37+ session[:after_sign_in_path] = request.path unless user_signed_in?(...)
38+ warden.authenticate!(...)
39+ end
40+
41+ def after_sign_in_path
42+ session.delete(:after_sign_in_path) || root_path
43+ end
44+
45+ def current_user(...)
46+ warden.user(...)
47+ end
48+ RUBY
49+ end
50+
51+ # ---------- Javascript --------------
52+
53+ template 'lib/generators/auth/templates/javascript/controllers/sign_in_controller.js' ,
54+ File . join ( 'app/javascript/controllers' , class_path , 'sign_in_controller.js' )
55+
56+ # ---------- Models --------------
57+
58+ template 'lib/generators/auth/templates/models/identity_plataform/cert_store.rb' ,
59+ File . join ( 'app/models/identity_platform' , class_path , 'cert_store.rb' )
60+
61+ template 'lib/generators/auth/templates/models/identity_plataform/error.rb' ,
62+ File . join ( 'app/models/identity_platform' , class_path , 'error.rb' )
63+
64+ template 'lib/generators/auth/templates/models/identity_plataform/token.rb' ,
65+ File . join ( 'app/models/identity_platform' , class_path , 'token.rb' )
66+
67+ template 'lib/generators/auth/templates/models/identity_plataform/warden_strategy.rb' ,
68+ File . join ( 'app/models/identity_platform' , class_path , 'warden_strategy.rb' )
69+
70+ template 'lib/generators/auth/templates/models/user_migration.rb' ,
71+ File . join ( 'db/migrate' , class_path , "#{ Time . now . strftime ( '%Y%m%d%H%M%S' ) } _create_users.rb" )
72+
73+ template 'lib/generators/auth/templates/models/user.rb' ,
74+ File . join ( 'app/models' , class_path , 'user.rb' )
75+
76+
77+ # ---------- Views --------------
78+
79+ create_file 'app/views/session/new.html.erb' ,
80+
81+ <<-FILE
82+ <section id='auth-container' data-controller='sign-in' class='w-full content-center'>
83+ <%= form_with url: session_path, scope: :session, data: { sign_in_target: 'sessionForm' } do |f| %>
84+ <%= f.hidden_field :token, data: { sign_in_target: 'token' } %>
85+ <% end %>
86+ </section>
87+ FILE
88+
89+ create_file 'app/views/home/show.html.erb' ,
90+
91+ <<-FILE
92+ <div>
93+ <h1 class="font-bold text-4xl">Hello<%= ' ' + current_user.email if user_signed_in? %>!</h1>
94+ <p class=" text-blue-600">
95+ <% unless user_signed_in? %><%= link_to 'Sign in?', new_session_path %>
96+ <% else %><%= link_to 'Sign out?', session_path, data: { 'turbo-method': :delete } %>
97+ <% end %>
98+ </p>
99+ </div>
100+ FILE
101+
102+ # ---------- Routes --------------
103+
104+ route "root 'home#show'"
105+ route "get 'sign-in', to: 'session#new', as: :new_session"
106+ route "resource :session, only: %i[create destroy], controller: :session"
107+
108+ # ---------- Importmap --------------
109+
110+ inject_into_file 'config/importmap.rb' , after : "pin_all_from \" app/javascript/controllers\" , under: \" controllers\" \n " do
111+ <<-'RUBY'
112+ pin 'firebaseui', to: 'https://ga.jspm.io/npm:[email protected] /dist/esm.js' 113+ pin '@firebase/app', to: 'https://ga.jspm.io/npm:@firebase/[email protected] /dist/esm/index.esm2017.js' 114+ pin '@firebase/app-compat', to: 'https://ga.jspm.io/npm:@firebase/[email protected] /dist/esm/index.esm2017.js' 115+ pin '@firebase/auth-compat', to: 'https://ga.jspm.io/npm:@firebase/[email protected] /dist/index.esm2017.js' 116+ pin '@firebase/auth/internal', to: 'https://ga.jspm.io/npm:@firebase/[email protected] /dist/esm2017/internal.js' 117+ pin '@firebase/component', to: 'https://ga.jspm.io/npm:@firebase/[email protected] /dist/esm/index.esm2017.js' 118+ pin '@firebase/logger', to: 'https://ga.jspm.io/npm:@firebase/[email protected] /dist/esm/index.esm2017.js' 119+ pin '@firebase/util', to: 'https://ga.jspm.io/npm:@firebase/[email protected] /dist/index.esm2017.js' 120+ pin 'dialog-polyfill', to: 'https://ga.jspm.io/npm:[email protected] /dialog-polyfill.js' 121+ pin 'firebase/compat/app', to: 'https://ga.jspm.io/npm:[email protected] /compat/app/dist/index.esm.js' 122+ pin 'firebase/compat/auth', to: 'https://ga.jspm.io/npm:[email protected] /compat/auth/dist/index.esm.js' 123+ pin 'idb', to: 'https://ga.jspm.io/npm:[email protected] /build/index.js' 124+ pin 'material-design-lite/src/button/button', to: 'https://ga.jspm.io/npm:[email protected] /src/button/button.js' 125+ pin 'material-design-lite/src/mdlComponentHandler', to: 'https://ga.jspm.io/npm:[email protected] /src/mdlComponentHandler.js' 126+ pin 'material-design-lite/src/progress/progress', to: 'https://ga.jspm.io/npm:[email protected] /src/progress/progress.js' 127+ pin 'material-design-lite/src/spinner/spinner', to: 'https://ga.jspm.io/npm:[email protected] /src/spinner/spinner.js' 128+ pin 'material-design-lite/src/textfield/textfield', to: 'https://ga.jspm.io/npm:[email protected] /src/textfield/textfield.js' 129+ pin 'tslib', to: 'https://ga.jspm.io/npm:[email protected] /tslib.es6.js' 130+ RUBY
131+ end
132+
133+ # ---------- Rake --------------
134+
135+ rake 'db:migrate'
136+
137+ # ---------- Initializers --------------
138+ initializer "warden.rb" do
139+ %{
140+ # frozen_string_literal: true
141+
142+ Rails.application.reloader.to_prepare do
143+ Warden::Strategies.add :identity_token, IdentityPlatform::WardenStrategy
144+ end
145+
146+ Rails.application.config.middleware.use Warden::Manager do |manager|
147+ manager.default_strategies :identity_token
148+ manager.failure_app = UnauthorizedController
149+
150+ manager.serialize_into_session(&:id)
151+ manager.serialize_from_session { |id| User.find_by id: }
152+ end
153+ }
154+ end
155+ # ---------- END --------------
156+ end
157+ end
158+ end
0 commit comments