Skip to content

Commit ca3c94a

Browse files
enable local login for dev environments
1 parent 816c745 commit ca3c94a

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Auth::LocalAdapter
2+
def initialize
3+
# In-memory user store, no database interaction here
4+
@users = [
5+
{ email: "dev@example.com", password: "password", confirmed: true }
6+
]
7+
end
8+
9+
def initiate_auth(email, password)
10+
# Find the user in the in-memory list
11+
user = @users.find { |u| u[:email] == email }
12+
13+
# If no user is found, simulate user creation
14+
if user.nil?
15+
Rails.logger.info "Simulating creation of new user for email: #{email}"
16+
17+
# Add the new user to the in-memory users array (no DB interaction)
18+
user = { email: email, password: password, confirmed: true }
19+
20+
# Simulate adding the user to the array
21+
@users << user
22+
end
23+
24+
# Check if the password matches
25+
if user[:password] != password
26+
raise Auth::Errors::InvalidCredentials.new("Incorrect password")
27+
elsif !user[:confirmed]
28+
raise Auth::Errors::UserNotConfirmed.new("User not confirmed")
29+
end
30+
31+
# Simulate a successful auth response
32+
{
33+
uid: "local-dev-user-uid",
34+
provider: "local",
35+
token: generate_token(user)
36+
}
37+
end
38+
39+
def generate_token(user)
40+
# Simulate token generation (JWT or other logic)
41+
JWT.encode({ user_id: user[:email], exp: 24.hours.from_now.to_i }, "secret_key")
42+
end
43+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This adds a :auth_service_authenticatable accessor for use in the `devise` portion of a user model
2+
# Heavily inspired by https://www.endpointdev.com/blog/2023/01/using-devise-for-authentication-without-database-stored-accounts/
3+
module Devise
4+
module Models
5+
module AuthServiceAuthenticatable
6+
extend ActiveSupport::Concern
7+
8+
module ClassMethods
9+
# Recreates a resource from session data.
10+
#
11+
# It takes as many params as elements in the array returned in
12+
# serialize_into_session.
13+
def serialize_from_session(id, access_token = "")
14+
resource = find_by(id: id)
15+
if resource
16+
resource.access_token = access_token
17+
end
18+
resource
19+
end
20+
21+
# Returns an array with the data from the user that needs to be
22+
# serialized into the session.
23+
def serialize_into_session(user)
24+
[ user.id, user.access_token ]
25+
end
26+
end
27+
end
28+
end
29+
end

app-rails/app/services/auth_service.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
2-
require 'ostruct'
2+
3+
require "ostruct"
34

45
class AuthService
56
puts "AuthService class loaded"

0 commit comments

Comments
 (0)