Skip to content

Instantly share code, notes, and snippets.

@indyarocks
Last active March 16, 2017 20:25
Show Gist options
  • Save indyarocks/5dad7fafd3f0d58a49afdd6f5113db5c to your computer and use it in GitHub Desktop.
Save indyarocks/5dad7fafd3f0d58a49afdd6f5113db5c to your computer and use it in GitHub Desktop.
Action to issue JWT token
module API
module V1
class SessionsController < ::API::MainController
skip_before_action :authenticate, only: [:create]
def create
# In users.rb
# def self.by_email_or_username(email:, username:)
# User.where('email = ? OR username = ?', email, username).first
# end
user = User.by_email_or_username(
email: auth_params[:email],
username: auth_params[:username]
)
if user.present? && user.authenticate(auth_params[:password])
jwt = Auth.issue({auth_token: user.auth_token}, Time.now + 1.year)
render json: {
success: true,
token: jwt
}, status: :created and return
else
render json: {
success: false,
error: 'Invalid Credentials'
}, status: :unauthorized and return
end
end
private
def auth_params
params.require(:user).permit(:email, :username, :password)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment