forked from AdaGold/media-ranker-revisited
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathsessions_controller.rb
72 lines (64 loc) · 1.97 KB
/
sessions_controller.rb
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class SessionsController < ApplicationController
before_action :require_login, except: [:create]
def create
auth_hash = request.env['omniauth.auth']
if auth_hash['uid']
user = User.find_by(uid: auth_hash[:uid], provider: 'github')
if user.nil?
# User doesn't match anything in the DB
# Attempt to create a new user
user = User.build_from_github(auth_hash)
user.save
redirect_to root_path
else
flash[:success] = "Logged in successfully"
redirect_to root_path
end
# If we get here, we have the user instance
session[:user_id] = user.id
else
flash[:error] = "Could not log in"
redirect_to root_path
end
# redirect_to root_path
end
# def index
# @user = User.find(session[:user_id]) # < recalls the value set in a previous request
# end
def destroy
session[:user_id] = nil
flash[:success] = "Successfully logged out!"
redirect_to root_path
end
# def login_form
# end
# def login
# username = params[:username]
# if username and user = User.find_by(username: username)
# session[:user_id] = user.id
# flash[:status] = :success
# flash[:result_text] = "Successfully logged in as existing user #{user.username}"
# else
# user = User.new(username: username)
# if user.save
# session[:user_id] = user.id
# flash[:status] = :success
# flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}"
# else
# flash.now[:status] = :failure
# flash.now[:result_text] = "Could not log in"
# flash.now[:messages] = user.errors.messages
# render "login_form", status: :bad_request
# return
# end
# end
# redirect_to root_path
# end
#
# def logout
# session[:user_id] = nil
# flash[:status] = :success
# flash[:result_text] = "Successfully logged out"
# redirect_to root_path
# end
end