From 9ded479384ae5d7c64d3a047c2619f4a48d642ec Mon Sep 17 00:00:00 2001 From: jiangplus Date: Sat, 7 Sep 2024 14:37:12 +0800 Subject: [PATCH] add session controller --- app/assets/config/manifest.js | 1 + app/assets/javascript/application.js | 3 +++ .../javascript/controllers/application.js | 9 ++++++++ .../controllers/hello_controller.js | 7 +++++++ app/assets/javascript/controllers/index.js | 8 +++++++ app/controllers/sessions_controller.rb | 21 ++++++++++++------- app/views/home/index.html.erb | 4 ++++ app/views/sessions/new.html.erb | 14 +++++++++++++ app/views/sessions/verify.html.erb | 18 ++++++++++++++++ config/initializers/assets.rb | 2 ++ .../initializers/filter_parameter_logging.rb | 2 +- config/routes.rb | 5 +++++ 12 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 app/assets/javascript/application.js create mode 100644 app/assets/javascript/controllers/application.js create mode 100644 app/assets/javascript/controllers/hello_controller.js create mode 100644 app/assets/javascript/controllers/index.js create mode 100644 app/views/sessions/new.html.erb create mode 100644 app/views/sessions/verify.html.erb diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index 9a99757..6a14cb6 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -1,2 +1,3 @@ //= link_tree ../images //= link_tree ../builds +//= link_directory ../../javascript .js diff --git a/app/assets/javascript/application.js b/app/assets/javascript/application.js new file mode 100644 index 0000000..d933293 --- /dev/null +++ b/app/assets/javascript/application.js @@ -0,0 +1,3 @@ +// Entry point for the build script in your package.json +import "@hotwired/turbo-rails" +import "./controllers" diff --git a/app/assets/javascript/controllers/application.js b/app/assets/javascript/controllers/application.js new file mode 100644 index 0000000..1213e85 --- /dev/null +++ b/app/assets/javascript/controllers/application.js @@ -0,0 +1,9 @@ +import { Application } from "@hotwired/stimulus" + +const application = Application.start() + +// Configure Stimulus development experience +application.debug = false +window.Stimulus = application + +export { application } diff --git a/app/assets/javascript/controllers/hello_controller.js b/app/assets/javascript/controllers/hello_controller.js new file mode 100644 index 0000000..5975c07 --- /dev/null +++ b/app/assets/javascript/controllers/hello_controller.js @@ -0,0 +1,7 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + connect() { + this.element.textContent = "Hello World!" + } +} diff --git a/app/assets/javascript/controllers/index.js b/app/assets/javascript/controllers/index.js new file mode 100644 index 0000000..d0685d3 --- /dev/null +++ b/app/assets/javascript/controllers/index.js @@ -0,0 +1,8 @@ +// This file is auto-generated by ./bin/rails stimulus:manifest:update +// Run that command whenever you add a new controller or create them with +// ./bin/rails generate stimulus controllerName + +import { application } from "./application" + +import HelloController from "./hello_controller" +application.register("hello", HelloController) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index aa9beeb..ba14404 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -10,11 +10,20 @@ def index def new end + def verify + code = rand(10_000..100_000) + token = ProfileToken.create(context: params[:context], sent_to: params[:email], code: code) + + p token + mailer = SigninMailer.with(code: code, recipient: params[:email]).signin_email + mailer.deliver_now! + @email = params[:email] + end + def show @profile = Profile.find(params[:id]) end - def create token = ProfileToken.find_by(context: "email-verify", sent_to: params[:email], code: params[:code]) return render json: { result: "error", message: "EMailSignIn::InvalidEmailOrCode" } unless token @@ -24,18 +33,16 @@ def create # token.update(verified: true) profile = Profile.find_or_create_by(email: params[:email]) - - p 'profile' - p profile + cookies.signed[:profile_id] = profile.id SigninActivity.create( app: "web", address: params[:email], address_type: "email", - # address_source: params[:address_source], + address_source: "email-verifier", profile_id: profile.id, - # locale: params[:locale], - # lang: params[:lang], + locale: params[:locale], + lang: params[:lang], remote_ip: request.remote_ip, ) # render json: { result: "ok", auth_token: profile.gen_auth_token, email: params[:email], id: profile.id, address_type: "email" } diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 04756a8..a0c624a 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -7,6 +7,10 @@ +
+current profile : <%= Current.session.try(:id) %> +
+
Sign in + +<%= form_with(url: verify_path) do |form| %> +
+ <%= form.label :email, style: "display: block" %> + <%= form.email_field :email, value: params[:email_hint], required: true, autofocus: true, autocomplete: "email" %> + <%= form.hidden_field :context, value: "email-verify" %> +
+
+ <%= form.submit "Sign in" %> +
+<% end %> + +
\ No newline at end of file diff --git a/app/views/sessions/verify.html.erb b/app/views/sessions/verify.html.erb new file mode 100644 index 0000000..ac75b58 --- /dev/null +++ b/app/views/sessions/verify.html.erb @@ -0,0 +1,18 @@ +

Verify

+ +<%= form_with(url: sign_in_path) do |form| %> +
+ <%= form.label :email, style: "display: block" %> + <%= form.email_field :email, value: @email, required: true %> +
+
+ <%= form.label :code, style: "display: block" %> + <%= form.text_field :code, required: true, autofocus: true, autocomplete: "code" %> +
+ +
+ <%= form.submit "Verify" %> +
+<% end %> + +
\ No newline at end of file diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index bd5bcd2..6188333 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -3,6 +3,8 @@ # Version of your assets, change this if you want to expire all your assets. Rails.application.config.assets.version = "1.0" +Rails.application.config.assets.precompile += %w(application.js) + # Add additional assets to the asset load path. # Rails.application.config.assets.paths << Emoji.images_path diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb index c010b83..bbd4a64 100644 --- a/config/initializers/filter_parameter_logging.rb +++ b/config/initializers/filter_parameter_logging.rb @@ -4,5 +4,5 @@ # Use this to limit dissemination of sensitive information. # See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors. Rails.application.config.filter_parameters += [ - :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn + :passw, :secret, :_key, :crypt, :salt, :certificate, :otp, :ssn ] diff --git a/config/routes.rb b/config/routes.rb index dcc691c..70385b2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -57,6 +57,11 @@ post "ticket/stripe_client_secret", to: "ticket#stripe_client_secret" end + get "sign_in", to: "sessions#new" + post "verify", to: "sessions#verify" + post "sign_in", to: "sessions#create" + resources :sessions, only: [:index, :show, :destroy] + # Defines the root path route ("/") root "home#index" end