Skip to content

Instantly share code, notes, and snippets.

@brianknapp
Created February 5, 2013 18:38
Show Gist options
  • Select an option

  • Save brianknapp/4716581 to your computer and use it in GitHub Desktop.

Select an option

Save brianknapp/4716581 to your computer and use it in GitHub Desktop.
This is a overly simplistic sign in user action that demonstrates roughly how a sign in might work in Obvious with users and user sessions
require_relative '../entities/user'
require_relative '../entities/user_session'
class SignInUser
def initialize user_jack, user_session_jack
@user_jack = user_jack
@user_session_jack = user_session_jack
end
def execute input
unless input.has_shape? email: String, password: String
raise ArgumentError.new 'invalid input format'
end
# find the user and check credentials
# use: UserJack.get, User.populate
user_data = @user_jack.find email: input[:email]
user = User.new
user.populate user_data
# set default id and values for new UserSession entity
session_data = {
id: -1,
email: user_data[:email],
token: generate_token
}
# create/populate UserSession object
# use: UserSession.populate
session = UserSession.new
session.populate session_data
# save session to jack
# use: UserSessionJack.save, UserSession.to_hash
# return the result
@user_session_jack.save session.to_hash
end
def generate_token
'random token!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment