-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocal_adapter.rb
More file actions
43 lines (36 loc) · 1.24 KB
/
local_adapter.rb
File metadata and controls
43 lines (36 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Auth::LocalAdapter
def initialize
# In-memory user store, no database interaction here
@users = [
{ email: "dev@example.com", password: "password", confirmed: true }
]
end
def initiate_auth(email, password)
# Find the user in the in-memory list
user = @users.find { |u| u[:email] == email }
# If no user is found, simulate user creation
if user.nil?
Rails.logger.info "Simulating creation of new user for email: #{email}"
# Add the new user to the in-memory users array (no DB interaction)
user = { email: email, password: password, confirmed: true }
# Simulate adding the user to the array
@users << user
end
# Check if the password matches
if user[:password] != password
raise Auth::Errors::InvalidCredentials.new("Incorrect password")
elsif !user[:confirmed]
raise Auth::Errors::UserNotConfirmed.new("User not confirmed")
end
# Simulate a successful auth response
{
uid: "local-dev-user-uid",
provider: "local",
token: generate_token(user)
}
end
def generate_token(user)
# Simulate token generation (JWT or other logic)
JWT.encode({ user_id: user[:email], exp: 24.hours.from_now.to_i }, "secret_key")
end
end