Created
March 4, 2020 02:45
-
-
Save donrestarone/3cbbe4dc8ab6b4399bd967ff3edde9b4 to your computer and use it in GitHub Desktop.
simple sessions controller for authenticating in a rails application with cookies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::SessionsController < ApplicationController | |
before_action only: [:destroy] do | |
authenticate_cookie | |
end | |
def destroy | |
user = current_user | |
if user | |
cookies.delete(:jwt) | |
render json: {status: 'OK', code: 200} | |
else | |
render json: {status: 'session not found', code: 404} | |
end | |
end | |
def create | |
email = params["email"] | |
password = params["password"] | |
if email && password | |
login_hash = User.handle_login(email, password) | |
if login_hash | |
cookies.signed[:jwt] = {value: login_hash[:token], httponly: true} | |
render json: { | |
user_id: login_hash[:user_id], | |
name: login_hash[:name], | |
} | |
else | |
render json: {status: 'incorrect email or password', code: 422} | |
end | |
else | |
render json: {status: 'specify email address and password', code: 422} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment