diff --git a/backend/Gemfile b/backend/Gemfile index 2c53374..af0b723 100644 --- a/backend/Gemfile +++ b/backend/Gemfile @@ -35,6 +35,9 @@ gem "bootsnap", require: false # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin Ajax possible gem "rack-cors" +gem "redis" +gem 'redis-actionpack' + group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem gem "debug", platforms: %i[ mri windows ] diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock index 668bdc1..8157349 100644 --- a/backend/Gemfile.lock +++ b/backend/Gemfile.lock @@ -185,6 +185,19 @@ GEM rake (13.1.0) rdoc (6.6.2) psych (>= 4.0.0) + redis (5.1.0) + redis-client (>= 0.17.0) + redis-actionpack (5.4.0) + actionpack (>= 5, < 8) + redis-rack (>= 2.1.0, < 4) + redis-store (>= 1.1.0, < 2) + redis-client (0.21.0) + connection_pool + redis-rack (3.0.0) + rack-session (>= 0.2.0) + redis-store (>= 1.2, < 2) + redis-store (1.10.0) + redis (>= 4, < 6) reline (0.4.3) io-console (~> 0.5) rspec-core (3.13.0) @@ -232,6 +245,8 @@ DEPENDENCIES puma (>= 5.0) rack-cors rails (~> 7.1.3, >= 7.1.3.2) + redis + redis-actionpack rspec-rails sqlite3 (~> 1.4) tzinfo-data diff --git a/backend/app/controllers/redis_controller.rb b/backend/app/controllers/redis_controller.rb new file mode 100644 index 0000000..00cf46a --- /dev/null +++ b/backend/app/controllers/redis_controller.rb @@ -0,0 +1,10 @@ +class RedisController < ApplicationController + def session_set + session[:test_time] = Time.current + render json: { status: "ok" } + end + + def session_get + render json: { time: session[:test_time]&.iso8601 } + end +end diff --git a/backend/app/controllers/session_controller.rb b/backend/app/controllers/session_controller.rb new file mode 100644 index 0000000..2f6cc9b --- /dev/null +++ b/backend/app/controllers/session_controller.rb @@ -0,0 +1,6 @@ +class SessionController < ApplicationController + def current_user_set + session[:current_user_id] = User.find_by(address: params[:address])&.id + render json: { status: "ok" } + end +end diff --git a/backend/app/controllers/user_coupons_controller.rb b/backend/app/controllers/user_coupons_controller.rb index 41e8099..7395f5e 100644 --- a/backend/app/controllers/user_coupons_controller.rb +++ b/backend/app/controllers/user_coupons_controller.rb @@ -4,7 +4,7 @@ class UserCouponsController < ApplicationController # GET /user_coupons # GET /user_coupons.json def index - @user_coupons = User.first.user_coupons || [] + @user_coupons = User.find_by(id: session[:current_user_id]).user_coupons || [] end # GET /user_coupons/1 diff --git a/backend/app/controllers/users_controller.rb b/backend/app/controllers/users_controller.rb index 7a696f9..3ecb2aa 100644 --- a/backend/app/controllers/users_controller.rb +++ b/backend/app/controllers/users_controller.rb @@ -17,6 +17,8 @@ def show def create @user = User.find_or_create_by(user_params) + binding.irb + if @user.present? render :show, status: :created, location: @user else @@ -50,4 +52,8 @@ def set_user def user_params params.require(:user).permit(:address, :name, :avatar) end + + def current_user + @current_user ||= session[:current_user_id] && User.find_by(id: session[:current_user_id]) + end end diff --git a/backend/config/initializers/session_store.rb b/backend/config/initializers/session_store.rb new file mode 100644 index 0000000..f537788 --- /dev/null +++ b/backend/config/initializers/session_store.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +Rails.application.config.middleware.insert_after ActiveRecord::Migration::CheckPending, ActionDispatch::Cookies +Rails.application.config.middleware.insert_after ActionDispatch::Cookies, ActionDispatch::Session::RedisStore, + servers: ["redis://#{ENV.fetch("REDIS_HOST") { "localhost" }}:6379/0"], + expire_after: 3.days, + key: "_redis_session_transback" diff --git a/backend/config/routes.rb b/backend/config/routes.rb index 18a8897..ecf82ad 100644 --- a/backend/config/routes.rb +++ b/backend/config/routes.rb @@ -1,9 +1,16 @@ Rails.application.routes.draw do + resource :redis, only: [] do + # redis working checker + get "/session_set", to: "redis#session_set" + get "/session_get", to: "redis#session_get" + end + defaults format: :json do resources :coupons resources :organizations resources :users resources :user_coupons + get "/current_user_set", to: "session#current_user_set" end # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html diff --git a/backend/dump.rdb b/backend/dump.rdb new file mode 100644 index 0000000..8a1bbb9 Binary files /dev/null and b/backend/dump.rdb differ diff --git a/backend/spec/requests/redis_spec.rb b/backend/spec/requests/redis_spec.rb new file mode 100644 index 0000000..341367a --- /dev/null +++ b/backend/spec/requests/redis_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe "Redis", type: :request do + describe "GET /index" do + pending "add some examples (or delete) #{__FILE__}" + end +end diff --git a/backend/spec/requests/session_spec.rb b/backend/spec/requests/session_spec.rb new file mode 100644 index 0000000..ade7aca --- /dev/null +++ b/backend/spec/requests/session_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe "Sessions", type: :request do + describe "GET /index" do + pending "add some examples (or delete) #{__FILE__}" + end +end diff --git a/frontend/src/app/services/session.service.ts b/frontend/src/app/services/session.service.ts new file mode 100644 index 0000000..8f619cd --- /dev/null +++ b/frontend/src/app/services/session.service.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@angular/core'; +import { HttpClient, HttpParams } from '@angular/common/http'; + +@Injectable({ + providedIn: 'root', +}) +export class SessionService { + url = 'http://localhost:3000'; + + constructor(private httpClient: HttpClient) {} + + currentUserSession(address: string) { + const options = { + params: new HttpParams().set('address', address) + }; + return this.httpClient.get(`${this.url}/current_user_set`, options); + } +} diff --git a/frontend/src/app/services/wallet.service.ts b/frontend/src/app/services/wallet.service.ts index c66ce2b..0e8c4a2 100644 --- a/frontend/src/app/services/wallet.service.ts +++ b/frontend/src/app/services/wallet.service.ts @@ -3,6 +3,7 @@ import { environment } from 'src/environments/environment.dev'; import { createWeb3Modal, defaultConfig } from '@web3modal/ethers5'; import { UserService } from './user.service'; import { Observable, concatMap, map } from 'rxjs'; +import { SessionService } from './session.service'; const projectId = environment.wc_key; @@ -35,7 +36,10 @@ export class WalletService { private _isConnected = signal(false); private _address = signal(""); - constructor(private userService: UserService) {} + constructor( + private userService: UserService, + private sessionService: SessionService + ) {} subscribeConnection(): Observable { return new Observable(observer => { @@ -54,6 +58,7 @@ export class WalletService { ) }).pipe( concatMap(() => this.userService.postOrFetchUser(this._address())), + concatMap(() => this.sessionService.currentUserSession(this._address())), map(() => this._isConnected()) ); }