I'm working on some Phoenix framework and i have encountered a weird problem (as usual). Whenever I try to create some Users, i get User with all fields set to nil. I'm using Mongo.Ecto/
def post_login(conn, %{"login" => login, "password" => password}) do
# IO.inspect Plug.Conn.read_body(conn)
a = User.changeset(%User{}, %{"login" => "login", "password" => "password"})
IO.inspect a
Repo.insert( a )
redirect conn, to: "/default"
end
And the model:
defmodule HelloWorld.User do
use HelloWorld.Web, :model
#primary_key {:id, :binary_id, autogenerate: true}
schema "users" do
field :login, :string
field :password, :string
end
#required_fields ~w()
#optional_fields ~w()
def changeset(model, params \\ :empty) do
model
|> cast(params, #required_fields, #optional_fields)
end
end
And the screen from console:
As you can see in the picture, both login and password fields are nils which makes me feel I've done something incredibly stupid.
The fields need to exist in the options to the cast/4 function:
#required_fields ~w()
#optional_fields ~w(login password)
def changeset(model, params \\ :empty) do
model
|> cast(params, #required_fields, #optional_fields)
end
Anything that is in required_fields but not in the params will add an error to that field on the changeset. If you want the fields to be required just move them to the required_fields list.
Related
When running my application using sinatra, I get the error message PG::SyntaxError at /bookmarks
ERROR: syntax error at or near "{" LINE 1: SELECT * FROM users WHERE id = {:id=>"5"} ^
It happens when I click the submit button on /users/new route which should then take me to index route /.
The backtrace provides the following information
/Users/BartJudge/Desktop/Makers_2018/bookmark-manager-2019/lib/database_connection.rb in async_exec
#connection.exec(sql)
/Users/BartJudge/Desktop/Makers_2018/bookmark-manager-2019/lib/database_connection.rb in query
#connection.exec(sql)
/Users/BartJudge/Desktop/Makers_2018/bookmark-manager-2019/lib/user.rb in find
result = DatabaseConnection.query("SELECT * FROM users WHERE id = #{id}")
app.rb in block in <class:BookmarkManager>
#user = User.find(id: session[:user_id])
This is the database_connection file
require 'pg'
class DatabaseConnection
def self.setup(dbname)
#connection = PG.connect(dbname: dbname)
end
def self.connection
#connection
end
def self.query(sql)
#connection.exec(sql)
end
end
This is the user model
require_relative './database_connection'
require 'bcrypt'
class User
def self.create(email:, password:)
encypted_password = BCrypt::Password.create(password
)
result = DatabaseConnection.query("INSERT INTO users (email, password) VALUES('#{email}', '#{encypted_password}') RETURNING id, email;")
User.new(id: result[0]['id'], email: result[0]['email'])
end
attr_reader :id, :email
def initialize(id:, email:)
#id = id
#email = email
end
def self.find(id)
return nil unless id
result = DatabaseConnection.query("SELECT * FROM users WHERE id = #{id}")
User.new(
id: result[0]['id'],
email: result[0]['email'])
end
end
This is the controller
require 'sinatra/base'
require './lib/bookmark'
require './lib/user'
require './database_connection_setup.rb'
require 'uri'
require 'sinatra/flash'
require_relative './lib/tag'
require_relative './lib/bookmark_tag'
class BookmarkManager < Sinatra::Base
enable :sessions, :method_override
register Sinatra::Flash
get '/' do
"Bookmark Manager"
end
get '/bookmarks' do
#user = User.find(id: session[:user_id])
#bookmarks = Bookmark.all
erb :'bookmarks/index'
end
post '/bookmarks' do
flash[:notice] = "You must submit a valid URL" unless Bookmark.create(url: params[:url], title: params[:title])
redirect '/bookmarks'
end
get '/bookmarks/new' do
erb :'bookmarks/new'
end
delete '/bookmarks/:id' do
Bookmark.delete(id: params[:id])
redirect '/bookmarks'
end
patch '/bookmarks/:id' do
Bookmark.update(id: params[:id], title: params[:title], url: params[:url])
redirect('/bookmarks')
end
get '/bookmarks/:id/edit' do
#bookmark = Bookmark.find(id: params[:id])
erb :'bookmarks/edit'
end
get '/bookmarks/:id/comments/new' do
#bookmark_id = params[:id]
erb :'comments/new'
end
post '/bookmarks/:id/comments' do
Comment.create(text: params[:comment], bookmark_id: params[:id])
redirect '/bookmarks'
end
get '/bookmarks/:id/tags/new' do
#bookmark_id = params[:id]
erb :'/tags/new'
end
post '/bookmarks:id/tags' do
tag = Tag.create(content: params[:tag])
BookmarkTag.create(bookmark_id: params[:id], tag_id: tag.id)
redirect '/bookmarks'
end
get '/users/new' do
erb :'users/new'
end
post '/users' do
user = User.create(email: params[:email], password: params[:password])
session[:user_id] = user.id
redirect '/bookmarks'
end
run! if app_file == $0
end
self.find(id), in the user model, is where the potentially offending SQL query resides.
I've tried;
"SELECT * FROM users WHERE id = #{id}"
and "SELECT * FROM users WHERE id = '#{id}'"
Beyond that, I'm stumped. The query looks fine, but sinatra is having none of it.
Hopefully someone can help me resolve this.
Thanks, in advance.
You're call find with a hash argument:
User.find(id: session[:user_id])
but it is expecting just the id:
class User
...
def self.find(id)
...
end
...
end
Then you end up interpolating a hash into your SQL string which results in invalid HTML.
You should be saying:
#user = User.find(session[:user_id])
to pass in just the id that User.find expects.
You're also leaving yourself open to SQL injection issues because you're using unprotected string interpolation for your queries rather than placeholders.
Your query method should use exec_params instead of exec and it should take some extra parameters for the placeholder values:
class DatabaseConnection
def self.query(sql, *values)
#connection.exec_params(sql, values)
end
end
Then things that call query should use placeholders in the SQL and pass the values separately:
result = DatabaseConnection.query(%q(
INSERT INTO users (email, password)
VALUES($1, $2) RETURNING id, email
), email, encypted_password)
result = DatabaseConnection.query('SELECT * FROM users WHERE id = $1', id)
...
I have integrated phoenix_swagger into my backend. I am autogenerating my swagger doc UI based off my controllers and using it to interactively test my endpoints.
Nonetheless, my routes are secured with Bearer JWTs. I am trying to figure out how to define authorization headers in phoenix_swagger with absolutely no luck.
I really appreciate the help Elixir friends!
For a visual:
swagger_path :create_user do
post "/api/v1/users/create"
description "Create a user."
parameters do
user :body, Schema.ref(:Create), "User to save", required: true
end
response 200, "Success"
end
def create_user(conn, query_params) do
changeset = User.changeset(%User{}, query_params)
with {:ok, user} <- Repo.insert(changeset),
{:ok, token, _claims} <- Guardian.encode_and_sign(user) do
conn
|> Conn.put_status(201)
|> render("jwt.json", jwt: token)
else
{:error, changeset} ->
conn
|> put_status(400)
|> render(ErrorView, "400.json", %{changeset: changeset})
end
end
Standard Swagger 2.0 JSON Reference:
How can I represent 'Authorization: Bearer <token>' in a Swagger Spec (swagger.json)
Okay, I think I got it! Adding security [%{Bearer: []}] to swagger_path passes the authorization token to the call.
Controller:
...
swagger_path :create_user do
post "/api/v1/users/create"
description "Create a user."
parameters do
user :body, Schema.ref(:Create), "User to save", required: true
end
security [%{Bearer: []}]
response 200, "Success"
end
def create_user(conn, query_params) do
changeset = User.changeset(%User{}, query_params)
with {:ok, user} <- Repo.insert(changeset),
{:ok, token, _claims} <- Guardian.encode_and_sign(user) do
conn
|> Conn.put_status(201)
|> render("jwt.json", jwt: token)
else
{:error, changeset} ->
conn
|> put_status(400)
|> render(ErrorView, "400.json", %{changeset: changeset})
end
end
...
Router:
...
def swagger_info do
%{
info: %{
version: "0.0.1",
title: "Server"
},
securityDefinitions: %{
Bearer: %{
type: "apiKey",
name: "Authorization",
in: "header"
}
}
}
end
...
This is something I need to look into myself. Here are a couple links that may help.
https://github.com/xerions/phoenix_swagger/blob/master/docs/getting-started.md#router
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#swagger-object
Error
Here's the error that I encountered when I'm testing my Account changeset. It seems like it would only be caused by the Ecto migration with wrongly structured database, but the ecto.migrate runs fine, as also Postgresql doesn't throw any error when I'm trying to insert a row using a similar changeset below.
** (Postgrex.Error) ERROR 23502 (not_null_violation): null value in column "email" violates not-null constraint
table: accounts
column: email
Failing row contains (118, 66168645856, 1, 2018-08-17 03:19:12.176247, 2018-08-17 03:19:12.17626, null, null, null, null).
code: account = insert(:account)
stacktrace:
(ecto) lib/ecto/adapters/sql.ex:554: Ecto.Adapters.SQL.struct/8
(ecto) lib/ecto/repo/schema.ex:547: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:213: anonymous fn/14 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:125: Ecto.Repo.Schema.insert!/4
test/schema/account_test.exs:26: (test)
Ecto migrations
migration_create_account.ex
def change do
create table(:accounts) do
add :phone_number, :string
add :access_level, :integer
timestamps()
end
end
migration_add_account.ex
def change do
alter table(:accounts) do
add :email, :string
add :auth_token, :string
add :auth_token_expires_at, :utc_datetime
add :signed_in_at, :utc_datetime
end
create unique_index(:accounts, :email, where: "email IS NOT NULL")
create unique_index(:accounts, [:phone_number], where: "phone_number IS NOT NULL")
end
ExMachina
factory.ex
def account_factory do
random_mobile_number = Enum.map(0..10, fn _i -> :rand.uniform(9) end)
|> List.foldl("", fn i, acc -> acc <> "#{i}" end)
%Account{
phone_number: random_mobile_number,
access_level: 1
}
end
ExUnit
account_test.exs
describe "Account.changeset/2" do
test "should check for valid phone number" do
account = insert(:account)
negative_number = %{phone_number: "-123233239" }
refute changeset(account, negative_number).valid?
end
end
Ecto schema and changeset
schema "accounts" do
field :email , :string
field :phone_number, :string
field :access_level , :integer
field :access_level_text, :string, virtual: true
field :auth_token , :string
field :auth_token_expires_at, :naive_datetime
field :signed_in_at , :naive_datetime
timestamps()
end
#required_params ~w(phone_number email access_level access_level_text)
def changeset(account, attrs) do
account
|> cast(attrs, #required_params)
|> cast_access_level_text()
|> validate_required([:access_level])
|> validate_required_contact_handle()
|> validate_number(:access_level, less_than: 3, greater_than: 0)
|> validate_subset(:access_level_text, #access_levels)
|> validate_format(:email, #email_regex)
|> validate_format(:phone_number, #phone_number_regex)
|> unique_constraint(:phone_number)
end
Thanks guys. What happened in my case is that because I changed migrations after using ecto.migrate, so that the migration changes differs between the test database and development database.
I just ran MIX_ENV=test mix ecto.reset to sync database between the environments.
I have an existing database and there is a unique index in that table
ALTER TABLE ONLY users ADD CONSTRAINT unique_document_id UNIQUE (document_id);
I dont have any migration in Ecto, and I want to insert a new record using a changeset. Here is the code from the model and the code to insert
defmodule User do
use Ecto.Schema
schema "users" do
field :name
field :email
field :document_id
end
def signup(name, id_number, email) do
changeset = User.changeset(%User{}, %{name: name,
email: email,
document_id: id_number})
if changeset.valid? do
IO.inspect "the chagenset is valid"
user = case Repo.insert(changeset) do
{:ok, model} -> {:ok, model }
{:error, changeset} -> {:error, changeset.errors}
end
end
def changeset(driver, params \\ :empty) do
driver
|> cast(params, [:document_id, :email, :name])
|> validate_required([:document_id, :email, :name])
|> unique_constraint(:document_id)
|> unique_constraint(:email)
end
end
end
But when I try to insert a duplicated user I get this error and changeset.valid? is true
11:04:17.896 [error] #PID<0.434.0> running App terminated
Server: localhost:4000 (http)
Request: POST /api/signup
** (exit) an exception was raised:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* unique: unique_document_id
If you would like to convert this constraint into an error, please
call unique_constraint/3 in your changeset and define the proper
constraint name. The changeset defined the following constraints:
* unique: users_document_id_index
(ecto) lib/ecto/repo/schema.ex:493: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
(elixir) lib/enum.ex:1229: Enum."-map/2-lists^map/1-0-"/2
(ecto) lib/ecto/repo/schema.ex:479: Ecto.Repo.Schema.constraints_to_errors/3
(ecto) lib/ecto/repo/schema.ex:213: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:684: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
(ecto) lib/ecto/adapters/sql.ex:620: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
(db_connection) lib/db_connection.ex:1275: DBConnection.transaction_run/4
(db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
You need to specify the constraint name in the call to unique_constraint since it's not the default Ecto convention (which would be users_document_id_index, as the error message says):
|> unique_constraint(:document_id, name: :unique_document_id)
If you have a unique constraint name for the email as well which is not users_name_index, you'll need to do the same for unique_constraint(:name) as well.
I am trying to set up facebook login integration with omniauth and devise.
Note that I have a MEMBER model which contains the devise info and a USER model that contains the member profile info (to keep them separate)
On the sign up page, the user has a nested form in the member sign up form and receives all the user data at that time. Once the member is saved, the email value for the saved member is also entered into the user table like so
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable
after_save :update_user
def update_user
user.email = self.email
user.save
end
I have the standard model method for the facebook data processing.....
def self.process_omniauth(auth)
where(provider: auth.provider, uid: auth.uid, email: auth.info.email).first_or_create do |member|
member.provider = auth.provider
member.uid = auth.uid
member.email = auth.info.email
end
end
But of course, in this case, there is no nested form so the 'user' is not instantiated and it throws an error when the update_user method is called on saving the member.
How can I modify this method above to instantiate a new user, when signing up via the facebook pathway?
EDIT: This works.....
def update_user
if User.find_by_member_id(self.id).present?
user.email = self.email
user.save
else
User.create(:email => self.email, :member_id => self.id)
end
end
BUT THIS RESULTS IN A CONSTRAINT ERROR - on email - it already exists in the database. From the logs, the else statement here appears to be attempting to create the user twice.
def update_user
if User.find_by_member_id(self.id).present?
user.email = self.email
user.save
else
User.create(:email => self.email)
end
end
Can anyone explain this? I am suprised I had to pass the foreign key to the user in the else block.
I'm not sure what fields are being stored in the User Model but what you should be doing here that in case of Facebook Callback you should create the User if the Member don't have an user associated with it. i.e your update_user should be something like this:
def update_user
if user.present?
user.email = self.email
user.save
else
User.create(:email => self.email, :member_id => self.id)
/*note that a lot of other information is returned in
facebook callback like First Name, Last Name, Image and
lot more, which i guess you should also start saving to
User Model */
end
end
**** EDIT ******** . You should also check if there is user with the same email and associate that with the member.
def update_user
if user.present?
user.email = self.email
user.save
else
user = User.find_by_email(self.email)
if user
user.update_attribute(:member_id, self.id)
else
User.create(:email => self.email, :member_id => self.id)
end
end