feat: implement login
This commit is contained in:
@@ -20,17 +20,23 @@ class OmniauthController < ApplicationController
|
||||
user = User.find_by(email: email)
|
||||
|
||||
if user
|
||||
render json: {
|
||||
message: 'Login successful',
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name
|
||||
}
|
||||
}
|
||||
# Create authentication token (DeviseTokenAuth way)
|
||||
@resource = user
|
||||
@token = @resource.create_token
|
||||
@resource.save!
|
||||
|
||||
# Sign in the user
|
||||
sign_in(:user, @resource, store: false, bypass: false)
|
||||
|
||||
# Update sign in tracking
|
||||
@resource.update_tracked_fields!(request)
|
||||
|
||||
# Render success response using Chatwoot's auth template
|
||||
render partial: 'devise/auth', formats: [:json], locals: { resource: @resource }
|
||||
else
|
||||
render json: {
|
||||
error: 'User not found'
|
||||
error: 'User not found',
|
||||
message: "No user exists with email: #{email}"
|
||||
}, status: :not_found
|
||||
end
|
||||
else
|
||||
|
||||
@@ -36,8 +36,29 @@ RSpec.describe 'SAML Authentication API', type: :request do
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['message']).to eq('Login successful')
|
||||
expect(json_response['user']['email']).to eq('john.doe@example.com')
|
||||
expect(json_response['data']).to be_present
|
||||
expect(json_response['data']['email']).to eq('john.doe@example.com')
|
||||
expect(json_response['data']['id']).to eq(existing_user.id)
|
||||
end
|
||||
|
||||
it 'creates authentication tokens for the user' do
|
||||
expect do
|
||||
post "/auth/saml/#{account.id}/callback",
|
||||
env: { 'omniauth.auth' => auth_hash },
|
||||
as: :json
|
||||
end.to change { existing_user.reload.tokens.count }.by(1)
|
||||
end
|
||||
|
||||
it 'updates user sign in tracking' do
|
||||
existing_user
|
||||
|
||||
expect do
|
||||
post "/auth/saml/#{account.id}/callback",
|
||||
env: { 'omniauth.auth' => auth_hash },
|
||||
as: :json
|
||||
end.to change { existing_user.reload.sign_in_count }.by(1)
|
||||
|
||||
expect(existing_user.reload.current_sign_in_at).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user