Commit 04ac087c authored by Klaus Stein's avatar Klaus Stein
Browse files

Add user session management

parent dde9a2ee
class SessionsController < ApplicationController
def create
@user = login(params[:username], params[:password])
if @user
redirect_back_or_to(:overview_index, notice: 'Login successful')
else
flash.now[:alert] = 'Login failed'
render action: 'new'
end
end
def destroy
logout
redirect_to(:login, notice: 'Logged out!')
end
end
......@@ -25,6 +25,7 @@ class UsersController < ApplicationController
respond_to do |format|
if @user.save
login(@user.username, user_params[:password])
format.html { redirect_to @user, notice: "User was successfully created." }
format.json { render :show, status: :created, location: @user }
else
......
module SessionsHelper
end
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# crypted_password :string
# salt :string
# username :string not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_username (username) UNIQUE
#
class User < ApplicationRecord
authenticates_with_sorcery!
......
......@@ -24,6 +24,14 @@
<li>
<%= link_to 'Images', image_maps_path, class: 'navlink work' %>
</li>
<li class="user">
<% if current_user %>
<%= link_to current_user.username, user_path(current_user) %>
<% else %>
<%= link_to "Login", :login %>
<% end %>
</li>
</ul>
</nav>
</header>
<%= form_with url: login_path, method: :post do |f| %>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="actions">
<%= f.submit "Login" %>
</div>
<% end %>
<h1>Login</h1>
<%= render 'form' %>
<%= link_to "Register", new_user_path %> |
<%= link_to 'Back', users_path %>
......@@ -11,4 +11,4 @@
</p>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
<%= link_to "Logout", :logout, method: :post %>
......@@ -2,6 +2,12 @@
#
Rails.application.routes.draw do
get 'login' => 'sessions#new', as: :login
post 'login' => 'sessions#create'
post 'logout' => 'sessions#destroy', as: :logout
get 'logout' => 'sessions#destroy' # for testing
resources :users
resources :repositories
resources :categories
......
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_12_16_064141) do
ActiveRecord::Schema.define(version: 2022_03_02_192312) do
# These are extensions that must be enabled in order to support this database
enable_extension "adminpack"
......@@ -158,6 +158,15 @@ ActiveRecord::Schema.define(version: 2021_12_16_064141) do
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "username", null: false
t.string "crypted_password"
t.string "salt"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["username"], name: "index_users_on_username", unique: true
end
create_table "wda_maps", force: :cascade do |t|
t.bigint "map_id", null: false
t.string "wda_id"
......
require "test_helper"
class SessionsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get sessions_new_url
assert_response :success
end
end
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# crypted_password :string
# salt :string
# username :string not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_username (username) UNIQUE
#
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
......
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# crypted_password :string
# salt :string
# username :string not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_username (username) UNIQUE
#
require "test_helper"
class UserTest < ActiveSupport::TestCase
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment