Skip to content

Commit

Permalink
add session controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangplus committed Sep 7, 2024
1 parent b63c866 commit 9ded479
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//= link_tree ../images
//= link_tree ../builds
//= link_directory ../../javascript .js
3 changes: 3 additions & 0 deletions app/assets/javascript/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
9 changes: 9 additions & 0 deletions app/assets/javascript/controllers/application.js
Original file line number Diff line number Diff line change
@@ -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 }
7 changes: 7 additions & 0 deletions app/assets/javascript/controllers/hello_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
connect() {
this.element.textContent = "Hello World!"
}
}
8 changes: 8 additions & 0 deletions app/assets/javascript/controllers/index.js
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 14 additions & 7 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" }
Expand Down
4 changes: 4 additions & 0 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<button class="btn btn-primary">Primary</button>
</div>

<div>
current profile : <%= Current.session.try(:id) %>
</div>

<div class="card bg-base-100 w-96 shadow-xl">
<figure>
<img
Expand Down
14 changes: 14 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1>Sign in</h1>

<%= form_with(url: verify_path) do |form| %>
<div>
<%= 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" %>
</div>
<div>
<%= form.submit "Sign in" %>
</div>
<% end %>

<br>
18 changes: 18 additions & 0 deletions app/views/sessions/verify.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1>Verify</h1>

<%= form_with(url: sign_in_path) do |form| %>
<div>
<%= form.label :email, style: "display: block" %>
<%= form.email_field :email, value: @email, required: true %>
</div>
<div>
<%= form.label :code, style: "display: block" %>
<%= form.text_field :code, required: true, autofocus: true, autocomplete: "code" %>
</div>

<div>
<%= form.submit "Verify" %>
</div>
<% end %>

<br>
2 changes: 2 additions & 0 deletions config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/filter_parameter_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9ded479

Please sign in to comment.