Archived
update minecraft name changes automatically
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
class SessionsController < ApplicationController
|
class SessionsController < ApplicationController
|
||||||
|
|
||||||
|
include UsersHelper
|
||||||
|
|
||||||
def new
|
def new
|
||||||
if current_user
|
if current_user
|
||||||
flash[:alert] = "You are already logged in!"
|
flash[:alert] = "You are already logged in!"
|
||||||
@@ -19,8 +21,17 @@ class SessionsController < ApplicationController
|
|||||||
flash[:alert] = "You are banned!"
|
flash[:alert] = "You are banned!"
|
||||||
else
|
else
|
||||||
session[:user_id] = user.id
|
session[:user_id] = user.id
|
||||||
flash[:alert] = "Remember to validate your email! Your account may be deleted soon!" if !user.confirmed?
|
|
||||||
flash[:notice] = "Logged in!"
|
flash[:notice] = "Logged in!"
|
||||||
|
|
||||||
|
new_ign = fetch_name(user.uuid)
|
||||||
|
if new_ign.present? && new_ign != user.ign
|
||||||
|
user.name = new_ign if user.ign == user.name
|
||||||
|
user.ign = new_ign
|
||||||
|
user.save
|
||||||
|
flash[:notice] += " Your name has been changed to #{new_ign}!"
|
||||||
|
end
|
||||||
|
|
||||||
|
flash[:alert] = "Remember to validate your email! Your account may be deleted soon!" if !user.confirmed?
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
flash[:alert] = "You're doing it wrong!"
|
flash[:alert] = "You're doing it wrong!"
|
||||||
|
|||||||
@@ -27,4 +27,26 @@ require "open-uri"
|
|||||||
yt
|
yt
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_name(uuid)
|
||||||
|
uri = URI.parse("https://api.mojang.com/user/profiles/#{CGI.escape(uuid)}/names")
|
||||||
|
http = Net::HTTP.new(uri.host, uri.port)
|
||||||
|
http.open_timeout = 3
|
||||||
|
http.read_timeout = 3
|
||||||
|
http.use_ssl = true
|
||||||
|
|
||||||
|
begin
|
||||||
|
response = http.get(uri)
|
||||||
|
if response.code == "200"
|
||||||
|
data = JSON.load(response.body)
|
||||||
|
return data.last["name"]
|
||||||
|
end
|
||||||
|
rescue => e
|
||||||
|
Rails.logger.error "----"
|
||||||
|
Rails.logger.error "Failed to get mojang profile for #{uuid}"
|
||||||
|
Rails.logger.error e.message
|
||||||
|
Rails.logger.error "----"
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user