From 4f97941f13f76ae2a611cd996b06f2a63ecd2814 Mon Sep 17 00:00:00 2001 From: ichitrang Date: Sun, 25 May 2025 14:25:14 +0530 Subject: [PATCH 1/2] login created from backend --- .../app/controllers/sessions_controller.rb | 15 +++++++++ NoTicket-Backend/app/models/user.rb | 4 +++ NoTicket-Backend/config/database.yml | 33 +++++++------------ NoTicket-Backend/config/initializers/cors.rb | 25 ++++++-------- NoTicket-Backend/config/routes.rb | 11 ++----- NoTicket-Backend/db/seeds.rb | 14 +++----- .../src/components/LoginForm.jsx | 5 +-- 7 files changed, 50 insertions(+), 57 deletions(-) create mode 100644 NoTicket-Backend/app/controllers/sessions_controller.rb create mode 100644 NoTicket-Backend/app/models/user.rb diff --git a/NoTicket-Backend/app/controllers/sessions_controller.rb b/NoTicket-Backend/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..de2835c --- /dev/null +++ b/NoTicket-Backend/app/controllers/sessions_controller.rb @@ -0,0 +1,15 @@ +class SessionsController < ApplicationController + def new + render 'sessions/new' + end + + def create + user = User.find_by(email: params[:email]) + + if user&.authenticate(params[:password]) + render json: { message: "Login Successful", user: {id: user.id, email: user.email} }, status: :ok + else + render json: { error: "Invalid email or password" }, status: :unauthorized + end + end +end diff --git a/NoTicket-Backend/app/models/user.rb b/NoTicket-Backend/app/models/user.rb new file mode 100644 index 0000000..0710051 --- /dev/null +++ b/NoTicket-Backend/app/models/user.rb @@ -0,0 +1,4 @@ +class User < ApplicationRecord + has_secure_password + validates :email, presence: true, uniqueness: true +end \ No newline at end of file diff --git a/NoTicket-Backend/config/database.yml b/NoTicket-Backend/config/database.yml index 01bebb5..d4b031b 100644 --- a/NoTicket-Backend/config/database.yml +++ b/NoTicket-Backend/config/database.yml @@ -1,32 +1,21 @@ -# SQLite. Versions 3.8.0 and up are supported. -# gem install sqlite3 -# -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem "sqlite3" -# default: &default - adapter: sqlite3 - pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - timeout: 5000 + adapter: postgresql + encoding: unicode + pool: 5 + username: postgres + password: postgres + host: localhost development: <<: *default - database: storage/development.sqlite3 + database: noticket_development -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. test: <<: *default - database: storage/test.sqlite3 + database: noticket_test - -# SQLite3 write its data on the local filesystem, as such it requires -# persistent disks. If you are deploying to a managed service, you should -# make sure it provides disk persistence, as many don't. -# -# Similarly, if you deploy your application as a Docker container, you must -# ensure the database is located in a persisted volume. production: <<: *default - # database: path/to/persistent/storage/production.sqlite3 + database: noticket_production + username: postgres + password: <%= ENV['DATABASE_PASSWORD'] %> diff --git a/NoTicket-Backend/config/initializers/cors.rb b/NoTicket-Backend/config/initializers/cors.rb index 0c5dd99..0341570 100644 --- a/NoTicket-Backend/config/initializers/cors.rb +++ b/NoTicket-Backend/config/initializers/cors.rb @@ -1,16 +1,11 @@ -# Be sure to restart your server when you modify this file. +Rails.application.config.middleware.insert_before 0, Rack::Cors do + allow do + origins 'http://localhost:3001' -# Avoid CORS issues when API is called from the frontend app. -# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin Ajax requests. - -# Read more: https://github.com/cyu/rack-cors - -# Rails.application.config.middleware.insert_before 0, Rack::Cors do -# allow do -# origins "example.com" -# -# resource "*", -# headers: :any, -# methods: [:get, :post, :put, :patch, :delete, :options, :head] -# end -# end + resource '*', + headers: :any, + methods: [:get, :post, :put, :patch, :delete, :options, :head], + credentials: false + end +end + \ No newline at end of file diff --git a/NoTicket-Backend/config/routes.rb b/NoTicket-Backend/config/routes.rb index 0b9c55f..5c87897 100644 --- a/NoTicket-Backend/config/routes.rb +++ b/NoTicket-Backend/config/routes.rb @@ -1,11 +1,4 @@ Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - - # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. - # Can be used by load balancers and uptime monitors to verify that the app is live. - get "up" => "rails/health#show", as: :rails_health_check - get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker - get "manifest" => "rails/pwa#manifest", as: :pwa_manifest - # Defines the root path route ("/") - # root "posts#index" + post '/login', to: 'sessions#create' + get '/login', to: 'sessions#new' end diff --git a/NoTicket-Backend/db/seeds.rb b/NoTicket-Backend/db/seeds.rb index 4fbd6ed..eba6c13 100644 --- a/NoTicket-Backend/db/seeds.rb +++ b/NoTicket-Backend/db/seeds.rb @@ -1,9 +1,5 @@ -# This file should ensure the existence of records required to run the application in every environment (production, -# development, test). The code here should be idempotent so that it can be executed at any point in every environment. -# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). -# -# Example: -# -# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name| -# MovieGenre.find_or_create_by!(name: genre_name) -# end +User.create!( + email: "test@example.com", + password: "password123", + password_confirmation: "password123" +) diff --git a/noticket_frontend/src/components/LoginForm.jsx b/noticket_frontend/src/components/LoginForm.jsx index db2cb89..bc15c76 100644 --- a/noticket_frontend/src/components/LoginForm.jsx +++ b/noticket_frontend/src/components/LoginForm.jsx @@ -8,10 +8,11 @@ const LoginForm = () => { const handleLogin = async (e) => { e.preventDefault(); try { - const res = await axios.post('http://localhost:3000/api/login', { + const res = await axios.post('http://localhost:3000/login', { email, - password, + password }); + console.log('Login success:', res.data); alert("Login successful"); } catch (err) { From 8b10a98f62b018e97b83cffbd2732cb851e0ada2 Mon Sep 17 00:00:00 2001 From: ichitrang <104492460+ichitrang@users.noreply.github.com> Date: Sun, 25 May 2025 14:31:20 +0530 Subject: [PATCH 2/2] Update IntroSection.jsx --- noticket_frontend/src/components/IntroSection.jsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/noticket_frontend/src/components/IntroSection.jsx b/noticket_frontend/src/components/IntroSection.jsx index 2b0656e..ac1fe34 100644 --- a/noticket_frontend/src/components/IntroSection.jsx +++ b/noticket_frontend/src/components/IntroSection.jsx @@ -2,10 +2,9 @@ import React from 'react'; const IntroSection = () => (
-

Welcome to TrainTrackr

+

Welcome to NoTicket

- Your personal travel dashboard with a smooth scrolling train animation experience. - Plan, book, and track your journeys with ease. + A smart platform to help last-minute train ticket seekers connect with those looking to cancel their bookings — enabling secure, verified, and seamless ownership transfers.

);