Skip to content
Permalink
Newer
Older
100644 75 lines (63 sloc) 2.28 KB
July 17, 2011 19:41
1
class SessionsController < ApplicationController
2
def new
3
@title = "sign in"
July 17, 2011 19:41
4
redirect_to root_path && return if logged_in?
March 28, 2011 01:18
5
end
6
April 11, 2011 20:54
7
# We have a bit of an interesting feature with the POST to /login.
8
# Normally, this would just log you in, but for super ease of use, we've
9
# decided to make it sign you up if you don't have an account yet, and log
10
# you in if you do. Therefore, we try to fetch your user from the DB, and
11
# check if you're there, which is the first half of the `if`. The `else`
12
# is your run-of-the-mill login procedure.
July 17, 2011 19:41
13
def create
14
u = User.find_by_case_insensitive_username(params[:username])
15
if u.nil? && admin_info.can_create_user?
16
# Grab the domain for this author from the request url
17
params[:domain] = root_url
19
author = Author.new_from_session!(session, params, root_url)
20
21
@user = User.new :author => author,
22
:username => params[:username],
23
:email => params[:email],
24
:password => params[:password]
27
if params[:password].length > 0
29
sign_in(@user)
30
flash[:notice] = "Thanks for signing up!"
31
32
if User.count == 1
33
# Administration options are available to the first user
34
@user.admin = true
35
@user.save
36
37
redirect_to "/admin"
38
else
39
redirect_to root_path
40
end
41
August 7, 2011 21:31
42
return
43
else
44
@user.errors.add(:password, "can't be empty")
45
end
March 28, 2011 01:18
46
end
48
if @user.errors.any?
49
error_message = render_to_string :partial => 'users/errors',
50
:locals => {:user => @user}
51
flash[:error] = error_message.html_safe
52
end
53
March 28, 2011 01:18
55
else
56
if user = User.authenticate(params[:username], params[:password])
57
sign_in(user)
March 28, 2011 01:18
58
flash[:notice] = "Login successful."
February 15, 2012 21:05
59
redirect_to root_path
July 17, 2011 19:41
60
return
March 28, 2011 01:18
61
end
62
flash[:error] = "The password given for username \"#{params[:username]}\" is incorrect.
63
64
If you are trying to create a new account, please choose a different username."
65
render :new
March 28, 2011 01:18
66
end
67
end
68
July 17, 2011 19:41
69
def destroy
July 17, 2011 19:41
71
flash[:notice] = "You've been logged out."
February 15, 2012 21:05
72
redirect_to root_path
March 28, 2011 01:18
73
end
74
75
end