-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c03390
commit 09ddd12
Showing
8 changed files
with
57 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Api::V1::SessionsController < ApplicationController | ||
def create | ||
user = User.find_by(email: params[:email]) | ||
if user && user.authenticate(params[:password]) | ||
session[:user_id] = user.id | ||
render json: UserSerializer.new(user) | ||
else | ||
render json: { errors: [{ message: 'Invalid email or password' }] }, status: :unauthorized | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class ApplicationController < ActionController::API | ||
include ActionController::Cookies | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,20 @@ | |
expect(error[:errors].first[:message]).to eq("Password confirmation doesn't match Password") | ||
end | ||
end | ||
|
||
describe 'POST /sessions' do | ||
it 'logs in a existing user' do | ||
user = User.create(email: '[email protected]', password: 'password', password_confirmation: 'password') | ||
|
||
post '/api/v1/sessions', params: { email: user.email, password: user.password } | ||
|
||
expect(response).to have_http_status(:ok) | ||
|
||
expect(session[:user_id]).to eq(user.id) | ||
|
||
response_body = JSON.parse(response.body, symbolize_names: true) | ||
|
||
expect(response_body[:data][:attributes][:email]).to eq(user.email) | ||
end | ||
end | ||
end |