Class: Caboose::RegisterController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/register_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#before_action, #before_before_action, #logged_in?, #logged_in_user, #login_user, #reject_param, #user_is_allowed, #validate_token, #var

Instance Method Details

#indexObject

GET /register



6
7
8
9
# File 'app/controllers/caboose/register_controller.rb', line 6

def index
  @return_url = params[:return_url].nil? ? "/" : params[:return_url];
  redirect_to @return_url if logged_in?
end

#registerObject

POST /register



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
44
45
46
47
48
49
50
# File 'app/controllers/caboose/register_controller.rb', line 12

def register
  
  resp = StdClass.new('error' => '', 'redirect' => '')
  
  if (logged_in?)
    resp.error = "Already logged in"
  else
    
    first_name  = params[:first_name]
    last_name   = params[:last_name]
    email       = params[:email]
    pass1       = params[:pass1]
    pass2       = params[:pass2]
                      
    if (first_name.nil? || first_name.strip.length == 0)
      resp.error = "Your first name is required."
    elsif (last_name.nil? || last_name.strip.length == 0)
      resp.error = "Your last name is required."
    elsif (email.nil? || email.strip.length == 0)
      resp.error = "Your email address is required."
    elsif (pass1.nil? || pass1.strip.length < 8)
      resp.error = "Your password must be at least 8 characters."
    elsif (pass2.nil? || pass1 != pass2)
      resp.error = "Your passwords don't match."
    else
      
      u = Caboose::User.new
      u.first_name    = first_name
      u.last_name     = last_name
      u.email         = email
      u.password      = Digest::SHA1.hexdigest(Caboose::salt + pass1)
      u.creation_date = DateTime.now
      u.save                    
      resp.redirect = "/login"

    end
  end
  render json: resp
end