Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
15 changes: 15 additions & 0 deletions backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions backend/app/controllers/redis_controller.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions backend/app/controllers/session_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion backend/app/controllers/user_coupons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions backend/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions backend/config/initializers/session_store.rb
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Binary file added backend/dump.rdb
Binary file not shown.
7 changes: 7 additions & 0 deletions backend/spec/requests/redis_spec.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions backend/spec/requests/session_spec.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions frontend/src/app/services/session.service.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 6 additions & 1 deletion frontend/src/app/services/wallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<boolean> {
return new Observable<boolean>(observer => {
Expand All @@ -54,6 +58,7 @@ export class WalletService {
)
}).pipe(
concatMap(() => this.userService.postOrFetchUser(this._address())),
concatMap(() => this.sessionService.currentUserSession(this._address())),
map(() => this._isConnected())
);
}
Expand Down