Skip to content

Instantly share code, notes, and snippets.

@richessler
Created December 29, 2015 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richessler/c4197e1b8a11137e0011 to your computer and use it in GitHub Desktop.
Save richessler/c4197e1b8a11137e0011 to your computer and use it in GitHub Desktop.
class UsersController < ApplicationController #for formalities, you know...
#for user login we would typically use a sessions controller, but for the sake of the assignment we'll call that `#new_user_session` within this controller
def new
@user = User.new
end
#user registration with no auth
def create
@user = User.new(user_params)
if @user.save
# signin user and redirect
flash[:success] = 'SUCCESS!'
redirect_to root_path # assuming views
else
# Errors handled by model validation
render :new
end
end
#SessionsController
def new_user_session
#assuming lots, but this will basically grab from MySQL db if exists
user = User.find_by(email: params[:email])
if user #and other auth here conditionally
sign_in user #function will be handled by sessions controller - example in comments following
redirect_to root_path #depending on starting page
else
flash[:error] = 'Invalid credentials!'
redirect_to new_user_session
end
end
# Example of sessions sign_in(user)
# def sign_in(user)
# cookies_here
# remember_tokens
# update_actions_for_valid_sessions
# reset_current_user_as_current_user
# end
private
#strong parameters ie, whitelisted params
def user_params
params.require(:user).permit(:email, :password, :type)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment